xAddEventListener(window,'load',btnInit,false);
xAddEventListener(window,'load',trgInit,false);
xAddEventListener(window,'resize',animBoxesResize,false);

function btnInit(){
  var i,a=xGetElementsByClassName('jsButton');
  for(i=0;i<a.length;++i){
    a[i].onclick=btnOnClick;
  }
}

function trgInit(){
  var i,a=xGetElementsByClassName('jsTrigger');
  for(i=0;i<a.length;++i){
    a[i].onmouseover=trgOnOver;
    a[i].onmouseout=trgOnOut;
  }
  a=xGetElementsByClassName('jsContainer');
  for(i=0;i<a.length;++i){
    a[i].onmouseover=trgOnOver;
    a[i].onmouseout=trgOnOut;
  }
}

function btnOnClick(){
  var i,xa=animBox.instances;
  for(i=0;i<xa.length;++i){
    if('btn_'+xa[i].eid==this.id){
      if(xa[i].st==1){
        xa[i].expand();
      }
      else if(xa[i].st==2){
        xa[i].collapse();
      }else xa[i].animate();
    }
  }
}

function trgOnOver(){
  var i,xa=animBox.instances;
  for(i=0;i<xa.length;++i){
    if('btn_'+xa[i].eid==this.id||xa[i].eid==this.id){
      if(xa[i].out_tmr){
        clearTimeout(xa[i].out_tmr);
        xa[i].out_tmr=null;
      }
      if(xa[i].st==1){
        xa[i].expand();
        xa[i].act=1;
      }else{
        xa[i].act=1;
      }
    }
  }
}

function trgOnOut(){
  var i,xa=animBox.instances;
  for(i=0;i<xa.length;++i){
    if('btn_'+xa[i].eid==this.id||xa[i].eid==this.id){
      xa[i].act=0;
      if(!xa[i].out_tmr)xa[i].out_tmr=setTimeout('trgOnTimer('+xa[i].idx+')',200);
    }
  }
}

function trgOnTimer(idx){
  var xa=animBox.instances;
  xa[idx].out_tmr=null;
  if(xa[idx].act==0){
    xa[idx].collapse();
  }
}

function animBoxesResize(){
  var i,a=animBox.instances;
  for(i=0;i<a.length;++i){
    if(a[i].pe){a[i].reposition();
    }
  }
}

function animBox(e,st,at,qc,tt,oas,cas,orf,otf,oed,oea,oef){
  this.init(e,st,at,qc,tt,oas,cas,orf,otf,oed,oea,oef);
  var a=animBox.instances;
  var i;
  for(i=0;i<a.length;++i){
    if(!a[i])break;
  }
  a[i]=this;
  this.idx=i;
  return this;
}

animBox.instances=[];
animBox.prototype.init=function(e,st,at,qc,tt,oas,cas,orf,otf,oed,oea,oef){
  this.e=xGetElementById(e);
  this.pe='bababah';
  this.xs=null;
  this.ys=null;
  this.eid=e;
  this.act=0;
  this.st=st||2;
  this.at=at||2;
  this.qc=qc||1;
  this.tt=tt||2000;
  this.oas=oas||'v+';
  this.cas=cas||'v-';
  this.orf=orf;
  this.otf=otf;
  this.oed=oed;
  this.oea=oea;
  this.oef=oef;
  this.to=20;
  this.as=false;
  this.x=xPageX(this.e);
  this.y=xPageY(this.e);
  this.w=xWidth(this.e);
  this.h=xHeight(this.e);
  if(st==1){
    xHide(this.e);
    switch(this.oas){
      case"h-":xResizeTo(e,2,this.h);
      break;
      case"h+":xResizeTo(e,2,this.h);
      break;
      case"v-":xResizeTo(e,this.w,2);
      break;
      case"v+":xResizeTo(e,this.w,2);break;
      default:
    }
  }
  return this;
};

animBox.prototype.reposition=function(e,xs,ys){
  var x,y,a=animBox.instances[this.idx];
  if(!e){
    e=a.pe;xs=a.xs;ys=a.ys
  }
  if(!xGetElementById(e))
  return;
  var px=xPageX(e);
  var py=xPageY(e);
  var pw=xWidth(e);
  var ph=xHeight(e);
  a.pe=e;
  a.xs=xs;
  a.ys=ys;
  if(xs=='+'){
    x=px+pw;
  }
  else if(xs=='-'&&ys==null){
    x=px-a.w;
  }
  else if(xs=='-'){
    x=px-a.w+pw;
  }
  else{x=px;}
  if(ys=='+'){y=py+ph;}
  else if(ys=='-'){y=py-a.h;}
  else{y=py;}
  xMoveTo(a.eid,x,y);
  a.x=x;
  a.y=y;
  switch(a.oas){
    case'h-':
    xResizeTo(a.eid,2,a.h);
    xMoveTo(a.eid,a.x+a.w-2,a.y);
    break;
    case'h+':xResizeTo(a.eid,2,a.h);break;
    case'v-':xResizeTo(a.eid,a.w,2);xMoveTo(a.eid,a.y,a.y+a.h-2);break;
    case'v+':xResizeTo(a.eid,a.w,2);break;
  }
};

animBox.prototype.animate=function(){
  var a=animBox.instances[this.idx];
  if(a.st==1){a.expand();}
  else if(a.st==2){a.collapse();}
  else
  return;
};

animBox.prototype.expand=function(){
  var a=animBox.instances[this.idx];
  a.x1=(a.oas=='h-'||a.oas=='h+')?2:a.w;
  a.y1=(a.oas=='v-'||a.oas=='v+')?2:a.h;
  a.x2=a.w;a.y2=a.h;a.orf=onRun;
  a.otf=onTarget;a.oea=0;a.oef=onEnd;
  xShow(a.e);
  a.start();

  function onRun(o){
    switch(o.oas){
      case'h-':xMoveTo(o.e,(o.x+o.w-2)-o.xd,o.y);break;
      case'v-':xMoveTo(o.e,o.x,(o.y+o.h-2)-o.yd);break;
    }
    xResizeTo(o.e,Math.round(o.xd),Math.round(o.yd));
  }

  function onTarget(o){}

  function onEnd(o){
    xResizeTo(o.e,o.w,o.h);xMoveTo(o.e,o.x,o.y);o.st=2;
  }
};

animBox.prototype.collapse=function() {
  var a=animBox.instances[this.idx];a.x1=xWidth(a.e);a.y1=xHeight(a.e);
  a.x2=(a.cas=='h-'||a.cas=='h+')?2:a.w;a.y2=(a.cas=='v-'||a.cas=='v+')?2:a.h;
  a.orf=onRun;a.otf=onTarget;a.oea=0;a.oef=onEnd;a.start();
  return a;

  function onRun(o){
    switch(o.cas){
      case'h+':xMoveTo(o.e,(o.x+o.w-2)-o.xd,o.y);break;
      case'v+':xMoveTo(o.e,o.x,(o.y+o.h-2)-o.yd);break;
    }
    xResizeTo(o.e,Math.round(o.xd),Math.round(o.yd));
  }

  function onTarget(o){}

  function onEnd(o){xHide(o.e);xResizeTo(o.e,o.w,2);o.st=1;}};

  animBox.prototype.start=function(){
    var a=this;
    if(a.at==1){
      a.ap=1/a.tt;
    }else{a.ap=a.qc*(Math.PI/(2*a.tt));}

    if(xDef(a.x1)){a.xm=a.x2-a.x1;}
    if(xDef(a.y1)){a.ym=a.y2-a.y1;}
    if(!(a.qc%2)){
      if(xDef(a.x1))a.x2=a.x1;
      if(xDef(a.y1))a.y2=a.y1;
    }
    if(!a.tmr){
      var d=new Date();a.t1=d.getTime();a.tmr=setTimeout('animBox.run('+a.idx+')',1);
    }
  };

  animBox.run=function(i){
    var a=animBox.instances[i];
    if(!a)return;var d=new Date();a.et=d.getTime()-a.t1;
    if(a.et<a.tt){
      a.tmr=setTimeout('animBox.run('+i+')',a.to);
      a.af=a.ap*a.et;
      if(a.at==2){a.af=Math.abs(Math.sin(a.af));}
      else if(a.at==3){a.af=1-Math.abs(Math.cos(a.af));}

      if(xDef(a.x1))a.xd=a.xm*a.af+a.x1;
      if(xDef(a.y1))a.yd=a.ym*a.af+a.y1;a.orf(a);
    }
    else{
      var rep=false;
      if(xDef(a.x2))a.xd=a.x2;
      if(xDef(a.y2))a.yd=a.y2;
      a.tmr=null;a.otf(a);
      if(xDef(a.oef)){
        if(a.oed)setTimeout(a.oef,a.oed);
        else if(xStr(a.oef)){
          rep=eval(a.oef);
        }
        else{rep=a.oef(a,a.oea);
        }
      }
      if(rep){a.resume(true);}
    }
  };

  animBox.prototype.kill=function(){animBox.instances[this.idx]=null;};
