var DirectionsDOM = [];

/* Direction DOM Class */
    function DirectionDOMClass(Direction){
        
        this.Direction = Direction || null;
        this.DirectionNumber = Direction ? Direction.DirectionNumber : null;
        
        this.entBox = null;
        this.entObj = null;
        
        this.Default = Direction ? false : true;
        
        this.DateInput = null;
        this.StartAirpInput = null;
        this.StartAirpCodeInput = null;
        this.EndAirpInput = null;
        this.EndAirpCodeInput = null;
        this.PlusMinusInput = null;
        
        if(Direction){
            this.create();
        }
        
    }
   
    DirectionDOMClass.prototype.createFromDOM = function(DirectionDOMObj){
        var self = this;
        
        
        
        this.entBox = DirectionDOMObj;
        this.entObj = this.entBox.getElementsByTagName("fieldset")[0];
        this.DirectionNumber = this.entObj.id.split("Direction")[1];
        
        this.DateInput = document.getElementById("Date"+this.DirectionNumber+"_input");
        
        this.DateLabel = document.getElementById("CalendarLink"+this.DirectionNumber);
        
        $(this.DateInput).focus(function(){ DirectionBalloon.hide(); })
        
        this.StartAirpInput = document.getElementById("StartAirp"+this.DirectionNumber+"_input");
        $(this.StartAirpInput).focus(function(){ DirectionBalloon.hide(); })
        this.StartAirpInput.Field = new FieldClass(this.StartAirpInput);
        this.StartAirpCodeInput = document.getElementById("StartAirp"+this.DirectionNumber+"Code_input");
        
        

        this.DateInput.onfocus = function(){
            var el_Label = $("label[for=" + self.DateInput.id + "]");
            Datepicker.show(self.DateInput,el_Label[0],self);
        }

        this.PlusMinusInput = document.getElementById("PlusMinus"+this.DirectionNumber+"_input");
        this.PlusMinusLabel = $(this.entObj).find('span[class="PlusMinusLabel"]')[0];
        

        this.Prev = $(this.entObj).find(".prev")[0];
        $(this.Prev).click(function(){
            if(!self.DateInput.value) return;
            var diapazon = self.PlusMinusInput.value * 1;
            if (diapazon > 1){
                diapazon--;        
                self.PlusMinusInput.value = diapazon;
                self.PlusMinusInput.style.display = 'inline';
                self.PlusMinusLabel.innerHTML = MO_range+': +/-';
            } else {
                self.PlusMinusInput.value = '';
                self.PlusMinusInput.style.display = 'none';
                self.PlusMinusLabel.innerHTML = MO_precise_date;
            }
            self.updateDirection();
            
        });
        
        this.Next = $(this.entObj).find(".next")[0];
        $(this.Next).click(function(){
            if(!self.DateInput.value) return;
            var diapazon = self.PlusMinusInput.value * 1;
            if (diapazon < 3){
                diapazon++;
                self.PlusMinusInput.value = diapazon;
                self.PlusMinusInput.style.display = 'inline';
                self.PlusMinusLabel.innerHTML = MO_range+': +/-';
            }
            self.updateDirection();
        });
        

        this.EndAirpInput = document.getElementById("EndAirp"+this.DirectionNumber+"_input");
        $(this.EndAirpInput).focus(function(){ DirectionBalloon.hide(); })
        this.EndAirpInput.Field = new FieldClass(this.EndAirpInput);
        this.EndAirpCodeInput = document.getElementById("EndAirp"+this.DirectionNumber+"Code_input");


        this.StartAirpCodeInput.onfocus = function(){ self.StartAirpInput.focus() }
        this.StartAirpCodeInput.parentNode.onclick = function(){ self.StartAirpInput.focus() }
        this.EndAirpCodeInput.onfocus = function(){ self.EndAirpInput.focus() }
        this.EndAirpCodeInput.parentNode.onclick = function(){ self.EndAirpInput.focus() }
        
        this.StartAirpInput.onfocus = function(){
            var number = parseInt(self.DirectionNumber)-1;
            if( Directions.length > 0 && Directions[ number ] != null ){
                if( self.StartAirpCodeInput.value == "" && Directions[ number ].EndAirpCode != '' ) {
                    self.StartAirpInput.value = Directions[ number ].EndAirp;
                    self.StartAirpInput.title = Directions[ number ].EndAirpStr;
                    self.StartAirpCodeInput.value = Directions[ number ].EndAirpCode;
                    self.StartAirpCodeInput.title = Directions[ number ].EndAirpStr;
                    self.StartAirpInput.Field.Update();
                    self.StartAirpInput.suggest.update();
                }
            }
        }
        
        this.EndAirpInput.onfocus = function(){
            var number = parseInt(self.DirectionNumber)+1;
            if( Directions.length > 0 && Directions[number ] != null ){
                if( self.EndAirpCodeInput.value == "" && Directions[ number ].StartAirpCode != '') {
                    self.EndAirpInput.value = Directions[ number ].StartAirp;
                    self.EndAirpInput.title = Directions[ number ].StartAirpStr;
                    self.EndAirpCodeInput.value = Directions[ number ].StartAirpCode;
                    self.EndAirpCodeInput.title = Directions[ number ].StartAirpStr;
                    self.EndAirpInput.Field.Update();
                    self.EndAirpInput.suggest.update();
                }
            }
        }

        this.StartAirpInput.suggest = new Suggest(this.StartAirpInput, self);
        this.EndAirpInput.suggest = new Suggest(this.EndAirpInput, self);
        
        
        
        this.ReturnRoute = $(this.entObj).find(".ReturnRoute a")[0];
        this.ReturnRoute.onclick = function(){ self.AddReturnRoute(); }

        this.PlusMinusInput.value = '';
        this.PlusMinusInput.style.display = 'none';
        this.PlusMinusLabel.innerHTML = MO_precise_date;
        
        DirectionsDOM[this.DirectionNumber] = this;
    }
    
    DirectionDOMClass.prototype.AddReturnRoute = function() {
        var self = this;
        
        var number = parseInt(this.DirectionNumber, 10) - 1;
        if (Directions.length > 0 && Directions[number] != null) {
            if (Directions[number].EndAirpCode != '') {
                this.StartAirpInput.value = Directions[number].EndAirp;
                this.StartAirpInput.title = Directions[number].EndAirpStr;
                this.StartAirpCodeInput.value = Directions[number].EndAirpCode;

                this.StartAirpInput.Field.Update();
                this.StartAirpInput.suggest.update();
            }
            if (Directions[number].StartAirpCode != '') {
                this.EndAirpInput.value = Directions[number].StartAirp;
                this.EndAirpInput.title = Directions[number].StartAirpStr;
                this.EndAirpCodeInput.value = Directions[number].StartAirpCode;

                this.EndAirpInput.Field.Update();
                this.EndAirpInput.suggest.update();
            }
            
            this.PlusMinusInput.value = '';
            this.PlusMinusInput.style.display = 'none';
            this.PlusMinusLabel.innerHTML = MO_precise_date;
            
        }
        setTimeout(function() {
            self.checkDefault();
        }, 10);
    };
    
    DirectionDOMClass.prototype.create = function(){
        
        var Directions = document.getElementById("Directions");
        var Direction_Prototype = document.getElementById("Direction_Prototype");
        var TempDirection = Direction_Prototype.firstChild.cloneNode(true);
        TempDirection
        var TempDirection1 = Direction_Prototype.firstChild.cloneNode(true);
        Direction_Prototype.removeChild(Direction_Prototype.firstChild);
        
        this.DirectionNumber = 0;

        var newDirectionNumber = DirectionsDOM.length;
        
        var newBox = Directions.appendChild(document.createElement('div'));
        newBox.className = "Direction default";
        
        

        newBox.appendChild(TempDirection);

        newBox.id = "Direction"+this.DirectionNumber+"_col";

        newBox.style.display = 'none';

        this.updateDirectionNumber(newDirectionNumber);
        
        Direction_Prototype.appendChild(TempDirection1);
        
        this.createFromDOM(newBox);
        
    }
    DirectionDOMClass.prototype.update = function(Direction){
        if(Direction){
            this.Direction = Direction;
            
            this.updateDirectionNumber();
            this.updateDate();
            this.updateStartAirp();
            this.updateEndAirp();
        }
        this.draw();
        
        this.checkDefault();
        this.updateDirection();
    }
    DirectionDOMClass.prototype.updateDirectionNumber = function(DirectionNumber){
        if( !this.Direction || this.DirectionNumber != this.Direction.DirectionNumber){
            var newDirectionNumber = DirectionNumber || (this.DirectionNumber - 1);
            document.getElementById("Direction"+this.DirectionNumber+"_col").id         = "Direction"+newDirectionNumber+"_col";
            
            document.getElementById("Direction"+this.DirectionNumber).id                 = "Direction"+newDirectionNumber;
            document.getElementById("CalendarLink"+this.DirectionNumber).htmlFor         = "Date"+newDirectionNumber+"_input";
            document.getElementById("CalendarLink"+this.DirectionNumber).id                = "CalendarLink"+newDirectionNumber;
            document.getElementById("Date"+this.DirectionNumber+"_input").name            = "Date"+newDirectionNumber;
            document.getElementById("Date"+this.DirectionNumber+"_input").id             = "Date"+newDirectionNumber+"_input";
            document.getElementById("StartAirp"+this.DirectionNumber+"_label").htmlFor     = "StartAirp"+newDirectionNumber+"_input";
            document.getElementById("StartAirp"+this.DirectionNumber+"_label").id         = "StartAirp"+newDirectionNumber+"_label";
            document.getElementById("StartAirp"+this.DirectionNumber+"_input").name     = "StartAirp"+newDirectionNumber;
            document.getElementById("StartAirp"+this.DirectionNumber+"_input").id         = "StartAirp"+newDirectionNumber+"_input";
            document.getElementById("StartAirp"+this.DirectionNumber+"Code_input").name = "StartAirp"+newDirectionNumber+"Code";
            document.getElementById("StartAirp"+this.DirectionNumber+"Code_input").id     = "StartAirp"+newDirectionNumber+"Code_input";
            document.getElementById("EndAirp"+this.DirectionNumber+"_label").htmlFor     = "EndAirp"+newDirectionNumber+"_input";
            document.getElementById("EndAirp"+this.DirectionNumber+"_label").id             = "EndAirp"+newDirectionNumber+"_label"
            document.getElementById("EndAirp"+this.DirectionNumber+"_input").name         = "EndAirp"+newDirectionNumber;
            document.getElementById("EndAirp"+this.DirectionNumber+"_input").id         = "EndAirp"+newDirectionNumber+"_input";
            document.getElementById("EndAirp"+this.DirectionNumber+"Code_input").name     = "EndAirp"+newDirectionNumber+"Code";
            document.getElementById("EndAirp"+this.DirectionNumber+"Code_input").id     = "EndAirp"+newDirectionNumber+"Code_input";
            
            document.getElementById("PlusMinus"+this.DirectionNumber+"_input").id     = "PlusMinus"+newDirectionNumber+"_input";
            
            this.DirectionNumber = newDirectionNumber;
        }
    }
    
    DirectionDOMClass.prototype.updateDate = function(){

        this.DateInput.value = this.Direction.Date;
        this.PlusMinusInput.value = this.Direction.PlusMinus;
    }

    DirectionDOMClass.prototype.updateStartAirp = function(){
        this.StartAirpInput.value = this.Direction.StartAirp;
        this.StartAirpInput.title = this.Direction.StartAirpStr;
        this.StartAirpInput.Field.Update();
        this.StartAirpCodeInput.value = this.Direction.StartAirpCode;
        this.StartAirpCodeInput.title = this.Direction.StartAirpStr;
        this.StartAirpInput.suggest.update();
    }
    DirectionDOMClass.prototype.updateEndAirp = function(){
        this.EndAirpInput.value = this.Direction.EndAirp;
        this.EndAirpInput.title = this.Direction.EndAirpStr;
        this.EndAirpInput.Field.Update();
        this.EndAirpCodeInput.value = this.Direction.EndAirpCode;
        this.EndAirpCodeInput.title = this.Direction.EndAirpStr;
        this.EndAirpInput.suggest.update();
    }
    DirectionDOMClass.prototype.draw = function(){
        if(this.DateInput.value!='')
            this.drawDateLabel();
        
        this.drawStartAirp();
        this.drawEndAirp();
    }
    DirectionDOMClass.prototype.drawDateLabel = function(){
        var self = this;
        var tempDate = this.DateInput.value;
        var arrDate = tempDate.split('.');
        var el_Label = $("label[for="+self.DateInput.id+"]")[0];
        var span_Text = $(el_Label).find('span[class="Text"]')[0];
        var span_Date = $(el_Label).find('span[class="Date"]')[0];
        
        var span_Label = $(el_Label).find('span[class="PlusMinusLabel"]')[0];
        
        span_Text.style.display = "none";
        span_Date.style.display = "block";
        var lDate = $(span_Date).find('span[class="lDate"]')[0];
            lDate.innerHTML = parseInt(arrDate[0],10);
        var lMonth = $(span_Date).find('span[class="lMonth"]')[0];
            lMonth.innerHTML = Datepicker.getMonthDName(arrDate[1]);
        var lYear = $(span_Date).find('span[class="lYear"]')[0];
        lYear.innerHTML = arrDate[2];
        Datepicker.date = Date.parseAWADString(tempDate);
        
        var diapazon = this.PlusMinusInput.value;
        if (diapazon > 0){
            this.PlusMinusInput.value = diapazon;
            this.PlusMinusInput.style.display = "inline";
            span_Label.innerHTML = MO_range+': +/-';
            
        } else {
            this.PlusMinusInput.value = '';
            this.PlusMinusInput.style.display = "none";
            span_Label.innerHTML = MO_precise_date;
        }
        span_Label.style.display = "inline";
        
    }
    DirectionDOMClass.prototype.drawStartAirp = function(){
        this.dropStartAirpCodeValue();
    }
    DirectionDOMClass.prototype.drawEndAirp = function(){
        this.dropEndAirpCodeValue();
    }
    DirectionDOMClass.prototype.dropStartAirpCodeValue = function(){
        if( this.StartAirpInput.value == "" )
        {
            this.StartAirpInput.title = '';
            this.StartAirpCodeInput.value = '';
            this.StartAirpCodeInput.title = '';
        }
    }
    DirectionDOMClass.prototype.dropEndAirpCodeValue = function(){
        if( this.EndAirpInput.value == "" )
        {
            this.EndAirpInput.title = '';
            this.EndAirpCodeInput.value = '';
            this.EndAirpCodeInput.title = '';
        }
    }
    DirectionDOMClass.prototype.checkDefault = function(){
        var self = this;
        if( this.Default == true ){
            if( this.DateInput.value=='' && this.StartAirpCodeInput.value!='' && this.EndAirpCodeInput.value!='' ){
                this.DateInput.focus();
            }
            else if( this.DateInput.value!='' && this.StartAirpCodeInput.value!='' && this.EndAirpCodeInput.value!='' ){
                if( this.Direction == null ){
                    this.createSegment();
                }
                this.setNotDefault();
                if(DirectionsDOM.length < 5){ 
                    var DirectionDOMObj = new DirectionDOMClass();
                    DirectionDOMObj.create();
                }
            }
        }
        
        var number = this.DirectionNumber * 1;
        if (number < 5) {
            var id = "Direction" + number + "_col";
            var direction = document.getElementById(id);
            direction.style.display = 'block';
            
        }        
        if (number < 4) {
            this.Add = $(this.entObj).find(".AddRoute")[0];
            this.Add.style.display = 'inline';
            
            this.Add.onclick = function(){ self.AddDirection(); }
        }
        
        
    }
            
    DirectionDOMClass.prototype.setNotDefault = function(){
        var self = this;
        this.Default = false;
        $(this.entBox).removeClass("default");
        this.Close = $(this.entObj).find(".Close")[0];
        this.Close.onclick = function(){ self.DeleteDirection(); }

    }

    DirectionDOMClass.prototype.AddDirection = function(){
        var self = this;
        var number = this.DirectionNumber * 1 + 1;
        if (number < 5) {
            var id = "Direction" + number + "_col";
            var direction = document.getElementById(id);
            direction.style.display = 'block';
        }
        
    }
    
    
    DirectionDOMClass.prototype.DeleteDirection = function(){
        var self = this;
        
        this.Direction.Delete();
        var Directions = document.getElementById("Directions");
        Directions.removeChild(this.entBox);

        DirectionsDOM.splice(this.DirectionNumber, 1);    
        for(seg in DirectionsDOM){
            DirectionsDOM[seg].updateDirectionNumber();
        }
        if(DirectionsDOM.length == 4 && DirectionsDOM[3].Default!=true){
            var DirectionDOMObj = new DirectionDOMClass();
            DirectionDOMObj.create();
        }
        Calendar.draw();

        document.getElementById("Direction"+this.DirectionNumber+"_col").style.display = 'block';
    }
    DirectionDOMClass.prototype.createSegment = function(){
        this.Direction = new DirectionClass(this.DirectionNumber, this.DateInput.value, this.StartAirpInput.value, this.EndAirpInput.value, this.StartAirpCodeInput.value, this.EndAirpCodeInput.value, this.StartAirpInput.title, this.EndAirpInput.title, this.PlusMinusInput.value );
    }
    DirectionDOMClass.prototype.updateDirection = function(){
        if(this.Direction){
            this.Direction.DirectionNumber = this.DirectionNumber;
            this.Direction.Date = this.DateInput.value;
            this.Direction.StartAirp = this.StartAirpInput.value;
            this.Direction.EndAirp = this.EndAirpInput.value;
            this.Direction.StartAirpCode = this.StartAirpCodeInput.value;
            this.Direction.EndAirpCode = this.EndAirpCodeInput.value;
            this.Direction.StartAirpStr = this.StartAirpInput.title;
            this.Direction.EndAirpStr = this.EndAirpInput.title;
            this.Direction.PlusMinus = this.PlusMinusInput.value;
            Calendar.draw();
        }
    }
/* /Direction DOM Class */
