var DestinationListControl = Class.create();
DestinationListControl.prototype = {  initialize: function(callback, frequency) {
      this.callback = callback;
      this.frequency = frequency;
      this.currentlyExecuting = false;
      this.selecteditems=Array();
      this.tbody=null;
      this.target=null;
      this.headers=Array();
      this.aptlist=Array();
      this.maxr=0;
      this.aptcodes=new Object();
      this.input=null;
      this.regids=Array();
      this.lastselection=null
      this.selection=false;
      this.renderfilter=null;
      this.selecteddst=null;
      this.styleType='profi';
      this.aptbycode=Array();
      this.regionsid=Array();
  },
  init:function()
  {
    this.target=document.getElementById('dsttablediv');
  },
  write:function()
  {
    this.target.innerHTML='<table id="dstaptlist" cellspacing="0" cellpadding="0" border="0"><tbody><tr></tr></tbody></table>';
  },
  setinput:function(input)
  {
    this.input=input;
  },
  addheader:function(aheader)
  {
    this.headers.push(aheader);
  },
  addapt:function(r,code,apt,rid)
  {
    if(!this.aptlist[r])
      this.aptlist[r]=Array();
    this.aptlist[r].push(Array(code.toUpperCase(),apt,rid));
    this.aptbycode[code.toUpperCase()]=apt;
  },
  mouseover:function(sender)
  {
    if ((!sender.isenabled) || sender.selected)  return false;
    this.setstate(sender,1);

  },
  selectitem:function(sender)
  {
    if (!sender.isenabled)  return false;
    alert(sender.innerHTML);
    if ((obj=document.getElementById('desCode')).style.display=='')
      try{obj.focus();}catch(e){};
  },
  mouseout:function(sender)
  {
    if (!sender.isenabled || sender.selected) return false;
    this.setstate(sender,0);
  },

  setstate:function(sender,state)
  {
    sender.style.color='';
    sender.style.fontWeight='';
    if (state==0){
      if (sender.type==0)
      {
        sender.className='region_row destflight_clicked';
        sender.style.backgroundColor='';
      }
      else
      {
        sender.className='city_row color0bg color12';
      }
        sender.style.backgroundImage="";
    }
    else if (state==1){
    }
    else    {
      sender.style.backgroundImage="url("+iberooturl+"img/trActiveState.gif)";
      sender.style.backgroundColor='';
    }

  },

  render:function()
  {
    this.noselection=true;
    this.tbody=document.getElementById('dstaptlist').tBodies[0];
    this.tbody.style.display='none';
    while(this.tbody.rows.length>0)
      this.tbody.deleteRow(0);
    this._oMethod = typeof(desArrDesc) == 'undefined';
    var dur = document.getElementById('tripduration_search').value;
    this.__type=this._oMethod?0:desArrDesc[(document.searchtypeRadio||ep3_getSearchType()||'PA')+'_'+dur];
    this.__ntype=this._oMethod?0:desArrDesc[''+(document.searchtypeRadio||ep3_getSearchType()||'PA')];
    var l= this._oMethod ? document.desregions.length : desArr.length;

    for (var i=this._oMethod?1:0;i<l;i++)
    {
      this.regrender((this.regids[i]=this.tbody.insertRow(this.tbody.rows.length)).insertCell(0),i);
      
      if(!this._oMethod && ((desArr[i][2] & this.__ntype) != this.__ntype))
        this.regids[i].style.display = 'none';
    }
    this.tbody.style.display='';
   document.dstlistpopup.fixframe();
  },
  
  regrender:function(target,id)
  {
    if(imagespath)
    {
      this.imgBase = imagespath;
    }
    else
      this.imgBase = '';
    
    
      target.innerHTML='<div style="margin-left:10px;margin-right:10px"><img border="0" alt="-->" '+imgsrc('listarrowr')+'/> '+(this._oMethod?document.desregions[id][1]: desArr[id][0])+'</div>';

    target.isenabled=true;
    target.type=0;
    target.cnum=id;
    this.setstate(target,0);
    this.regionsid[id]=target;
    if (this.renderfilter) return ;
    target.control=this;
    target.regid=id;
    target.onclick=function(){this.control.renderselected(this.regid)};
    target.onmouseover=function(){this.control.mouseover(this)};
    target.onmouseout=function(){this.control.mouseout(this)};
  },
  
  deleteselection:function(id)
  {
    while ((this.regids[id+1] && (this.regids[id+1].rowIndex-1>this.regids[id].rowIndex)) || ((!this.regids[id+1]) && ((this.regids[id].rowIndex+1)<this.tbody.rows.length)))
    {
      this.tbody.deleteRow(this.regids[id].rowIndex+1);
    }
     this.lastselection=null;
  },
  
  renderselected:function(id)
  {
    if ((obj=document.getElementById('desCode')).style.display=='')
      try{obj.focus();}catch(e){};
    var delonly=this.lastselection===id;
    if (this.lastselection !== null)
    {
      this.picttoggle(this.regionsid[this.lastselection],false);
      this.deleteselection(this.lastselection);
      if (delonly) {
        document.dstlistpopup.fixframe();
        return;
      }
    }
    this.regexpand(id);
    this.picttoggle(this.regionsid[id],true);
    document.dstlistpopup.fixframe();
    try{
    //ep3_getdep();
      ess_loadDur();
    }catch(ex){}
  },
  
  regexpand:function(id)
  {
    if(!this._oMethod)
    {
      //set value for country
      document.getElementById('dstbox').innerHTML=desArr[id][1][0][1];//country
      document.getElementById('desCode').value = desArr[id][1][0][0];
    }
    this.lastselection=id;
    var l=this._oMethod?document.desregions[id][2].length :desArr[id][1].length;
    this.subsellen=l-1;
    var i2=0;
    for (var i=this._oMethod?1:0;i<l;i++)
    {
      if ((!this.renderfilter) || 
            (this._oMethod 
                 ?(document.descodes[document.desregions[id][2][i]][0].substring(0,this.renderflen)==this.renderfilter)
                 :(desArr[id][1][i][0].substring(0,this.renderflen)==this.renderfilter)
            )
        )
      {
        if(this._oMethod || desArr[id][1][i][2] & this.__ntype == this.__ntype)
        {
          i2++;
          this.dstrender(this.tbody.insertRow((this.regids[id].rowIndex+i2)),id,this._oMethod?document.desregions[id][2][i]:i);
        }
      }
    }
    return i2;
  },

  dstrender:function(target,reg,id)
  {
    /// TR, country_id, this._oMethod ? document.descodes : region_id
    var dst=this._oMethod?document.descodes[id]:desArr[reg][1][id];
    ctarget=target.insertCell(0);
    target.cnum=id;
    target.code=dst[0];
    target.type=1;
    target.isenabled=true;
    target.control=this;
    target.dstname=dst[1];
    this.selecteddst=id;
    if (this.renderfilter)
    {
      ctarget.innerHTML='<div>[<B style="color:red">'+this.renderfilter+'</B>'+dst[0].substring(this.renderflen,3)+'] '+dst[1]+(this.renderflen<3?' ('+(this._oMethod?document.desregions[reg][1]:desArr[reg][0])+')':'')+'</div>';

    }
    else
      {
        if(this.styleType=='ibe')
        { 
          ctarget.innerHTML='<div style="margin-left:15px"><img border="0" alt="-->" '+imgsrc('listarrow')+'/><span class="destpanel_link"> '+dst[1]+'</span></div>';
        }
        else
          ctarget.innerHTML='<div style="margin-left:20px">'+dst[1]+'</div>';
      }
    target.onmouseover=function(){this.control.mouseover(this)};
    target.onmouseout=function(){this.control.mouseout(this)};
    target.onclick=function(){this.control.selectapt(this)};
    this.setstate(target,0);
  },
  selectapt:function(apt)
  {


    document.getElementById('dstbox').innerHTML=apt?apt.dstname:AnyText;
    if (!apt)
    {
      ess_loadDur();
      return false;
    }
    var input=document.getElementById('desCode');
    input.value=apt.code;
    this.checkinput();
    this.lastselection=null;
    document.dstlistpopup.hide();
    try{
      //ep3_getdep();
      ess_loadDur();
    }catch(ex){}
    
  },
  selectaptbycode:function(code)
  {
    var code=code.toUpperCase();
    var apt=new Object;
    var aptlist=document.desscut[code.substring(0,1)];
      for (i in aptlist)
        for (var i2=0;i2<aptlist[i].length;i2++)
          if (document.descodes[[aptlist[i][i2]]][0]==code)
          {
            apt.code=code;
            apt.dstname=document.descodes[[aptlist[i][i2]]][1];
            this.selectapt(apt);
            return true;
          }
     this.selectapt(null);
  },
  getaptcell:function (r,n)
  {
    if (this.tbody.rows.length-(r>3?this.maxr-2:0)<=(n+2))
    {
      row=this.tbody.insertRow(this.tbody.rows.length-(r>3?0:1));
      for (var i=0;i<4;i++)
        row.insertCell(i);
      var res=row.cells[r%4];
    }
    else
      res=this.tbody.rows[n+(r>3?this.maxr:1)].cells[r%4];
    if ((r<4) && (this.maxr<this.tbody.rows.length))
      this.maxr=this.tbody.rows.length;
      return res;
   },

  addselected:function(item)
  {
    if (item.selected) return;
    this.selecteditems.push(item);
    item.selected=true;
    this.setstate(item,2);

  },
  
  removeselected:function(item)
  {
      if (!item.selected) return false;
      var i=this.selecteditems.length;
      while ((i>=0) && (this.selecteditems[i]!=item))i--;
      if (i<0) return false;
      this.selecteditems.splice(i,1);
      item.selected=false;
      this.setstate(item,0);
  },
  
  rebuildvals:function()
  {
   var values=this.input.value.toUpperCase().split(',');
   for (var i=values.length-1;i>=0;i--)
      if  ((values[i]=='') || (found=this.aptcodes[values[i]]) && (!found.selected))
      {
        values.splice(i,1);
        i--;
      }
    for (var s=this.selecteditems.length-1;s>=0;s--)
    {
      var found=false;
      for (var i=values.length-1;i>=0;i--)
        if (values[i]==this.selecteditems[s].code)
        {
          found=true;
          break;
        }
      if (!found)
        values.push(this.selecteditems[s].code);
    }

    this.input.value=values.join(',');
   if ((obj=document.getElementById('depCode')).style.display=='')
    obj.setSelectionRange(document.getElementById('depCode').value.length,document.getElementById('depCode').value.length);
  },
  
  setpopupstate:function(visible)
  {
    this.popupactive=visible;
  },
  
  checkinput:function()
  {
    this.input=document.getElementById('desCode');
    this.renderflen=0;
    this.renderfilter=this.input.value.toUpperCase();
    
      if (this.renderflen==3)
          document.deplist.setaptlist(this.selecteddst);
      if (!this.popupactive) document.dstlistpopup.hide();
      this.renderfilter=null;
      this.render();
  },

  picttoggle:function(target,open)
  {
     if(open==true)
     {
     	setimgonobj(target.firstChild.firstChild,'listarrowd');
     }
     if(open==false)
     	setimgonobj(target.firstChild.firstChild,'listarrowr');
  },

  checkapt:function()
  {
      if (document.getElementById('desCode').value.length==3)
          document.deplist.setaptlist(this.selecteddst)

  }
}
