var multiselect_iframe;
var thing='';
var submiting = false;

function U(au,aw,ap,ai,aj,ah,Z,ac,D,G) {
  av = this;

  this.ax = au;
  this.f = aw;
  this.O = ap;
  this.A = ai;
  this.P = aj;
  this.J = ah;
  this.w = Z;
  this.R = ac;
  this.L = D;
  this.o = G;
  this.n = false;

  this.r=document.getElementById(this.f);
    this.Q=false;
    this.at = function() {
      this.r.style.display = "none";
      thing = this.r.getAttribute('attr');
      B = document.getElementById(this.f+'_box');
      B.innerHTML += '<div class="multi-input"><input readonly="readonly" id="'+this.f+'_text" name="'+this.f+'_text" type="text" value="" class="'+this.J+'"  /></div>';
      if(thing == 'single'){
          B.innerHTML += '<div id="'+this.f+'_arrow" class="singleselect multi-select" ></div><div id="'+this.f+'_optionDiv" class="'+this.w+'" ></div>';
        } else {
          B.innerHTML += '<div id="'+this.f+'_arrow" class="multiselect multi-select" ></div><div id="'+this.f+'_optionDiv" class="'+this.w+'" ></div>';
        }
    };
    // $label.text = implode(',', $array_of_selected_elements);
    this.F = function() {
      this.r=document.getElementById(this.f);
      C = this.r.options;
      //show("debug",this.r.id +'=id | name='+ this.r.name);
        v = '';

				for(j=0; j < C.length; j++){
					if(C[j].selected == true){
						if(v != '') {
							v += ',';
            }
            v += C[j].innerHTML;
          }
        }
        this.t.value = v;
    };

    this.K = function() {
      var thing2 = '';
			thing2 = this.d.parentNode.getAttribute('attr');
      ar = document.body.offsetTop;
      ab = document.body.offsetLeft;

      if(this.n == false) {
        this.n = true;
        this.d.style.top  = aa(this.t) + this.t.offsetHeight + 'px';
        this.d.style.left = Y(this.B) + 'px';
        this.d.style.display = 'block';
        if((true || IE6_TODO) && (thing2 =="single" || thing2 == "multiple")) {
          this.d.parentNode.appendChild(multiselect_iframe);
          this.d.style.zIndex = 100;

          multiselect_iframe.style.top  = this.d.style.top;
          multiselect_iframe.style.left = this.d.style.left;
          multiselect_iframe.style.height = (this.d.offsetHeight) + 'px';
          multiselect_iframe.style.width  = (this.d.offsetWidth)  + 'px';
          multiselect_iframe.style.display = 'block';
				}
        this.F();
      } else {
        multiselect_iframe.style.display = 'none';
        this.al();
      }
    };


    this.al = function() {
			var wasopen = (this.d.style.display == 'block') ? true : false;
			this.n=false;
      this.d.style.top  = -2000 + 'px';
      this.d.style.left = -2000 + 'px';
      this.d.style.display = 'none';
      if(!submiting && wasopen)
        validator.validate(this.r);
    };

    // create the table and position the div
    this.T = function() {
			C = this.r.options;
			this.d = document.getElementById(this.f+'_optionDiv');
      
      /*
			if(this.t.offsetWidth)
				this.d.style.width = (this.t.offsetWidth - 2) + 'px'; // this.d.style.width = this.t.scrollWidth + this.l.width + 'px';
      */
			
      v = '<table id="'+this.f+'_table" cellpadding="0" cellspacing="0" border="0" width="100%" >';
			for(j=0; j < C.length; j++){
				if(C[j].selected == true) {
					v += '<tr class="row"><td id="'+this.f+'_td_'+j+'" nowrap class="'+this.o+'">'+C[j].innerHTML+'</td></tr>';
        } else {
					v += '<tr class="row"><td id="'+this.f+'_td_'+j+'" nowrap class="'+this.R+'">'+C[j].innerHTML+'</td></tr>';
        }
      }
      v += '</table>';

      this.d.innerHTML = v;
      this.d.style.display = 'block';

      this.I=document.getElementById(this.f+'_table');

      /*
      if(this.I.scrollWidth > parseInt(this.d.style.width)){
        this.d.style.width = this.I.scrollWidth + 'px';
      }
      this.d.style.height = this.I.scrollHeight + 'px';*/

      as = this;
    };

		this.X=function(j) {
			selectelement = this.r;
			if(this.r.options[j].selected == true){
				if(this.r.getAttribute('attr')=="single"){
					for (x=0;x<this.r.options.length;x++){
						this.r.options[x].selected=false;
					}
				} else {
					this.r.options[j].selected =false;
				}
			}
			else if(this.r.getAttribute('attr')=="single"){
					for (x=0;x<this.r.options.length;x++){
						this.r.options[x].selected=false;
					}
					this.r.options[j].selected = true;
			} else {
				this.r.options[j].selected = true;
			}
			this.F();
		};

    this.H=function(index,ao){
      if(this.r.options[index].selected==true){
        this.e[index].className=G;
      } else{
        if(ao=="selected") {
					this.e[index].className=D;
        } else if(ao=="hover"){
          this.e[index].className=D;
        } else {
          this.e[index].className=ac;
        }
      }
      this.e[index].refresh;
    };
		this.HH=function(index,ao){
			for(k=0; k < this.r.options.length; k++) {
				if(this.r.options[k].selected==true){
					this.e[k].className=G;
				} else {
					this.e[k].className=ac;
				}
				this.e[k].refresh;
      }
    };
};

// add eventhandlers
function af(g){
  var thing = c[g].r.getAttribute('attr');
  if (thing == "single" || thing == "multiple"){
    c[g].l.onclick = function() {
        c[g].K();
      //show("debug",c[g].l.id);
    };

    c[g].l.onmouseover = function() {
      c[g].l.src=c[g].O;
    };

    c[g].l.onmouseout = function() {
      c[g].l.src=c[g].A;
		};
    c[g].t.onclick = function() {
          c[g].K();
    };

    c[g].B.onclick = function(ay){
        c[g].Q = true;
      //show("debug",c[g].B.id);
    };

    for(k=0; k < c[g].r.options.length; k++) {
      c[g].e[k] = document.getElementById(c[g].f+'_td_'+k);
      if(thing == "single"){
        eval('c['+g+'].e['+k+'].onclick = function(){ c['+g+'].X( '+k+' ); c['+g+'].HH( '+k+',\'selected\' );aqq();}');
      } else if(thing == "multiple"){
        eval('c['+g+'].e['+k+'].onclick = function(){ c['+g+'].X( '+k+' ); c['+g+'].H( '+k+',\'selected\' );}');
      }
      eval('c['+g+'].e[ '+k+'].onmouseover = function(){  c['+g+'].H('+k+', \'hover\' ); }');
      eval('c['+g+'].e[ '+k+'].onmouseout = function(){  c['+g+'].H( '+k+', \'normal\' ); }');
    }
  }
};

function aa(am){
  V=am;
  ag = 0;

 if(am.offsetTop);
   ag = am.offsetTop;


while(V.offsetParent != null && V.offsetParent.tagName != 'BODY' && V.offsetParent.className != 'contentholder'){
    V = V.offsetParent;

    if(V.offsetTop)
     ag += V.offsetTop;
  }

  return ag;
};

function Y(an){
  M = an;

  ad = 0;
  if(an.offsetLeft)
    ad = an.offsetLeft;

  while(M.offsetParent != null && M.offsetParent.tagName != 'BODY' && M.offsetParent.className != 'contentholder'){
    M = M.offsetParent;

    if(M.offsetLeft)
      ad += M.offsetLeft;
  }
  return ad;
};

function az(){};

c=Array();

function multiselectInit(ak, O, A, P, J, w, R, L, o){
  //show("debug",ak);
  if(true || IE6_TODO) {
    multiselect_iframe = document.createElement('iframe');
    multiselect_iframe.src = 'empty.html';
    multiselect_iframe.style.display  = 'none';
    multiselect_iframe.style.position = 'absolute';
    multiselect_iframe.style.border = 'none';
    multiselect_iframe.style.zIndex   = 99;

    lightboxContent.appendChild(multiselect_iframe);
  }

  c[c.length] = new U(c.length, ak, O, A, P, J, w, R, L, o);
  c[c.length-1].at(); // create div and hide selectbox
  c[c.length-1].B = document.getElementById(c[c.length-1].f+'_box');
  c[c.length-1].t = document.getElementById(c[c.length-1].f+'_text');
  c[c.length-1].l = document.getElementById(c[c.length-1].f+'_arrow');
  c[c.length-1].d = document.getElementById(c[c.length-1].f+'_optionDiv');
  c[c.length-1].e = new Array();
  c[c.length-1].F(); // update label
  c[c.length-1].T(); // create table
  af(c.length-1); // add eventhandlers

};

function multiselectReInit(selectID) {
  for(selectBox in c)
  {
    if (selectID == c[selectBox].f || selectID == 'All')
    {
      c[selectBox].F(); // update label
      c[selectBox].T(); // create table
      af(selectBox); // add eventhandlers
    }
  }
}

function aq(){
  var hide_iframe = true;
  for(m = 0; m < c.length; m++){
    if(c[m].Q == false){
      c[m].al();
    } else {
      hide_iframe = false;
    }
    if(c && c[m] && c[m].Q)
      c[m].Q = false;
  }
  submiting = false;
  if(hide_iframe && multiselect_iframe) {
    multiselect_iframe.style.display  = 'none';
  }
};
function aqq(){
  var hide_iframe = true;
  for(m = 0; m < c.length; m++){
    c[m].al();
    if(c && c[m] && c[m].Q)
      c[m].Q = false;
  }
  if(hide_iframe && multiselect_iframe) {
    multiselect_iframe.style.display  = 'none';
  }
};

function ae(){
  for(m=0; m < c.length; m++){
    if(c[m].n==true){
      c[m].al();
      c[m].K();
    }
  }
};

document.onclick = aq;
window.onresize  = ae;