Array.prototype.swap=function(a,b){var x=this[a];this[a]=this[b];this[b]=x;return this;}
Array.prototype.inArray=function(a){for(var x=0;x<this.length;x++) if(this[x]==a) return x;return false;}


function $(){
 var x,l,r=[],a=arguments
 for(x=0,l=a.length;x<l;x++) {
  if(typeof a[x]=="string") {
   r.push(document.getElementById(arguments[x]))
  } else if(typeof a[x]=="function") {
   if(window.loaded){
    window.onload=function(){}
    window.onloads=null
    a[x]()
   } else {
    addEvent(window,'onload',a[x])
   }
  }
 }
 return r.length>1?r:r[0]
}

var JAX=new function(){

 var extend=this.extend=function(a,b){
  var x
  for(x in b) a[x]=b[x]
  return a
 }

 var d=document,dE=document.documentElement,JAX=this

 this.isArray=function(a){
  return a.constructor.toString().match("Array")?true:false;
 }

 this.overlay=function(show){
  show=parseInt(show)
  var ol=document.getElementById('overlay'),s,op
  if(ol){
   s=ol.style;
   s.zIndex=JAX.el.getHighestZIndex()
   s.top=0
   s.height=dE.clientHeight+"px";s.width=dE.clientWidth+"px";
   s.display=show?'':'none'
  } else {
   if(!show) return;
   ol=document.createElement('div');s=ol.style;
   ol.id='overlay'
   s.height=dE.clientHeight+"0px";s.width=dE.clientWidth+"0px";
   dE.appendChild(ol)
  }
 }

/********************************************************************/

 this.el={
  apply:function(a){
   var func
   for(var x in JAX.el) {
    if(x!="apply") a[x]=new Function("b","return JAX.el."+x+"(this,b)");
   }
   return a
  },
  getComputedStyle:function(a,b){
   if(a.currentStyle) return a.currentStyle
   else if(window.getComputedStyle) return getComputedStyle(a,b)
   return false
  },
  getCoordinates:function(a){
   var x=0,y=0,h=parseInt(a.offsetHeight)||0,w=parseInt(a.offsetWidth)||0
   do{
    x+=parseInt(a.offsetLeft)||0;y+=parseInt(a.offsetTop)||0;
   } while((a=a.offsetParent))
   return {x:x,y:y,yh:y+h,xw:x+w,w:w,h:h};
  },

  //insert a before b, insert a after b
  insertBefore:function(a,b){
   if(a.parentNode)a.parentNode.removeChild(a)
   b.parentNode.insertBefore(a,b)
  },
  insertAfter:function(a,b){
   if(a.parentNode)a.parentNode.removeChild(a)
   b.parentNode.insertBefore(a,b.nextSibling)
  },
  getElementsByClassName:function(parent,classn){
   if(!classn) {classn=parent;parent=document}
   var els=parent.getElementsByTagName("*"),x,r=[],regex=new RegExp(" "+classn+"| "+classn+" |"+classn+" |^"+classn+"$")
   for(x=0;x<els.length;x++) if(els[x].className.match(regex)) r.push(els[x])
   return !r.length?false:r;
  },

  addClass:function(a,c){
   if(!a.className.match(c)) a.className+=" "+c;
  },

  removeClass:function(a,c){
   a.className=a.className.replace(" "+c,"")
  },

  hasClass:function(a,c){
   return a.className.match(c);
  },

  getHighestZIndex:function(){
   var a=document.getElementsByTagName("*"),l=a.length,x,max=0
   for(x=0;x<l;x++) if(a[x].style.zIndex&&Number(a[x].style.zIndex)>max) max=a[x].style.zIndex
   return Number(max)+1
  }
 }

 this.drag=function(){
  var me=this;
  me.priv={
   start:function(e,t){
    e=(new JAX.event(e)).cancel().stopBubbling();var el=t||this,s=JAX.el.getComputedStyle(el)
    if(el.getAttribute('draggable')=="false") return
    me.sess={el:el,mx:parseInt(e.pageX),my:parseInt(e.pageY),ex:parseInt(s.left)||0,ey:parseInt(s.top)||0,info:{},bc:JAX.el.getCoordinates(el)}
    me.sess.zIndex=el.style.zIndex;
    el.style.zIndex=JAX.el.getHighestZIndex()
    if(typeof me.onstart=='function')me.onstart(me.sess)
    document.onmousemove=me.priv.drag;me.priv.drag(e);
    document.onmouseup=me.priv.drop;
   },
   drag:function(e){
    e=(new JAX.event(e)).cancel();
    var s=me.sess.el.style,sess,tmp=false,tx,ty
    var tx,ty,mx=tx=parseInt(e.pageX),my=ty=parseInt(e.pageY),left=me.sess.ex+mx-me.sess.mx,top=me.sess.ey+my-me.sess.my,b=me.bounds
    if(b){if(left<b[0]){mx=mx-left+b[0];left=b[0];} else if(left>b[0]+b[2])left=b[0]+b[2];
    if(top<b[1]){my=my-top+b[1];top=b[1];} else if (top>b[1]+b[3])top=b[1]+b[3];}
    s.left=left+"px";s.top=top+"px";
    tmp=JAX.el.getCoordinates(me.sess.el)
    if(me.useElCoord){tx=tmp.x;ty=tmp.y;}
    tmp=(sess=me.sess.info).droptarget;
    me.sess.info=sess={left:left,top:top,e:e,el:me.sess.el,mx:mx,my:my,droptarget:me.priv.testdrops(tx,ty),dx:mx-(sess.mx||mx),dy:my-(sess.my||my),self:me};
    if(typeof me.ondrag=='function')me.ondrag(sess)
    if(typeof me.ondragover=='function'&&sess['droptarget']&&tmp!=sess['droptarget'])me.ondragover(sess)
    if(typeof me.ondragout=='function'&&tmp&&sess['droptarget']!=tmp){sess['droptarget']=tmp;me.ondragout(sess);sess['droptarget']=false;}
   },
   drop:function(){
    document.onmousemove=document.onmouseup=function(){}
    if(typeof me.ondrop=='function')me.ondrop(me.sess.info)
    me.sess.el.style.zIndex=me.sess.zIndex;
    return true
   },
   testdrops:function(a,b){
    var x,d=me.droppables,z,r=false
    if (!d) return r
    for(x=0;x<d.length;x++) {z=JAX.el.getCoordinates(d[x]);if(d[x]!=me.sess.el&&a>=z.x&&b>=z.y&&a<=z.xw&&b<=z.yh)r=d[x];}
    return r
   }
  }
  me.drops=function(a){this.droppables=a;return this;}
  me.addListener=function(a){extend(this,a);return this;}
  me.apply=function(el,t){var x;if(el[0]){for(x=0;x<el.length;x++) me.apply(el[x]);return me;}var pos=JAX.el.getComputedStyle(el,'').position;if(!pos||pos=='static')el.style.position='relative';(t||el).onmousedown=t?function(e){me.priv.start(e,el);}:me.priv.start;return this;}
  me.boundingBox=function(x,y,w,h){this.bounds=[x,y,w,h];return this;}
 }

 this.event=function(e){
  e=e||window.event
  var dB=document.body
  if(typeof e.pageY=='undefined'){e.pageY=e.clientY+(parseInt(dE.scrollTop||dB.scrollTop)||0);e.pageX=e.clientX+(parseInt(dE.scrollLeft||dB.scrollLeft)||0);}
  e.cancel=function(){e.returnValue=false;if(e.preventDefault)e.preventDefault();return e;}
  e.stopBubbling=function(){if(e.stopPropagation) e.stopPropagation();e.cancelBubble=true;return e;}
  return e;
 }

/********************************************************************/

 this.ajax=function(s){
  var me=this
  me.xmlobj=window.XMLHttpRequest?XMLHttpRequest:ActiveXObject("Microsoft.XMLHTTP")
  me.setup={readyState:4,callback:function(){},method:0}
  me.setup=JAX.extend(me.setup,(s))

  me.load=function(a,b,c,d,e){
   //a=URL b=callback c=send_data d=POST e=type(1=update,2=load new)
   d=d||this.setup.method||"GET"
   if(c&&typeof c!="string") c=me.build_query(c)
   var xmlobj=new me.xmlobj;
   if(b) me.setup.callback=b
   xmlobj.onreadystatechange=function(status){if(xmlobj.readyState==me.setup.readyState) {me.setup.callback(xmlobj);}}
   if(!xmlobj) return false;
   xmlobj.open(d,a,true)
   xmlobj.url=a
   xmlobj.type=e
   if(d)xmlobj.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
   xmlobj.setRequestHeader("X-JSACCESS",e||1);
   xmlobj.send(c||null)
   return xmlobj
  }

  me.build_query=function(a){
    var q=""
    for(x in a) q+=encodeURIComponent(x)+"="+encodeURIComponent(a[x]||"")+"&";
    return q.substring(0,q.length-1)
  }
 }

 this.window=function(){
  var me=this,zindex=JAX.el.getHighestZIndex()
  me.title="Title"
  me.wait=true
  me.content="Content"
  me.container=false
  me.useoverlay=false
  me.minimizable=true
  me.resize=false
  me.className=""
  me.pos="center"
  me.create=function(){
   var d1=d.createElement('div'),
       d2=d.createElement('div'),
       d3=d.createElement('div'),
       d4=d.createElement('div'),
       b1=d.createElement('div'),
       b2=d.createElement('div'),
       pos=me.pos,x=0,y=0,s

   me.container=d1
   if(me.id) d1.id=me.id
   me.contentcontainer=d3

   if(me.useoverlay) var tmp=JAX.overlay(1,zindex)
   d1.className="window"+(me.className?" "+me.className:"");
   d2.className="title";
   d3.className="content";
   if(this.minimizable){
    b1.innerHTML="-"
    b1.onclick=me.minimize
   }
   b2.innerHTML="X"
   b2.onclick=me.close
   d4.appendChild(b1);d4.appendChild(b2);
   d4.className="controls"
   d2.innerHTML=me.title;d3.innerHTML=me.content;
   d2.appendChild(d4);d1.appendChild(d2);d1.appendChild(d3);d.body.appendChild(d1);

   if(me.resize){
    var targ=$$(me.resize,d1)
    if(!targ) return alert("Resize target not found");
    targ.style.width=targ.clientWidth+"px";targ.style.height=targ.clientHeight+"px";
    var rsize=document.createElement('div')
    rsize.className="resize"
    d1.appendChild(rsize)
    rsize.style.left=(d1.clientWidth-16)+"px";rsize.style.top=(d1.clientHeight-16)+"px";
    (new JAX.drag).boundingBox(100,100,Infinity,Infinity).addListener({ondrag:function(a){
     var w=parseFloat(targ.style.width)+a.dx,h=parseFloat(targ.style.height)+a.dy
     targ.style.width=w+"px";targ.style.height=h+"px";
    }}).apply(rsize)
   }

   s=d1.style
   s.zIndex=zindex+5

   me.setPosition(pos)

   me.drag=(new JAX.drag).boundingBox(0,0,dE.clientWidth-50,dE.clientHeight-50).apply(d1,d2);
   d1.close=me.close
   d1.minimize=me.minimize
   return d1
  }
  me.close=function(){
   var s=me.container.style
   if(me.animate) {JAX.sfx(me.container,10)
    .add("top",s.top,(parseFloat(s.top)+100)+"px")
    .then(function(){document.body.removeChild(me.container)}).play()}
   else document.body.removeChild(me.container)
   if(me.onclose) me.onclose()
   if(me.useoverlay) JAX.overlay(0)
  }
  me.minimize=function(){
   var c=me.container
   if (JAX.el.hasClass(c,"minimized")) {
    JAX.el.removeClass(c,"minimized")
    c.removeAttribute('draggable')
    me.setPosition(me.oldpos,0)
   } else {
    JAX.el.addClass(c,"minimized")
    c.setAttribute('draggable','false')
    me.oldpos=me.getPosition()
    me.setPosition("bl 0 0",0);
   }
  }
  me.setPosition=function(pos,animate){
   var d1=me.container,x=0,y=0,cH=dE.clientHeight,cW=dE.clientWidth
   if(s=pos.match(/(\d+) (\d+)/)) {x=Number(s[1]);y=Number(s[2]);}
   x=Math.floor(x);y=Math.floor(y);
   if(pos.charAt(1)=="r"){
    x=cW-x-d1.clientWidth;
   }
   switch(pos.charAt(0)){
    case "b":y=cH-y-d1.clientHeight;break;
    case "c":y=(cH-d1.clientHeight)/2;x=(cW-d1.clientWidth)/2;break;
   }
   x=Math.floor(x);y=Math.floor(y);

   if(x<0) x=0;if(y<0) y=0;
   d1.style.left=x+"px"
   if(me.animate&&animate!==0) {
    JAX.sfx(d1,10).add("top",(y-100)+"px",y+"px").play()
   } else d1.style.top=y+"px"
   me.pos=pos
  }
  this.getPosition=function(){
   var s=this.container.style
   return "tl "+parseFloat(s.left)+" "+parseFloat(s.top)
  }
 }
 this.window.close=function(win){
  do{
   if(win.close) {win.close();break;}
   win=win.offsetParent
  } while(win)
 }
}