/* Calendar Class */
    function CalendarObj(){
        var self = this;

        this.date = (new Date()).cutTime();
        this.today = (new Date()).cutTime();
        this.StartDate = this.today;
        this.period = [this.today,null]
        
        this.DateTime = new Date( this.date );
        
        this.entObj = document.getElementById("Calendar");
        this.CalendarHeader = $(this.entObj).find(".CalendarHeader")[0];
        
        this.PrevMonth = $(this.CalendarHeader).find(".PrevMonth")[0];
        this.PrevMonth.onclick = function(){self.prevMonth()};
        
        this.NextMonth = $(this.CalendarHeader).find(".NextMonth")[0];
        this.NextMonth.onclick = function(){self.nextMonth()};
        
        this.Month = $(this.CalendarHeader).find(".Month")[0];
        this.Year = $(this.CalendarHeader).find(".Year")[0];
        
        this.NextMonth1 = $(this.CalendarHeader).find(".NextMonth1")[0];
        this.NextMonth1.onclick = function(){self.nextMonth()};
        this.NextMonth2 = $(this.CalendarHeader).find(".NextMonth2")[0];
        this.NextMonth2.onclick = function(){self.nextMonth2()};

        this.PrevMonth1 = $(this.CalendarHeader).find(".PrevMonth1")[0];
        this.PrevMonth1.onclick = function(){self.prevMonth2()};
        
        this.PrevMonth2 = $(this.CalendarHeader).find(".PrevMonth2")[0];
        this.PrevMonth2.onclick = function(){self.prevMonth()};
        
        
        this.CalendarBody = $(this.entObj).find(".CalendarBody")[0];
        this.CalendarGrid = $(this.CalendarBody).find(".CalendarGrid")[0];
        
        this.Days = [];
        this.options = {};
        
        defLeng = {
            lang:                    'ru-ru',
            monthesNames:            ["Январь", "Февраль", "Март", "Апрель", "Май", "Июнь", "Июль", "Август", "Сентябрь", "Октябрь", "Ноябрь", "Декабрь"],
            monthesNamesGenitive:    ["Января", "Февраля", "Марта", "Апреля", "Мая", "Июня", "Июля", "Августa", "Сентября", "Октября", "Ноября", "Декабря"],
            monthesNamesShort:        ["Янв", "Фев", "Мар", "Апр", "Май", "Июн", "Июл", "Авг", "Сен", "Окт", "Ноя", "Дек"],
            daysNames:                ["Воскресенье", "Понедельник", "Вторник", "Среда", "Четверг", "Пятница", "Суббота"],
            daysNamesShort:            ["Вс", "Пн", "Вт", "Ср", "Чт", "Пт", "Сб"],
            weekend:                [0, 6],
            weekstart:                1
        };

/////////////////////////////
        if( language == 'en') {
            defLeng = {
                lang:                    'en-en',
                monthesNames:            ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
                monthesNamesGenitive:    ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
                monthesNamesShort:        ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sen", "Oct", "Nov", "Dec"],
                daysNames:                ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
                daysNamesShort:            ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
                weekend:                [0, 6],
                weekstart:                1
            };
        }

        if( language == 'uk') {
            defLeng = {
                lang:                    'uk-uk',
                monthesNames:            ["Січень", "Лютий", "Березень", "Квітень", "Травень", "Червень", "Липень", "Серпень", "Вересень", "Жовтень", "Листопад", "Грудень"],
                monthesNamesGenitive:    ["Січня", "Лютого", "Березня", "Квітня", "Травня", "Червня", "Липня", "Серпня", "Вересня", "Жовтня", "Листопада", "Грудня"],
                monthesNamesShort:        ["Січ", "Лют", "Бер", "Кві", "Тра", "Чер", "Лип", "Сер", "Вер", "Жов", "Лис", "Гру"],
                daysNames:                ["Неділя", "Понеділок", "Вівторок", "Середа", "Четвер", "П`ятниця'", "Субота"],
                daysNamesShort:            ["Нд", "Пн", "Вт", "Ср", "Чт", "Пт'", "Сб"],
                weekend:                [0, 6],
                weekstart:                1
            };
        }

/////////////////////////////

    for (lg in defLeng)
            this.options[lg] = defLeng[lg];
        
        this.isCreated = false;
        this.create();
    }
    CalendarObj.prototype.create = function() {
        this.draw();
        this.isCreated = true;
    }
    CalendarObj.prototype.nextMonth = function(){
        var self = this;
        this.date.setDate(1); //fix:при переходе с 31 на 30 добавление месяца осуществляется на +2
        this.date = new Date(this.date.setMonth(this.date.getMonth()+1));
        this.draw();
    }
    CalendarObj.prototype.nextMonth2 = function(){
        var self = this;
        this.date.setDate(1);
        this.date = new Date(this.date.setMonth(this.date.getMonth()+2));        
        this.draw();
    }
    
    CalendarObj.prototype.prevMonth = function(){
        var self = this;
        this.date.setDate(1);
        this.date = new Date(this.date.setMonth(this.date.getMonth()-1));
        this.draw();
    }
    CalendarObj.prototype.prevMonth2 = function(){
        var self = this;
        this.date.setDate(1);
        this.date = new Date(this.date.setMonth(this.date.getMonth()-2));
        this.draw();
    }
    
    CalendarObj.prototype.draw = function(){
        this.drawCalendarHeader();
        this.drawCalendarGrid();
    }
    CalendarObj.prototype.drawCalendarHeader = function(){
        this.Month.innerHTML =  this.options.monthesNames[this.date.getMonth()];
        this.Year.innerHTML = this.date.getFullYear();
        
        var PrevM1 = new Date( this.date );
        PrevM1.setDate(1);
        PrevM1.setMonth( this.date.getMonth() - 2 );
        var PrevM2 = new Date( this.date );
        PrevM2.setDate(1);
        PrevM2.setMonth( this.date.getMonth() - 1 );
        this.PrevMonth1.innerHTML = this.options.monthesNames[PrevM1.getMonth()];
        this.PrevMonth2.innerHTML = this.options.monthesNames[PrevM2.getMonth()];

        var NextM1 = new Date( this.date );
        NextM1.setDate(1);
        NextM1.setMonth( this.date.getMonth() + 1 );
        var NextM2 = new Date( this.date );
        NextM2.setDate(1);
        NextM2.setMonth( this.date.getMonth() + 2 );
        this.NextMonth1.innerHTML = this.options.monthesNames[NextM1.getMonth()];
        this.NextMonth2.innerHTML = this.options.monthesNames[NextM2.getMonth()];
}
    CalendarObj.prototype.drawCalendarGrid = function(){
        DirectionBalloon.hide();
        var self = this;
        var rows = this.CalendarGrid.childNodes;
        
        var tempDate = new Date(this.date);
        if( (tempDate.getFullYear() == this.StartDate.getFullYear()) && (tempDate.getMonth() == this.StartDate.getMonth()) ){

        } else {
            tempDate.setDate(1);
        }
        
        var day1 = (tempDate.getDay() - this.options.weekstart) % 7;
        if(day1 < 0) day1 += 7;
        tempDate.setDate(tempDate.getDate() - day1);
        
        var DayCounter = 0;
        for(var weekNumber=0; weekNumber < 5; weekNumber++){
            var row = rows[weekNumber];
            var cells = row.childNodes;
            for( var i=0; i<7; i++ )
            {
                if( self.isCreated == false )
                {
                    var cell = cells[i];
                    var DayObj = new DayClass( cell, tempDate, self );                    
                    self.Days.push(DayObj);                    
                    if( tempDate.getMonth() != self.date.getMonth() )
                    {
                        DayObj.inCurrentMonth = false;
                        DayObj.draw();
                    }
                } else {
                    var DayObj = self.Days[DayCounter];                    
                    if( tempDate.getMonth() != self.date.getMonth() )
                    {
                        DayObj.inCurrentMonth = false;
                    } 
                    else
                    {
                        DayObj.inCurrentMonth = true;
                    }                    
                    DayObj.update(tempDate);
                    DayCounter++; 
                }
                tempDate.setDate(tempDate.getDate() + 1);
            }
        }
        
    }
/* /Calendar Class */
