/* Date DOM Class */
    function DayClass(cell, date, Calendar){
        var self = this;
        this.Calendar = Calendar;
        this.cell = cell;
        this.today = self.Calendar.today;
        this.date = new Date(date);
        this.strDate = this.date.toAWADString();
        this.cell.setAttribute("date",this.strDate);
        this.inCurrentMonth = true;
        this.maxFlightOptions = self.Calendar.maxFlightOptions;
        this.type = self.Calendar.type;
        
        this.div_Date = this.cell.firstChild;
        this.div_DateNumber = $(this.div_Date).find(".DateNumber")[0];
        this.div_Directions = $(this.div_Date).find(".Directions")[0];
        this.div_DateIcons = $(this.div_Date).find(".DateIcons")[0];
        
        this.LeftDirection = null;
        this.RightDirection = null;            
        
        this.cell.active = false;
        this.cell.onclick = function(){ self.cellOnClick(); };
        this.Directions = [];
        
        this.draw();
    }
    DayClass.prototype.checkDirections = function(){
        var self = this;
        this.Directions = [];
        for(seg in Directions){
            if(Directions[seg].Date == self.strDate){
                self.Directions.push(Directions[seg]);
            }
        }
    }
    DayClass.prototype.draw = function(){
        this.cell.className = "Day";
        
        this.dropIcons();
        this.dropDirections();        
        
        this.drawCell();
        this.drawDate();        
        //this.drawPlanes();
        this.drawDirections();
    }
    
    DayClass.prototype.drawCell = function(){
        var self = this;

        var bCanChoose = true;
        var ADayToday = new Date(self.today);
        var A8DayAfterToday = new Date(self.today);
        var MaxDayAfterToday = new Date(self.today);
        
        A8DayAfterToday.setDate(ADayToday.getDate() + 8);
        MaxDayAfterToday.setDate(ADayToday.getDate() + maxDays);
        
        if(( this.date.valueOf() < ADayToday.valueOf() ) || ( this.date.valueOf() > MaxDayAfterToday.valueOf() )){
            bCanChoose = false;
        } else {
            for(var i = 1; i < Directions.length; i++){
                if (Directions[i]) {
                    var sDate = Directions[i].Date;
                    var arrDate = sDate.split('.');
                    var oDate = new Date(arrDate[2],arrDate[1]-1,arrDate[0]);
                    if( this.date.valueOf() < oDate.valueOf() )
                    {
                        bCanChoose = false;
                    }
                }
            }
        }
        
        if( !bCanChoose ) {
            $(this.cell).addClass("cantChoose");
        } else if(!window.XMLHttpRequest){
            this.cell.onmouseover = function(){ $(self.cell).addClass("hover") };
            this.cell.onmouseout = function(){ $(self.cell).removeClass("hover"); };
        }
        if(this.Directions.length > 0){
            $(this.cell).addClass("hasDirections");
        }
        
        var SelfDate = new Date( self.date );

        for(var i = 1; i < Directions.length; i++){
            if (Directions[i]) {
                var sLeftDate = Directions[i].Date;
                var arrLeftDate = sLeftDate.split('.');
                var oLeftDate = new Date(arrLeftDate[2],arrLeftDate[1]-1,arrLeftDate[0]);
            
                if( SelfDate > oLeftDate )
                {
                    // выделение дат между полетами
                    var oRightDate = null;
                    if( i + 1 <= Directions.length-1 ) {
                        var sRightDate = Directions[i+1].Date;
                        var arrRightDate = sRightDate.split('.');
                        oRightDate = new Date(arrRightDate[2],arrRightDate[1]-1,arrRightDate[0]);
                    
                        if( SelfDate < oRightDate ){
                            $(self.cell).addClass("betweenDirections");
                        }
                    }
                }
            }
        }
        
        
        // +8 days
        //console.log(Directions.length);
        if( (this.date.valueOf() == A8DayAfterToday.valueOf()) && (Directions.length <= 1)){
            var cookie      = ReadCookie('Directions_' + cur_domain),
            	DatesCookie = ReadCookie('Dates_' + cur_domain);
            if ((cookie == 'null' || !cookie) && (typeof(ReSave) == 'undefined' || !ReSave) && (typeof(out_search) == 'undefined' || !out_search)) {
                this.cell.active = true;
                $(this.cell).addClass("active");            
                DirectionBalloon.show(self);
            } else if (typeof out_search != 'undefined' && !out_search && typeof out_search_params != 'undefined' && !out_search_params) {
            	DirectionBalloon.hide();
            } else if(DatesCookie != null){
            	var AllDates = DatesCookie.split(';');
                if (Date.parseAWADString(AllDates[0]) < this.today.getTime()) {       	
                	DirectionBalloon.show(self);
                } else {
                	DirectionBalloon.hide();
                }
            }  else{
                DirectionBalloon.hide();
            }
        }

    }
    
    DayClass.prototype.dropIcons = function(){
        if (this.div_DateIcons.firstChild) {
            for(var ch=0; this.div_DateIcons.childNodes.length>0;ch++){
                this.div_DateIcons.removeChild(this.div_DateIcons.firstChild);
            }
        }
    }
    DayClass.prototype.dropDirections = function(){
        if (this.div_Directions.firstChild) {
            for(var ch=0; this.div_Directions.childNodes.length>0;ch++){
                this.div_Directions.removeChild(this.div_Directions.firstChild);
            }
        }
    }
    
    DayClass.prototype.drawDate = function(){
        
        while( this.div_DateNumber.hasChildNodes() )
        {    
            this.div_DateNumber.removeChild(this.div_DateNumber.lastChild);
        }
        
        
        if( this.date.valueOf() < this.today.valueOf() ){
            $(this.cell).addClass("beforeToday");
        }
        if( this.date.valueOf() == this.today.valueOf() ){
            $(this.cell).addClass("today");
        }
        if(this.inCurrentMonth == false){
            $(this.cell).addClass("otherMonth");
        } else {
            $(this.cell).removeClass("otherMonth");
        }
        if(this.Directions[0]){
            $(this.cell).addClass("Direction"+this.Directions[0].DirectionNumber);
        }
        if(this.Directions[1]){
            $(this.cell).addClass("Direction"+this.Directions[1].DirectionNumber+"second");
        }
        
        if( this.date.valueOf() == this.today.valueOf() ){
            if( !$(this.cell).hasClass("hasDirections") )
            {
                var dayToday = document.createElement("span");
                dayToday.className += " dayToday";
                dayToday.appendChild( document.createTextNode( MO_str_Today + ' ' ) );
                this.div_DateNumber.appendChild( dayToday );
            }
        }
        var span_DateNumber = document.createElement("span");
        span_DateNumber.className = "DateNumber";        
        span_DateNumber.appendChild( document.createTextNode( this.date.getDate()) );
        this.div_DateNumber.appendChild( span_DateNumber );
        
        if( this.date.valueOf() >= this.today.valueOf() ) { //not beforeToday
            var titleDate = this.date.getDate() + '\u00A0' + defLeng.monthesNamesGenitive[this.date.getMonth()].toLowerCase();
            var CellTitle = MO_str_SetDeparture + ' ' + titleDate;
            this.cell.title = CellTitle;
        } else this.cell.title = '';
    }
    
    // рисуем стрелочки в ячейке
    DayClass.prototype.drawDirections = function(){
        var self = this;
        
        for(var seg=0, length=this.Directions.length; seg<length; seg++){
            if(seg>1){
                self.div_Directions.appendChild( document.createTextNode("…") );
                break;
            }
            CurrentDirection = self.Directions[seg];
            
            if (CurrentDirection.StartAirpCode) {
            
                var Airp = document.createElement("span");
                if(self.type == 'rail'){
                    Airp.appendChild( document.createTextNode(CurrentDirection.StartAirpStr.substr(0, 3).toUpperCase()));
                } else {
                    Airp.appendChild( document.createTextNode(CurrentDirection.StartAirpCode));
                }
                
                Airp.title = CurrentDirection.StartAirpStr;
                self.div_Directions.appendChild(Airp);
            
                var Arrow = document.createElement("span");
                Arrow.className = "arrow arrowDirection"+CurrentDirection.DirectionNumber;
                Arrow.appendChild( document.createTextNode("\u2192") );
            
                var br = document.createElement("br");
                br.className = "clear-fix";
            
                self.div_Directions.appendChild(Arrow);
            
            /*
            if( seg==1 || !self.Directions[seg+1] || self.Directions[seg].EndAirpCode != self.Directions[seg+1].StartAirpCode ){
                var EndAirp = document.createElement("span");
                EndAirp.appendChild( document.createTextNode(CurrentDirection.EndAirpCode) );
                EndAirp.title = CurrentDirection.EndAirpStr;
                self.div_Directions.appendChild(EndAirp);
                if( self.Directions[seg+1] ){
                    self.div_Directions.appendChild( document.createTextNode("…") );
                    break;
                }
            }
            */
                var EndAirp = document.createElement("span");
                if(self.type == 'rail'){
                    EndAirp.appendChild( document.createTextNode(CurrentDirection.EndAirpStr.substr(0, 3).toUpperCase()));
                } else {
                    EndAirp.appendChild( document.createTextNode(CurrentDirection.EndAirpCode));
                }
                EndAirp.title = CurrentDirection.EndAirpStr;
                self.div_Directions.appendChild(EndAirp);
            
            
                self.div_Directions.appendChild(br);
            }
        }
    }
    DayClass.prototype.update = function(date){ 
        // проверить нужны ли в этой функции внутренные вызовы.
        // этот апдейт вызывается и при просисовке погоды в том числе, убрал отсюда функцию скрытия балуна и вставил сюда: CalendarObj.prototype.drawCalendarGrid ибо нефиг скрывать балун 35 раз =)
        this.date = date || this.date;
        this.strDate = this.date.toAWADString();
        this.cell.setAttribute("date",this.strDate);
        this.dropActive();
        this.checkDirections();
        this.draw();
    }
    DayClass.prototype.cellOnClick = function(){
        if($(this.cell).hasClass("cantChoose")) {
            return;
        }
      
        var self = this;
        self.setActive();
        
        if( Directions.length <= self.maxFlightOptions) {
            var LastDirection = DirectionsDOM[DirectionsDOM.length-1];
            if(LastDirection.Default == true && LastDirection.StartAirpCodeInput.value != "" && LastDirection.EndAirpCodeInput.value != "" ){
                LastDirection.DateInput.value = self.strDate;
                LastDirection.update();
            } else {
                DirectionBalloon.show(self);
            }
        }
    }
    DayClass.prototype.setActive = function(){
        this.dropActive();
        this.cell.active = true;
        $(this.cell).addClass("active");
    }
    DayClass.prototype.dropActive = function(){
    //ccc
        for(day in Calendar.Days){
            if( Calendar.Days[day].cell.active == true ) // peter
            {
                Calendar.Days[day].cell.active = false;
                $(Calendar.Days[day].cell).removeClass("active");
            }
        }
    }
/* /Date DOM Class */
