var Class =
{ create: function()
  { return function()
    { this.initialize.apply(this, arguments);
    }
  }
}

var PopupControl = Class.create();
PopupControl.prototype = {

  initialize: function(callback, frequency) {
      this.callback = callback;
      this.frequency = frequency;
      this.currentlyExecuting = false;
      this.listcontrols=Array();
      this.selecteditems=Array();
      this.afterinit=false;
      this.target=false;
      this.targetid;
      this.mouseover=false;
      this.leftspace=0;
      this.mouseover=false;
      this.noiframe=false;
      this.oldclass=null;
      this.autoleftspace=true;
    },

  setvalues:function()
  { var args=this.setvalues.arguments;
    this.bcolor=args[2];
    this.popupname=args[0];
    this.visible=false;
    this.targetid=args[1];
//    this.blight=(args.length>=5?args[3]:'#999999');
//    this.bdark=(args.length>=5?args[4]:'#000000');
    this.blight='#bbbbbb';
    this.bdark='#bbbbbb';
    this.writebefore();
    this.noiframe=(navigator.userAgent.indexOf('MSIE')<0);
    return this;
  },

  setleftspace:function(num)
  { this.leftspace=num;
    return this;
  },

  setautoleftspace:function(num)
  { this.leftspace=num;
    this.autoleftspace=true;
    return this;
  },

  writebefore:function()
  { document.write(
     '<table style="display:none;z-index:98;border:1px solid #bbbbbb;" id="'+this.popupname+'popuptable" border="0" cellspacing="0" cellpadding="0" class="bg_dep">'+
     '<td class="depflight_clicked"><div>');
    return this;
  },

  writeafter:function()
  { document.write(
      '</div>'+        
      '</table>');
  },

  initobjects:function()
  {
  
    this.tbl=document.getElementById(this.popupname+'popuptable');
    this.target=document.getElementById(this.targetid);
    this.afterinit=true;
    this.tbl.popup=this;
    this.tbl.onmouseover=function(){this.popup.mouseover=true};
    this.tbl.onmouseout=function(){this.popup.mouseover=false};
  },

  resize:function()
  {
    ((((this.tbl.offsetWidth+this.currentox)<document.body.clientWidth) ||(this.tbl.offsetWidth>(this.currentox+this.target.offsetWidth))) !=(this.leftspace==0)) && this.show();
  },

  show:function()
  {
    if (this.visible)
      document.currentpopup=null;
    else if (document.currentpopup)
      document.currentpopup.hide();
    if (!this.afterinit) this.initobjects();
    var args=this.show.arguments;
    if (args.length==1 && args[0])
    {
       if (isObject(args[0]))
         this.target=args[0];
       else
        this.target=(document.getElementById(this.targetid=args[0]));
    }
     var p=this.target;
     for (var ox=0,oy=0;p!=null;ox+=p.offsetLeft,oy+=p.offsetTop,p=p.offsetParent);
     if (this.autoleftspace)
     {
         if (!this.visible)
         {
           this.tbl.style.position='absolute';
           this.tbl.style.top='0px';
           this.tbl.style.left='0px';
           this.tbl.style.visibility='hidden';
           this.tbl.style.display='';
         }
         if (((this.tbl.offsetWidth+ox)<document.body.clientWidth) || (this.tbl.offsetWidth>(ox+this.target.offsetWidth)))
           this.leftspace=0;
         else
           this.leftspace=this.tbl.offsetWidth-(this.target.offsetWidth+4);
     }
 
     this.tbl.style.position='absolute';
     this.tbl.style.top=(oy+this.target.offsetHeight)+'px';
     if(this.leftspace > 200)
       this.leftoffset = 4;
     else
       this.leftoffset = 0;
     this.tbl.style.left=(ox-(this.leftoffset+this.leftspace))+'px';


     this.tbl.style.display='';
     if (this.autoleftspace)
       this.tbl.style.visibility='';
     if (!this.visible)
     {
       if (!this.oldclass)
         this.oldclass=this.target.className;
       this.target.className=this.target.className+' depflight_clicked';
     }
     
     this.visible=true;
     this.currentox=ox;
     this.fixframe();
     document.currentpopup=this;
     if (this.onshow)
       this.onshow();
  },

  fixframe:function()
  {
      return;
      if (this.noiframe) return;
     if (!this.visible) return;
     this.iframe.style.width=(this.tbl.offsetWidth)+'px';
     this.iframe.style.height=(this.tbl.offsetHeight)+'px';
  },

  hide:function()
  { if (this.forcevisible) return;
    if (!this.afterinit) return true;
    this.visible=false;  
     this.tbl.style.display='none';
     this.target.className=this.oldclass;
     this.oldclass=null;
     if (this.onhide)
       this.onhide();
    document.currentpopup=null;
  },

  toggle:function()
  { if (this.visible)
      this.hide();
    else
     this.show(this.toggle.arguments.length==1?this.toggle.arguments[0]:null);
   if (this.ontoggle)
     this.ontoggle(this.visible);
  },

  focus:function()
  { this.show();
  },

  blur:function()
  { if (!this.mouseover)
    { this.hide();
      this.mouseover=false;
    }
  }

}



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(img/trActiveState.gif)";
      sender.style.backgroundColor='';
    }
    else if (state==3) {
      sender.style.backgroundImage="";
      sender.style.backgroundColor='black';
    }
    sender.className=(sender.valid?'f11 wp24 deptgroup color12bg':'flightpanel_label_disabled wp24 deptgroup color12bg');
  },

  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>';
    if(apt[1] != 'empty')
      target.innerHTML='<div>&nbsp;&nbsp;<input type="checkbox" class="flightpanel_checkbox" readonly="true" />&nbsp;&nbsp;'+apt[1]+'</div>';
    else
      target.innerHTML='<div>&nbsp;&nbsp;<input type="checkbox" class="flightpanel_checkbox" readonly="true" style="display:none;"/>&nbsp;&nbsp;</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 = "bg2";
     	  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 = "color12bg ";
      }
      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()
  {
     selector = document.getElementById('flightpanel_hin_text');
     selector.value = 'beliebig';
     if (!this.input) return;
     var values=this.input.value.toUpperCase().split(',');
     var names=selector.value.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);
      }
    }
    for (var s=this.selecteditems.length-1;s>=0;s--)
    {
      var found=false;
      for (var i=names.length-1;i>=0;i--)
        if (names[i]==this.selecteditems[s].aptname)
        {
          found=true;
          break;
        }
      if (!found)
      {
        selector.value = '';
        names.push(this.selecteditems[s].aptname);
      }
    }
         var str = 'beliebig';
           if(str.search(names))
             names.splice('beliebig',1);
           selector.innerHTML = names;
           this.input.value = values;
},

  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(',');
  }

}
