var DepartureListControl = Class.create();
DepartureListControl.prototype = {  initialize: function(callback, frequency) {
      this.callback = callback;
      this.frequency = frequency;
      this.currentlyExecuting = false;
      this.listcontrols=Array();
      this.selecteditems=Array();
      this.tbody=null;
      this.target=null;
      this.headers=Array();
      this.maxitems=0;
      this.aptlist=Array();
      this.maxr=0;
      this.defaultvalues=Array();
      this.aptcodes=new Object();
      this.input=null;
      this.aptids=Array();
      this.lastaptlist=-1;
  },
  init:function()
  {
    document.writeln('<div id="apttablediv"></div>');
    this.target=document.getElementById('apttablediv');
  },

  setmaxitems:function(num)
  {
    this.maxitems=num;
  },

  write:function()
  {
    this.target.innerHTML='<table id="depaptlist" 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] = new Array();
    this.aptlist[r].push(Array(code.toUpperCase(),apt,rid));
  },
  
  mouseover:function(sender)
  {
    if ((!sender.isenabled) || sender.selected)  return false;
    this.setstate(sender,1);
  },
  
  selectitem :function(sender)
  {
    if (!sender.isenabled ) return false;
    if ( sender.selected )
    {
      this.removeselected(sender);
    }
    else
    {
      if ((this.maxitems!=0) && (this.selecteditems.length<this.maxitems))
        this.addselected(sender);
    }
    this.rebuildvals();
  },
  
  mouseout:function(sender)
  {
    if (!sender.isenabled || sender.selected) return false;
      this.setstate(sender,0);
  },

  setstate:function(sender,state)
  {
    //sender.style.color=(sender.valid?'':'red');
    sender.isenabled=(sender.valid?true:false);
    if (state==0){
      sender.style.backgroundImage="";
      sender.childNodes[0].childNodes[1].checked=false;
    }
    else if (state==1){
      //sender.style.backgroundImage="url(img/bs_tc.png)";
      sender.style.backgroundColor='';
    }
    else if (state==2)   {
      sender.childNodes[0].childNodes[1].checked=true;
      //sender.style.backgroundImage="url(/img/trActiveState.gif)";
      sender.style.backgroundImage="url("+iberooturl+"img/trActiveState.gif)";
      sender.style.backgroundColor='';
    }
    else if (state==3) {
      sender.style.backgroundImage="";
      sender.style.backgroundColor='black';
    }
    sender.className=(sender.valid?'flightpanel_label_enabled':'flightpanel_label_disabled');
  },

  renderapt:function(target,apt)
  { 
    this.aptcodes[apt[0]]=target;
    this.aptids[apt[2]]=target;
    target.valid=true;
    target.aptname=apt[1];
    //target.innerHTML='<div>&nbsp;&nbsp;<input type="checkbox" class="flightpanel_checkbox" readonly="true"></input>&nbsp;&nbsp;'+apt[1]+'</div>';
    target.innerHTML='<div>&nbsp;&nbsp;<input type="checkbox" class="flightpanel_checkbox" readonly="true" />&nbsp;&nbsp;'+apt[1]+'</div>';
    target.code=apt[0];
    target.aptlist=this;
    target.onmouseover=function(){this.aptlist.mouseover(this)};
    target.onmouseout=function(){this.aptlist.mouseout(this)};
    target.onclick=function(){this.aptlist.selectitem(this)};
    target.isenabled=true;
    target.selected=false;
//    target.style.margin='2px';
    target.style.width='133px';
    if (this.defaultvalues.toString().indexOf(target.code)>-1)
    {  
      this.addselected(target);
    }     
    this.mouseout(target);
  },

  renderhdr:function(target,hdr)
  {
    target.innerHTML=hdr;
    target.style.backgroundColor='#9C9C9C';
    target.style.color='#ffffff';
    target.style.fontWeight='bold';
  },
  
  render:function()
  { 
    this.tbody=document.getElementById('depaptlist').tBodies[0];
    while(this.tbody.rows.length>0)
      this.tbody.deleteRow(0);
    for (var i=0;i<2;i++)
    {
      row=this.tbody.insertRow(this.tbody.rows.length);
      for (var i2=0;i2<4;i2++)
      {
        var ne=document.createElement('th');
        //ne.style.backgroundColor = 'rgb(237, 245, 251)'; 
        ne.className = "color1bg color10";
     	  row.appendChild(ne);
        ne.innerHTML=this.headers[(i*4)+i2];
      }
    }
    this.maxr=0;
    for (var i=0 ; i<this.aptlist.length; i++)
      for (var i2=0 ; i2<this.aptlist[i].length; i2++)
      {   
        this.renderapt(this.getaptcell(i,i2),this.aptlist[i][i2]);
      }
  },
  
  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);
        row.className = "color10bg ";
      }
      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()
  {
    if (!this.input) return;
   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) || (!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(',');
    var dptName=Array();
    for (key in values)
    {
       dptName.push(this.aptcodes[values[key]].aptname);
    }
    document.getElementById('flightpanel_hin_text').innerHTML=dptName.join(',');
    document.getElementById('depCode').value=values.join(',');
//    var obj;
//    if ((obj=document.getElementById('dl_depCode')) && (obj.setSelectionRange))
//      obj.setSelectionRange(obj.value.length,obj.value.length);
  },

  checkinput:function()
  {
    var values=this.input.value.toUpperCase().split(',');
    var apt;
    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)
        this.removeselected(this.selecteditems[s])
    }

    for (var i=values.length-1;i>=0;i--)
    {
      if ((this.aptcodes[values[i]]) && (!(apt=this.aptcodes[values[i]]).selected))
        this.addselected(apt);
    }
  },
  
  setaptlist:function(dstid)
  {
    if (dstid==this.lastaptlist) return true
    this.lastaptlist=dstid;
    var l=this.aptids.length;
    var apt;
    for (var i=0;i<l;i++)
    if (apt=this.aptids[i])
      apt.valid=(dstid==-1) || false;
    if (dstid!=-1)
    {
      var aptlist=document.deptodes[document.descodes[dstid][0]];
      if (aptlist)
      {
        var l=aptlist.length;
        var apt;
        for (var i=0;i<l;i++)
        if (apt=this.aptids[aptlist[i]])
        {
          apt.valid=true;
        }
      }
    }
    this.checkapts();
//    setaptlist(this.selecteddst)
  },
  
  checkapts:function(apt)
  {
    for (var i=this.aptids.length-1;i>=0;i--)
    if (apt=this.aptids[i])
    {
      if (apt.selected)
        this.setstate(apt,2);
      else
        this.setstate(apt,0);
    }
  },
  
  setdefaultvalues:function(text)
  {
    this.defaultvalues=text.split(',');
  }
  
}
