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

        this.date = (new Date()).cutTime();
        this.today = (new Date()).cutTime();
        this.StartDate = this.today;
        this.period = [this.today,null]
        this.maxFlightOptions = maxFlightOptions;
        this.type = calendarType;
        
        this.DateTime = new Date( this.date );

        this.PrevMonth = $(".PrevMonth");
        this.PrevMonth.click(function(){self.prevMonth()});

        this.NextMonth = $(".NextMonth");
        this.NextMonth.click(function(){self.nextMonth()});

        this.Month = $(".Month");
        this.Year = $(".Year");
        
        this.NextMonth1 = $(".NextMonth1");
        this.NextMonth1.click(function(){self.nextMonth()});
        this.NextMonth2 = $(".NextMonth2");
        this.NextMonth2.click(function(){self.nextMonth2()});

        this.PrevMonth1 = $(".PrevMonth1");
        this.PrevMonth1.click(function(){self.prevMonth2()});
        
        this.PrevMonth2 = $(".PrevMonth2");
        this.PrevMonth2.click(function(){self.prevMonth()});
        
        
        this.CalendarBody = $("#CalendarBody");
        this.CalendarGrid = $(".CalendarGrid");
        
        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(){
        DirectionBalloon.hide();
        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(){
        DirectionBalloon.hide();
        var self = this;
        this.date.setDate(1);
        this.date = new Date(this.date.setMonth(this.date.getMonth()+2));        
        this.draw();
    }
    
    CalendarObj.prototype.prevMonth = function(){
        DirectionBalloon.hide();
        var self = this;
        this.date.setDate(1);
        this.date = new Date(this.date.setMonth(this.date.getMonth()-1));
        this.draw();
    }
    CalendarObj.prototype.prevMonth2 = function(){
        DirectionBalloon.hide();
        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.html(this.options.monthesNames[this.date.getMonth()]);
        this.Year.html(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.html(this.options.monthesNames[PrevM1.getMonth()]);
        this.PrevMonth2.html(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.html(this.options.monthesNames[NextM1.getMonth()]);
        this.NextMonth2.html(this.options.monthesNames[NextM2.getMonth()]);
}
    CalendarObj.prototype.drawCalendarGrid = function(){
        var self = this;
        //var rows = this.CalendarGrid.childNodes;
        var rows = this.CalendarGrid.children();
        
        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 */

