function takeoff_dates(takeoff_arr) {  
  this.takeoff_arr = takeoff_arr;
}

takeoff_dates.prototype.set = function(mode, vilet_date, vozvrat_date) {
  this.mode = mode;
    
  this.vilet_date = vilet_date;  
  this.vozvrat_date = vozvrat_date;
}

takeoff_dates.prototype.get = function() {
  return this.result_arr;
}

takeoff_dates.prototype.init = function() {
  
  if(this.mode == 'vilet'){    
//    this.init_vozvrat();
    this.result_arr = this.takeoff_arr;
  }
  
  if(this.mode == 'vozvrat'){
    this.vozvrat();
  }
}

takeoff_dates.prototype.init_vozvrat = function() {
  if(this.vozvrat_date == null || this.vozvrat_date == ''){
    var j=0;    
    if(this.vilet_date !== null){
      this.result_arr = new Array();
      _month_vilet = this.vilet_date.getMonth()+1;
     
      _vilet_date = this.vilet_date.getDate()+'.'+_month_vilet+'.'+this.vilet_date.getFullYear();
      
      for (i = 0; i < this.takeoff_arr.length; i++) {
        _date = this.takeoff_arr[i][1]+'.'+this.takeoff_arr[i][0]+'.'+this.takeoff_arr[i][2];    
        if (_vilet_date == _date) {      
          j = i+1;
        }      
      }
    }
        
    this.set_vozvrat(this.takeoff_arr[j]);
    $( "#vozvrat" ).removeAttr('disabled');
  }  
}

takeoff_dates.prototype.vilet = function() {
  var j=0;
  if(this.vozvrat_date !== null){
    this.result_arr = new Array();
    _month = this.vozvrat_date.getMonth()+1;
    _vozvrat_date = this.vozvrat_date.getDate()+'.'+_month+'.'+this.vozvrat_date.getFullYear();
    
    for (i = 0; i < this.takeoff_arr.length; i++) {
      _date = this.takeoff_arr[i][1]+'.'+this.takeoff_arr[i][0]+'.'+this.takeoff_arr[i][2];    
      if (_vozvrat_date == _date) {      
        j = i;
      }
    }  
  }
  
  if(j > 0) this.result_arr = this.takeoff_arr.slice(0, j);
  else this.result_arr = this.takeoff_arr;  
}

takeoff_dates.prototype.correct = function() {
  var j=0;
  var vilet_pos=0;  
  var vozvrat_pos=0;  
  
  if(this.vozvrat_date !== null){
    this.result_arr = new Array();
    _month_vilet = this.vilet_date.getMonth()+1;
    _month_vozvrat = this.vozvrat_date.getMonth()+1;
    
    _vilet_date = this.vilet_date.getDate()+'.'+_month_vilet+'.'+this.vilet_date.getFullYear();
    _vozvrat_date = this.vozvrat_date.getDate()+'.'+_month_vozvrat+'.'+this.vozvrat_date.getFullYear();
    
    for (i = 0; i < this.takeoff_arr.length; i++) {
      _date = this.takeoff_arr[i][1]+'.'+this.takeoff_arr[i][0]+'.'+this.takeoff_arr[i][2];    
      if (_vozvrat_date == _date) {      
        j = i;
        vozvrat_pos = i;
      }

      if (_vilet_date == _date) {
        vilet_pos = i;
      }
    }  
  }
  
  if(vilet_pos == (this.takeoff_arr.length-1)){
    $( "#vozvrat" ).val('');
    
  }
  
  if(vozvrat_pos < vilet_pos){
    if(this.takeoff_arr[vilet_pos+1]){
      this.set_vozvrat(this.takeoff_arr[vilet_pos+1]);
    }
    else{
      $( "#vozvrat" ).val('');
    }
  }  
  
  if($( "#vozvrat" ).val() == null || $( "#vozvrat" ).val() == ''){
    $( "#vozvrat" ).attr({'disabled':'disabled'});
    this.by_popup($( "#vozvrat" ));
  }
  
}

takeoff_dates.prototype.vozvrat = function() {
  var j=0;    
  if(this.vilet_date !== null){
    this.result_arr = new Array();
    _month_vilet = this.vilet_date.getMonth()+1;
   
    _vilet_date = this.vilet_date.getDate()+'.'+_month_vilet+'.'+this.vilet_date.getFullYear();    
    for (i = 0; i < this.takeoff_arr.length; i++) {
      _date = this.takeoff_arr[i][1]+'.'+this.takeoff_arr[i][0]+'.'+this.takeoff_arr[i][2];    
      if (_vilet_date == _date) {      
        j = i+1;
      }      
    }
  }        

  if(j > 0) this.result_arr = this.takeoff_arr.slice(j);
  else this.result_arr = this.takeoff_arr;
}

takeoff_dates.prototype.set_vozvrat = function(offset_date) {
  _currect_date = new Date(offset_date[2], offset_date[0]-1, offset_date[1]);      
  _month_currect_date = _currect_date.getMonth()+1;
  _day_currect_date = _currect_date.getDate();
  
  if(_month_currect_date < 10) _month_currect_date = '0'+_month_currect_date;
  if(_day_currect_date < 10) _day_currect_date = '0'+_day_currect_date;
  
  var currect_date = _day_currect_date+'.'+_month_currect_date+'.'+_currect_date.getFullYear();
  $( "#vozvrat" ).val(currect_date);
}

takeoff_dates.prototype.by_popup = function(obj) {
  var winWidth = $("body").width();
  
  $(".close, .close-popup").live("click", function(event) {
    event.preventDefault();
  });
  $(".close, .close-popup").live("click", function() {
    $(".popupchik").remove();
  });

  $("*:not(.popupchik)").click(function(e) {
    
    kids = e.target;
    kidClass = $(kids).hasClass('ui-state-default');
    if (kidClass) return false;
    
    var _a = kids;
    while (true) {
    _atag = _a.tagName;
    if (_atag && ($(_a).hasClass('popupchik'))) {
      break;
    }
    else {
      if (!_atag) {
      $(".popupchik").remove();
      break;
      }
      _a = _a.parentNode;
    }
    }
  });
    //selector = $(this).parent();  
  var offset = obj.offset();
  pageX = offset.left;
  pageY = offset.top;
//  alert(pageX);
  $("body").append('<div class="popupchik by_popup"><a href="#" class="close-popup">close</a></div>');
  $(".popupchik").css({
    right: (winWidth - pageX) - 245,
    top: pageY + 25
  });
    
  var selector = $(".popupchik");
  $.post('/booking.php',{mode:'popupchik'},  function(popup_text){
    $(selector).prepend(popup_text);  
  });
  
//  setTimeout(function(){
//    $(".by_popup").animate({"opacity":0}, 200).remove;
//    }, 3000);
}

