// (C) 2009, lastadm, mailto:lastadm@mail.ru

function init_calendar(calendar_url, data_url, data_div)
{
NMonths = new Array("января", "февраля", "марта", "апреля", "мая",
                    "июня", "июля", "августа", "сентября",
                    "октября", "ноября", "декабря");


        if($('calendar_frame') == null || $(data_div) == null)
                return false;

        var now = new Date();
        year =now.getFullYear();
        month =now.getMonth();
        day =now.getDate();

        $("this_day").innerHTML=day+"&nbsp;"+NMonths[month]+"&nbsp;"+year;
        load_url(calendar_url+'?month=' + month + '&year=' + year, 'calendar', init_date_links);
        
        controls=$$('div.date_selector');
        controls.each(function(el){
                var opened=false;
                el.onclick = function(){
                        if(this.id)
                        {
                                this.addClassName('clicked_selector');
                                setTimeout("$('"+this.id+"').removeClassName('clicked_selector')",300);
                                switch(this.id)
                                {
                                        case 'prev_year':
                                                year--;
                                                break;
                                        case 'prev_month':
                                                month--;
                                                if(month < 0)
                                                {
                                                        year--;
                                                        month=11;
                                                }
                                                break;
                                        case 'set_today':
                                                now = new Date();
                                                year =now.getFullYear();
                                                month =now.getMonth();
                                                day =now.getDate();
                                                break;
                                        case 'next_month':
                                                month++;
                                                if(month > 11)
                                                {
                                                        month=0;
                                                        year++;
                                                }
                                                break;
                                        case 'next_year':
                                                year++;
                                                break;
                                }
                                $("this_day").innerHTML=day+"&nbsp;"+NMonths[month]+"&nbsp;"+year;
                                load_url(calendar_url+'?month=' + month + '&year=' + year, 'calendar', init_date_links);
                        }
                        if($(data_div))
                                load_url(data_url+'?day='+day+"&month="+month + '&year=' + year, data_div, calendar_postloader);
                        return false;
                }
                el.onmouseover = function(){
                        this.addClassName('active_date_selector');
                        return false;
                }
                el.onmouseout = function(){
                        this.removeClassName('active_date_selector');
                        return false;
                }
        });

        function init_date_links()
        {
                days=$$('div.has_data');
                days.each(function(el){
                        var opened=false;
                        el.onclick = function(){
                                load_url(data_url+'?day='+this.innerHTML+"&month="+month + '&year=' + year, data_div, calendar_postloader);
                                $("this_day").innerHTML=this.innerHTML+"&nbsp;"+NMonths[month]+"&nbsp;"+year;
                                return false;
                        }
                        el.onmouseover = function(){
                                this.addClassName('selected_date');
                                return false;
                        }
                        el.onmouseout = function(){
                                this.removeClassName('selected_date');
                                return false;
                        }
                });

        }

        function calendar_postloader()
        {
                init_maillinks();
                init_infolinks();
                init_interlaced();

                if($$('div.rounded'))
                        Nifty("div.rounded");
                if($$('div.top-rounded'))
                        Nifty("div.top-rounded","top");
                if($$('div.bottom-rounded'))
                        Nifty("div.bottom-rounded","bottom");
        }
}

function xinit_date_links(data_url, data_div)
{
        days=$$('div.has_data');
        window.alert(days);
        days.each(function(el){
                var opened=false;
                el.onclick = function(){
                    window.alert(data_div);
                        load_url(data_url+'?day='+this.innerHTML+"&month="+month + '&year=' + year, data_div, init_interlaced);
                        $("this_day").innerHTML=this.innerHTML+"&nbsp;"+NMonths[month]+"&nbsp;"+year;
                        return false;
                }
                el.onmouseover = function(){
                        this.addClassName('selected_date');
                        return false;
                }
                el.onmouseout = function(){
                        this.removeClassName('selected_date');
                        return false;
                }
        });

}
