function initCalendar(_hold){
    if(_hold.is(':hidden')) return;
    var month_name = ['Январь','Февраль','Март','Апрель','Май','Июнь','Июль','Август','Сентябрь','Октябрь','Ноябрь','Декабрь'];
    var i_month_name = ['января','февраля','марта','апреля','мая','июня','июля','августа','сентября','октября','ноября','декабря'];
    var _date = new Date();
    var curr_date = new Date();

    var day_h = $('<div class="days-hold"></div>').get(0);
    var month_h = $('<div class="months-hold"></div>').get(0);
    var color_f = true;

    _hold.find('div.calendar').empty().append(day_h).append(month_h);
    var day_list = [];

    _hold.find('div.calendar').customScrollV();

    var info_text = _hold.find('span.selected-info');
    var info_year = _hold.find('span.info');
    var btn_prev = _hold.find('a.btn-prev');
    var btn_next = _hold.find('a.btn-next');
    var btn_clear = _hold.find('a.btn-clear');
    var btn_search = _hold.find('a.btn-search');
    // pgn
    // initial hidden inputs values
    var date1 = _hold.parents('div.datapicker').find('input[name="date1"]');
    var date2 = _hold.parents('div.datapicker').find('input[name="date2"]');
    var dt1 = null;
    var dt2 = null;
    if (date1.val()){
        dt1_list = date1.val().split(' ');
        dt1 = new Date(parseInt(dt1_list[2]), parseInt(dt1_list[1])-1, parseInt(dt1_list[0]));
    }
    if (date2.val()){
        dt2_list = date2.val().split(' ');
        dt2 = new Date(parseInt(dt2_list[2]), parseInt(dt2_list[1])-1, parseInt(dt2_list[0]));
    }
    btn_prev.hide();
    info_text.empty();
    info_year.html(curr_date.getFullYear());
    /*----------- INIT -----------*/
    /*--- init current current ---*/
    function initCurrent(){
        day_h.innerHTML = '';
        month_h.innerHTML = '';
        day_list = [];
        var t_date = _date.getDate();
        var t_month = _date.getMonth();
        var t_year = _date.getYear();
        _date.setDate(0);
        _date.setDate(_date.getDate() - _date.getDay() + 1);
        
        var t_el;
    
        if(_date.getDate() != 1){
            while(_date.getDate() != 1){
                t_el = document.createElement('SPAN');
                t_el.innerHTML = _date.getDate();
                t_el.className = 'empty'
                t_el._day = _date.getDate();
                t_el._month = _date.getMonth();
                t_el._year = _date.getFullYear();
                if(color_f) t_el.className += ' alt';
                day_h.appendChild(t_el);
                _date.setDate(_date.getDate() + 1);
            }
        }
        color_f = !color_f;
        do{
            t_el = document.createElement('SPAN');
            t_el.innerHTML = _date.getDate();
            t_el._day = _date.getDate();
            t_el._month = _date.getMonth();
            t_el._year = _date.getFullYear();
            if(_date.getMonth() == t_month && _date.getDate() < t_date) t_el.className = 'empty';
            else day_list.push(t_el);
            if(color_f) t_el.className += ' alt';
            day_h.appendChild(t_el);
            _date.setDate(_date.getDate() + 1);
            if(_date.getDate() == 1) color_f = !color_f;
        }
        while(_date.getYear() != t_year+1);
    
        while(_date.getDay() != 1){
            t_el = document.createElement('SPAN');
            t_el.innerHTML = _date.getDate();
            t_el._day = _date.getDate();
            t_el._month = _date.getMonth();
            t_el._year = _date.getFullYear();
            if(color_f) t_el.className += ' alt';
            day_h.appendChild(t_el);
            day_list.push(t_el);
            _date.setDate(_date.getDate() + 1);
        }
    
        /*--- ---*/
        if(day_list[0]._month%2) color_f = !color_f;
        var t_h = 0;
        t_el = document.createElement('SPAN');
        t_el.className = 'numb-'+t_month;
        if(color_f) t_el.className += ' alt';
        t_el.innerHTML = month_name[t_month];
        month_h.appendChild(t_el);
        for(var i = 0; i < day_list.length; i++){
            if(day_list[i]._month != t_month){
                t_el.style.height = day_list[i].offsetTop - t_h + 'px';
                t_month = day_list[i]._month;
                t_h = day_list[i].offsetTop;
                color_f = !color_f;
                t_el = document.createElement('SPAN');
                t_el.className = 'numb-'+t_month;
                if(color_f) t_el.className += ' alt';
                t_el.innerHTML = month_name[t_month];
                month_h.appendChild(t_el);
            }
        }
        t_el.style.height = day_h.offsetHeight - t_h + 'px';
        if(t_el.className.indexOf('numb-0') != -1) t_el.className += ' last';
        /*--- ---*/
        
        if(_hold.hasClass('day')){
            initActions_d();
            period_b = -2;
            period_e = -2;
            numb_p_b = -1;
            numb_p_e = -1;
            numb_p_t = -1;
        }
        else{
            initActions_p();
            a_day = -1;
        }
    }
    /*--- init more years callendar ---*/
    function initMore(){
        day_h.innerHTML = '';
        month_h.innerHTML = '';
        day_list = [];
        var t_date = _date.getDate();
        var t_month = _date.getMonth();
        var t_year = _date.getYear();
    
        _date.setDate(0);
        _date.setDate(_date.getDate() - _date.getDay() + 1);
    
        var t_el;
    
        if(_date.getDate() != 1){
            while(_date.getDate() != 1){
                t_el = document.createElement('SPAN');
                t_el.innerHTML = _date.getDate();
                t_el._day = _date.getDate();
                t_el._month = _date.getMonth();
                t_el._year = _date.getFullYear();
                if(color_f) t_el.className += ' alt';
                day_h.appendChild(t_el);
                day_list.push(t_el);
                _date.setDate(_date.getDate() + 1);
            }
        }
        color_f = !color_f;
        do{
            t_el = document.createElement('SPAN');
            t_el.innerHTML = _date.getDate();
            t_el._day = _date.getDate();
            t_el._month = _date.getMonth();
            t_el._year = _date.getFullYear();
            if(color_f) t_el.className += ' alt';
            day_h.appendChild(t_el);
            day_list.push(t_el);
            _date.setDate(_date.getDate() + 1);
            if(_date.getDate() == 1) color_f = !color_f;
        }
        while(_date.getYear() != t_year+1);
    
        while(_date.getDay() != 1){
            t_el = document.createElement('SPAN');
            t_el.innerHTML = _date.getDate();
            t_el._day = _date.getDate();
            t_el._month = _date.getMonth();
            t_el._year = _date.getFullYear();
            if(color_f) t_el.className += ' alt';
            day_h.appendChild(t_el);
            day_list.push(t_el);
            _date.setDate(_date.getDate() + 1);
        }
    
        /*--- ---*/
        var t_h = 0;
        t_el = document.createElement('SPAN');
        t_el.className = 'numb-'+t_month;
        if(color_f) t_el.className += ' alt';
        t_el.innerHTML = month_name[t_month];
        month_h.appendChild(t_el);
        for(var i = 0; i < day_list.length; i++){
            if(day_list[i]._month != t_month){
                t_el.style.height = day_list[i].offsetTop - t_h + 'px';
                t_month = day_list[i]._month;
                t_h = day_list[i].offsetTop;
                color_f = !color_f;
                t_el = document.createElement('SPAN');
                t_el.className = 'numb-'+t_month;
                if(color_f) t_el.className += ' alt';
                t_el.innerHTML = month_name[t_month];
                month_h.appendChild(t_el);
            }
        }
        t_el.style.height = day_h.offsetHeight - t_h + 'px';
        if(t_el.className.indexOf('numb-0') != -1) t_el.className += ' last';
        /*--- ---*/
        if(_hold.hasClass('day')) initActions_d();
        else initActions_p();
    }

    /*----------- ACTIONS -----------*/
    var _f1 = false;
    var _f2 = false;
    var _f3 = false;
    var _f4 = false;
    var a_day = -1;
    var period_b = -2;
    var period_e = -2;
    var numb_p_b = -1;
    var numb_p_e = -1;
    var numb_p_t = -1;
    /*--- day action's ---*/
    function initActions_d(){
        // pgn 
        // specify a_day value corresponding to initial values of hidden inputs
        if (dt1){
            for(var i = 0; i < day_list.length; i++){
                if((day_list[i]._year == dt1.getFullYear()) && (day_list[i]._month == dt1.getMonth()) && (day_list[i]._day == dt1.getDate())){
                    //day_list[i].className += ' active-day';
                    a_day = day_list[i];
                }
            }
            //if (typeof(initial_set) != 'undefined'){
                //y = dt1.getFullYear();
                //if (y >= _date.getFullYear()){
                    //btn_prev.show();
                    //info_year.html(y);
                    //initMore();
                //}
            //} else initial_set = true;
        }
        //--/
        curr_date = new Date();
        if(a_day != -1){
            for(var i = 0; i < day_list.length; i++){
                if((a_day._year == day_list[i]._year) && (a_day._month == day_list[i]._month) && (a_day._day == day_list[i]._day)){
                    day_list[i].className += ' active-day';
                    a_day = day_list[i];
                }
            }
            info_text.html(a_day._day + ' ' + i_month_name[a_day._month] + ' ' + a_day._year +'г.');
        }
        else{
            info_text.html('');
        }
        for(var i = 0; i < day_list.length; i++){
            if(day_list[i]._year == curr_date.getFullYear() && day_list[i]._month == curr_date.getMonth() && day_list[i]._day == curr_date.getDate()) day_list[i].className += ' current';
            day_list[i].onmouseover = function(){
                this.className += ' hover';
                if(_f1 && a_day != this){
                    if(a_day != -1) a_day.className = a_day.className.replace('active-day','');
                    this.className += ' active-day';
                    a_day = this;
                    info_text.html(a_day._day + ' ' + i_month_name[a_day._month] + ' ' + a_day._year +'г.');
                }
            }
            day_list[i].onmouseout = function(){
                this.className = this.className.replace('hover','');
            }
            day_list[i].onclick = function(){
                if(a_day != -1) a_day.className = a_day.className.replace('active-day','');
                this.className += ' active-day';
                a_day = this;
                info_text.html(a_day._day + ' ' + i_month_name[a_day._month] + ' ' + a_day._year +'г.');
            }
            day_list[i].onmousedown = function(){
                if(this == a_day){
                    _f1 = true;
                    return false;
                }
            }
            day_list[i].onmouseup = function(){
                _f1 = false;
            }
            day_list[i].onselectstart = function(){
                return false;
            }
        }
        if(a_day.offsetTop && -a_day.offsetTop + a_day.offsetHeight*2 <= 0) _hold.find('div.calendar > div.scroll-content').css('top', -a_day.offsetTop + a_day.offsetHeight*2);
        else _hold.find('div.calendar > div.scroll-content').css('top', 0);
        _hold.find('div.calendar').customScrollV();
    }
    /*--- diapazon action's ---*/
    function initActions_p(){
        curr_date = new Date();
        if(period_b ==-2 && period_e == -2){
            numb_p_e = -1;
            numb_p_b = -1;
            if (dt1) {
                _pdate = new Date();
                _pdate.setTime(dt1.valueOf());
                for(var i = 0; i < day_list.length; i++){
                    if(day_list[i]._year == _pdate.getFullYear() && day_list[i]._month == _pdate.getMonth() && day_list[i]._day == _pdate.getDate()) day_list[i].className += ' current';
                    day_list[i]._i = i;
                    if(_pdate.getMonth() == day_list[i]._month && _pdate.getDate() == day_list[i]._day){
                        day_list[i].className += ' period-b';
                        period_b = day_list[i];
                        numb_p_b = i;
                        if (dt2){
                            day = 1000*60*60*24;
                            diff = Math.ceil((dt2.getTime()-dt1.getTime())/(day))
                            numb_p_e = i + diff;
                        } else numb_p_e = i + 6;
                    }
                    if(i > numb_p_b && i < numb_p_e){
                        day_list[i].className += ' period-c';
                    }
                    if(i == numb_p_e){
                        day_list[i].className += ' period-e';
                        period_e = day_list[i];
                    }
                }
            }
            else if(curr_date.getFullYear() == day_list[10]._year){
                for(var i = 0; i < day_list.length; i++){
                    if(day_list[i]._year == curr_date.getFullYear() && day_list[i]._month == curr_date.getMonth() && day_list[i]._day == curr_date.getDate()) day_list[i].className += ' current';
                    day_list[i]._i = i;
                    if(curr_date.getMonth() == day_list[i]._month && curr_date.getDate() == day_list[i]._day){
                        day_list[i].className += ' period-b';
                        period_b = day_list[i];
                        numb_p_b = i;
                        numb_p_e = i + 6;
                    }
                    if(i > numb_p_b && i < numb_p_e){
                        day_list[i].className += ' period-c';
                    }
                    if(i == numb_p_e){
                        day_list[i].className += ' period-e';
                        period_e = day_list[i];
                    }
                }
            } 
        }
        else{
            numb_p_e = day_list.length;
            numb_p_b = day_list.length;
            for(var i = 0; i < day_list.length; i++){
                if(day_list[i]._year == curr_date.getFullYear() && day_list[i]._month == curr_date.getMonth() && day_list[i]._day == curr_date.getDate()) day_list[i].className += ' current';
                day_list[i]._i = i;
                if(day_list[i]._year == period_b._year && day_list[i]._month == period_b._month && day_list[i]._day == period_b._day){
                    period_b = day_list[i];
                    numb_p_b = i;
                }
                if(day_list[i]._year == period_e._year && day_list[i]._month == period_e._month && day_list[i]._day == period_e._day){
                    period_e = day_list[i];
                    numb_p_e = i;
                }
            }
            if(period_b != -1 && period_e != -1){
                for(var i = 0; i < day_list.length; i++){
                    if(day_list[i]._year == period_b._year && day_list[i]._month == period_b._month && day_list[i]._day == period_b._day){
                        day_list[i].className += ' period-b';
                        period_b = day_list[i];
                        numb_p_b = i;
                    }
                    if(day_list[i]._year == period_e._year && day_list[i]._month == period_e._month && day_list[i]._day == period_e._day){
                        day_list[i].className += ' period-e';
                        period_e = day_list[i];
                        numb_p_e = i;
                    }
                    if(i < numb_p_e && i > numb_p_b){
                        day_list[i].className += ' period-c';
                    }
                }
            }
            if(numb_p_e == day_list.length && period_e._year < day_list[10]._year){
                for(var i = 0; i < day_list.length; i++){
                    day_list[i].className = day_list[i].className.replace('period-c','');
                }
                numb_p_e = -1;
            }
            if(numb_p_b == day_list.length && period_b._year < day_list[10]._year){
                if(period_e != -1){
                    for(var i = 0; i < numb_p_e; i++){
                        day_list[i].className += ' period-c';
                    }
                    numb_p_b = -1;
                }
                else{
                    numb_p_e = -1;
                }
            }
        }
        if(period_b != -1 && period_e != -1) info_text.html(period_b._day + ' ' + i_month_name[period_b._month] + ' - ' + period_e._day + ' ' + i_month_name[period_e._month]);
        for(var i = 0; i < day_list.length; i++){
            day_list[i].onmouseover = function(){
                this.className += ' hover';
                if(_f2){
                    if(numb_p_t == -1) numb_p_t = numb_p_e;
                    if(this._i < numb_p_t){
                        for(var j = numb_p_b+1; j < numb_p_e; j++){
                            day_list[j].className = day_list[j].className.replace('period-c','');
                        }
                        period_b.className = period_b.className.replace('period-b', '');
                        period_b = this;
                        numb_p_b = this._i;
                        if(period_b.className.indexOf('period-b') == -1) period_b.className += ' period-b';
                        if(numb_p_e != day_list.length){
                            period_e.className = period_e.className.replace('period-e', '');
                            period_e = day_list[numb_p_t];
                            numb_p_e = numb_p_t;
                            if(period_e.className.indexOf('period-e') == -1) period_e.className +=' period-e';
                        }
                        for(var j = numb_p_b+1; j < numb_p_e; j++){
                            if(day_list[j].className.indexOf('period-c') == -1) day_list[j].className += ' period-c';
                        }
                    }
                    else if(this._i > numb_p_t){
                        for(var j = numb_p_b+1; j < numb_p_e; j++){
                            day_list[j].className = day_list[j].className.replace('period-c','');
                        }
                        if(numb_p_e != day_list.length){
                            period_e.className = period_e.className.replace('period-e', '');
                            period_e = this;
                            numb_p_e = this._i;
                            if(period_e.className.indexOf('period-e') == -1) period_e.className +=' period-e';
                        }
                        period_b.className = period_b.className.replace('period-b', '');
                        period_b = day_list[numb_p_t];
                        numb_p_b = numb_p_t;
                        if(period_b.className.indexOf('period-b') == -1) period_b.className += ' period-b';
                        for(var j = numb_p_b+1; j < numb_p_e; j++){
                            if(day_list[j].className.indexOf('period-c') == -1) day_list[j].className += ' period-c';
                        }
                    }
                    info_text.html(period_b._day + ' ' + i_month_name[period_b._month] + ' - ' + period_e._day + ' ' + i_month_name[period_e._month]);
                }
                if(_f3){
                    if(numb_p_t == -1) numb_p_t = numb_p_b;
                    if(this._i < numb_p_t){
                        for(var j = numb_p_b+1; j < numb_p_e; j++){
                            day_list[j].className = day_list[j].className.replace('period-c','');
                        }
                        if(numb_p_b != -1){
                            period_b.className = period_b.className.replace('period-b', '');
                            period_b = this;
                            numb_p_b = this._i;
                            if(period_b.className.indexOf('period-b') == -1) period_b.className += ' period-b';
                        }
                        period_e.className = period_e.className.replace('period-e', '');
                        period_e = day_list[numb_p_t];
                        numb_p_e = numb_p_t;
                        if(period_e.className.indexOf('period-e') == -1) period_e.className +=' period-e';
                        for(var j = numb_p_b+1; j < numb_p_e; j++){
                            if(day_list[j].className.indexOf('period-c') == -1) day_list[j].className += ' period-c';
                        }
                    }
                    else if(this._i > numb_p_t){
                        for(var j = numb_p_b+1; j < numb_p_e; j++){
                            day_list[j].className = day_list[j].className.replace('period-c','');
                        }
                        period_e.className = period_e.className.replace('period-e', '');
                        period_e = this;
                        numb_p_e = this._i;
                        if(period_e.className.indexOf('period-e') == -1) period_e.className +=' period-e';
                        if(numb_p_b != -1){
                            period_b.className = period_b.className.replace('period-b', '');
                            period_b = day_list[numb_p_t];
                            numb_p_b = numb_p_t;
                            if(period_b.className.indexOf('period-b') == -1) period_b.className += ' period-b';
                        }
                        for(var j = numb_p_b+1; j < numb_p_e; j++){
                            if(day_list[j].className.indexOf('period-c') == -1) day_list[j].className += ' period-c';
                        }
                    }
                    info_text.html(period_b._day + ' ' + i_month_name[period_b._month] + ' - ' + period_e._day + ' ' + i_month_name[period_e._month]);
                }
                if(_f4){
                    if(numb_p_t < this._i){
                        if(period_b != -1 && period_e != -1){
                            period_b.className = period_b.className.replace('period-b','');
                            period_e.className = period_e.className.replace('period-e','');
                            for(var j = numb_p_b+1; j < numb_p_e; j++){
                                day_list[j].className = day_list[j].className.replace('period-c','');
                            }
                        }
                        numb_p_b = numb_p_t;
                        period_b = day_list[numb_p_b];
                        if(period_b.className.indexOf('period-b') == -1) period_b.className += ' period-b';
                        numb_p_e = this._i;
                        period_e = day_list[numb_p_e];
                        if(period_e.className.indexOf('period-e') == -1) period_e.className += ' period-e';
                        for(var j = numb_p_b+1; j < numb_p_e; j++){
                            if(day_list[j].className.indexOf('period-c') == -1) day_list[j].className += ' period-c';
                        }
                        info_text.html(period_b._day + ' ' + i_month_name[period_b._month] + ' - ' + period_e._day + ' ' + i_month_name[period_e._month]);
                    }
                    else if(numb_p_t > this._i){
                        if(period_b != -1 && period_e != -1){
                            period_b.className = period_b.className.replace('period-b','');
                            period_e.className = period_e.className.replace('period-e','');
                            for(var j = numb_p_b+1; j < numb_p_e; j++){
                                day_list[j].className = day_list[j].className.replace('period-c','');
                            }
                        }
                        numb_p_b = this._i;
                        period_b = day_list[numb_p_b];
                        if(period_b.className.indexOf('period-b') == -1) period_b.className += ' period-b';
                        numb_p_e = numb_p_t;
                        period_e = day_list[numb_p_e];
                        if(period_e.className.indexOf('period-e') == -1) period_e.className += ' period-e';
                        for(var j = numb_p_b+1; j < numb_p_e; j++){
                            if(day_list[j].className.indexOf('period-c') == -1) day_list[j].className += ' period-c';
                        }
                        info_text.html(period_b._day + ' ' + i_month_name[period_b._month] + ' - ' + period_e._day + ' ' + i_month_name[period_e._month]);
                    }
                }
                if(period_b != -1 && period_e == -1){
                    if((this._year < period_b._year) || (this._year == period_b._year && this._month < period_b._month) || (this._year == period_b._year && this._month == period_b._month && this._i < numb_p_b)){
                        if(numb_p_t != -1){
                            for(var j = numb_p_t; j < numb_p_b; j++){
                                day_list[j].className = day_list[j].className.replace('period-c', '');
                            }
                            for(var j = this._i; j < numb_p_t; j++){
                                day_list[j].className = day_list[j].className.replace('period-c', '');
                            }
                            day_list[numb_p_t].className = day_list[numb_p_t].className.replace('period-b','');
                            day_list[numb_p_t].className = day_list[numb_p_t].className.replace('period-e','');
                        }
                        for(var j = this._i+1; j < numb_p_b; j++){
                            if(day_list[j].className.indexOf('period-c') == -1) day_list[j].className += ' period-c';
                        }
                        if(numb_p_b != day_list.length){
                            day_list[numb_p_b].className = day_list[numb_p_b].className.replace('period-b','');
                            day_list[numb_p_b].className = day_list[numb_p_b].className.replace('period-e','');
                            if(day_list[numb_p_b].className.indexOf('period-e') == -1) day_list[numb_p_b].className += ' period-e';
                        }
                        numb_p_t = this._i;
                        if(this.className.indexOf('period-b') == -1) this.className += ' period-b';
                        info_text.html(this._day + ' ' + i_month_name[this._month] + ' - ' + period_b._day + ' ' + i_month_name[period_b._month]);
                    }
                    else if(period_b != this){
                        if(numb_p_t != -1){
                            for(var j = numb_p_b+1; j < numb_p_t; j++){
                                day_list[j].className = day_list[j].className.replace('period-c', '');
                            }
                            for(var j = numb_p_t; j < this._i; j++){
                                day_list[j].className = day_list[j].className.replace('period-c', '');
                            }
                            day_list[numb_p_t].className = day_list[numb_p_t].className.replace('period-b','');
                            day_list[numb_p_t].className = day_list[numb_p_t].className.replace('period-e','');
                        }
                        if(numb_p_b == day_list.length){
                            for(var j = 0; j < this._i; j++){
                                if(day_list[j].className.indexOf('period-c') == -1) day_list[j].className += ' period-c';
                            }
                        }
                        else{
                            for(var j = numb_p_b+1; j < this._i; j++){
                                if(day_list[j].className.indexOf('period-c') == -1) day_list[j].className += ' period-c';
                            }
                        }
                        if(numb_p_b != day_list.length){
                            day_list[numb_p_b].className = day_list[numb_p_b].className.replace('period-b','');
                            day_list[numb_p_b].className = day_list[numb_p_b].className.replace('period-e','');
                            if(day_list[numb_p_b].className.indexOf('period-b') == -1) day_list[numb_p_b].className += ' period-b';
                        }
                        numb_p_t = this._i;
                        if(this.className.indexOf('period-e') == -1) this.className += ' period-e';
                        info_text.html(period_b._day + ' ' + i_month_name[period_b._month] + ' - ' + this._day +' ' + i_month_name[this._month]);
                    }
                }
            }
            day_list[i].onmouseout = function(){
                this.className = this.className.replace('hover','');
            }
            day_list[i].onclick = function(){
                if(period_b != -1 && period_e != -1){
                    if(this._i < numb_p_b){
                        period_b.className = period_b.className.replace('period-b', 'period-c');
                        for(var j = this._i+1; j < numb_p_b; j++){
                            if(day_list[j].className.indexOf('period-c') == -1) day_list[j].className += ' period-c';
                        }
                        if(this.className.indexOf('period-b') == -1) this.className += ' period-b';
                        period_b = this;
                        numb_p_b = this._i;
                        info_text.html(period_b._day + ' ' + i_month_name[period_b._month] + ' - ' + period_e._day + ' ' + i_month_name[period_e._month]);
                    }
                    else if(this._i > numb_p_e){
                        period_e.className = period_e.className.replace('period-e', 'period-c');
                        for(var j = numb_p_e+1; j < this._i; j++){
                            if(day_list[j].className.indexOf('period-c') == -1) day_list[j].className += ' period-c';
                        }
                        if(this.className.indexOf('period-e') == -1) this.className += ' period-e';
                        period_e = this;
                        numb_p_e = this._i;
                        info_text.html(period_b._day + ' ' + i_month_name[period_b._month] + ' - ' + period_e._day + ' ' + i_month_name[period_e._month]);
                    }
                    else if(this._i > numb_p_b && this._i < numb_p_e){
                        if(this._i - numb_p_b < (numb_p_e - numb_p_b)/2){
                            period_b.className = period_b.className.replace('period-b', '');
                            for(var j = numb_p_b+1; j <= this._i; j++){
                                day_list[j].className = day_list[j].className.replace('period-c', '');
                            }
                            if(this.className.indexOf('period-b') == -1) this.className += ' period-b';
                            period_b = this;
                            numb_p_b = this._i;
                            info_text.html(period_b._day + ' ' + i_month_name[period_b._month] + ' - ' + period_e._day + ' ' + i_month_name[period_e._month]);
                        }
                        else{
                            period_e.className = period_e.className.replace('period-e', '');
                            for(var j = this._i; j < numb_p_e; j++){
                                day_list[j].className = day_list[j].className.replace('period-c', '');
                            }
                            if(this.className.indexOf('period-e') == -1) this.className += ' period-e';
                            period_e = this;
                            numb_p_e = this._i;
                            info_text.html(period_b._day + ' ' + i_month_name[period_b._month] + ' - ' + period_e._day + ' ' + i_month_name[period_e._month]);
                        }
                    }
                }
                else{
                    if(period_b == -1){
                        period_b = this;
                        numb_p_b = this._i;
                        info_text.html(period_b._day + ' ' + i_month_name[period_b._month]);	
                    }
                    else{
                        if((this._year < period_b._year) || (this._year == period_b._year && this._month < period_b._month) || (this._year == period_b._year && this._month == period_b._month && this._i < numb_p_b)){
                            period_e = period_b;
                            numb_p_e = numb_p_b;
                            if(period_e.className.indexOf('period-e') == -1) period_e.className += ' period-e';
                            for(var j = this._i+1; j < numb_p_e; j++){
                                if(day_list[j].className.indexOf('period-c') == -1) day_list[j].className += ' period-c';
                            }
                            if(this.className.indexOf('period-b') == -1) this.className += ' period-b';
                            period_b = this;
                            numb_p_b = this._i;
                            info_text.html(period_b._day + ' ' + i_month_name[period_b._month] + ' - ' + period_e._day + ' ' + i_month_name[period_e._month]);
                        }
                        else if(period_b != this){
                            if(numb_p_b != period_b._i) numb_p_b = -1;
                            period_e = this;
                            numb_p_e = this._i;
                            if(period_e.className.indexOf('period-e') == -1) period_e.className += ' period-e';
                            for(var j = numb_p_b + 1; j < numb_p_e; j++){
                                if(day_list[j].className.indexOf('period-c') == -1) day_list[j].className += ' period-c';
                            }
                            if(period_b.className.indexOf('period-b') == -1) period_b.className += ' period-b';
                            info_text.html(period_b._day + ' ' + i_month_name[period_b._month] + ' - ' + period_e._day + ' ' + i_month_name[period_e._month]);
                        }
                    }
                }
            }
            day_list[i].onmousedown = function(){
                if(this == period_b){
                    _f2 = true;
                    return false;
                }
                else if(this == period_e){
                    _f3 = true;
                }
                else if(this._i < numb_p_b || this._i > numb_p_e){
                    numb_p_t = this._i;
                    _f4 = true;
                }
                return false;
            }
            day_list[i].onmouseup = function(){
                _f2 = false;
                _f3 = false;
                _f4 = false;
                numb_p_t = -1;
            }
            day_list[i].onselectstart = function(){
                return false;
            }
        }
        if(period_b.offsetTop && -period_b.offsetTop + period_b.offsetHeight*2 <= 0) 
            _hold.find('div.calendar > div.scroll-content').css('top', -period_b.offsetTop + period_b.offsetHeight*2)
        else 
            _hold.find('div.calendar > div.scroll-content').css('top', 0);
        _hold.find('div.calendar').customScrollV();
    }
    /*--- auto scroll ---*/
    var t_top = 0;
    var t_scroll;
    var scroll_timer;
    _hold.find('div.calendar > div').unbind('mouseenter').mouseenter(function(){
        t_top = $(this).parent().offset().top;
        t_scroll = this;
        if(scroll_timer) clearInterval(scroll_timer);
    }).unbind('mouseleave').mouseleave(function(e){
        if(scroll_timer) clearInterval(scroll_timer);
        if(_f1 || _f2 || _f3 || _f4){
            scroll_timer = setInterval(function(){
                if(e.pageY - t_top < 22 && t_scroll.offsetTop != 0){
                    var n_top = -t_scroll.offsetTop - 22;
                    if(n_top <= 22) n_top = 0;
                    else n_top = Math.floor(n_top/22)*22;
                    t_scroll.style.top = - n_top + 'px';
                    _hold.find('div.calendar').customScrollV();
                }
                else if(t_top + t_scroll.parentNode.offsetHeight < e.pageY + 22){
                    var n_top = -t_scroll.offsetTop + 22;
                    n_top = Math.ceil(n_top/22)*22;
                    if(t_scroll.offsetHeight - n_top <= t_scroll.parentNode.offsetHeight) n_top = t_scroll.offsetHeight - t_scroll.parentNode.offsetHeight;
                    if(n_top < 0) n_top = 0;
                    t_scroll.style.top = - n_top + 'px';
                    _hold.find('div.calendar').customScrollV();
                }
            }, 200);
        }
    });
    document.onmouseup = function(){
        _f1 = false;
        _f2 = false;
        _f3 = false;
        _f4 = false;
        if(scroll_timer) clearInterval(scroll_timer);
    }
    /*--- buttons events ---*/
    btn_prev.unbind('click').click(function(){
        _date.setFullYear(_date.getFullYear() - 2);
        info_year.html(_date.getFullYear());
        curr_date = new Date();
        if(_date.getFullYear() == curr_date.getFullYear()){
            btn_prev.hide();
            _date = curr_date;
            initCurrent();
        }
        else{
            initMore();
        }
        return false;
    }).unbind('mousedown').mousedown(function(){
        return false;
    });
    btn_next.unbind('click').click(function(){
        btn_prev.show();
        if (dt1 &&  (_date.getFullYear() == parseInt(info_year.html())))
            _date.setFullYear(parseInt(info_year.html())+1);
        info_year.html(_date.getFullYear());
        initMore();
        return false;
    }).unbind('mousedown').mousedown(function(){
        return false;
    });
    btn_clear.unbind('click').click(function(){
        date1.val('');
        date2.val('');
        if(a_day != -1) a_day.className = a_day.className.replace('active-day','');
        if(period_b != -2 && period_e != -2 && period_b != -1 && period_e != -1){
            period_b.className = period_b.className.replace('period-b','');
            period_e.className = period_e.className.replace('period-e','');
        }
        for(var i = 0; i < day_list.length; i++){
            day_list[i].className = day_list[i].className.replace('period-c','');
            day_list[i].className = day_list[i].className.replace('period-b','');
            day_list[i].className = day_list[i].className.replace('period-e','');
        }
        info_text.html('');
        a_day = -1;
        period_b = -1;
        period_e = -1;
        numb_p_b = -1;
        numb_p_e = -1;
        return false;
    });
    btn_search.unbind('click').click(function(){
        if(a_day != -1) 
            date1.val(a_day._day + ' ' + (a_day._month+1) + ' ' + a_day._year);
        if(period_b != -2 && period_e != -2 && period_b != -1 && period_e != -1){
            date1.val(period_b._day + ' ' + (period_b._month+1) + ' ' + period_b._year);
            date2.val(period_e._day + ' ' + (period_e._month+1) + ' ' + period_e._year);
        }
        _hold.parents('form')[0].submit();
        return false;
    });
    // pgn
    if (dt1 && (dt1.getFullYear() > curr_date.getFullYear())){
        _date = new Date();
        _date.setTime(dt1.valueOf());
        _date.setDate(1);
        _date.setMonth(0);
        btn_prev.show();
        info_year.html(_date.getFullYear());
        initMore();
    } else {
        initCurrent();
    }
}

$(document).ready(function(){
    //$('div.calendar-hold').each(function(){
        //initCalendar($(this));
    //});
    var _set = $('.set');
    _set.find('a.set').each(function(){
        var _href = $(this).attr('href');
        if(_href) {
            if($(this).hasClass('active')) {
                $(_href).show();
                initCalendar($(_href).find('div.calendar-hold'));
            }else{
                $(_href).hide();
            }
        }
    });
    _set.find('a.set').click(function(){
        _set.find('a.set').removeClass('active');
        $(this).addClass('active');
        _set.find('a.set').each(function(){
            var _href = $(this).attr('href');
            if(_href) {
                if($(this).hasClass('active')) {
                    $(_href).show();
                    initCalendar($(_href).find('div.calendar-hold'));
                }else{
                    $(_href).hide();
                }
            }
        });
        return false;
    });
});
