addEvent(window,"onload",initDragables)
function initDragables(){
  var dragables = document.getElementsByClassName("Draggable");
  for(var i=0;i<dragables.length;i++)
    new DraggedObj(dragables[i]);
}


/* ******* DraggedObj ******* */
function DraggedObj(domNode){
  this.domNode = domNode;
  this.domNodeDragged = new Dragger(this.domNode,this.domNode.offsetLeft,this.domNode.offsetTop);
  this.domNodeDragged.ondrag = this.eventHandler("onDrag");
  this.domNodeDragged.ondragstart=this.eventHandler("onDragStart");
  this.domNodeDragged.ondragend=this.eventHandler("onDragEnd");
  this.onmousedown=null;
}
DraggedObj.prototype.onDrag = function(){
  //this.domNode.innerHTML = "onDrag";
  addToClassName(this.domNode,"Dragged");
}
DraggedObj.prototype.onDragStart=function(b){
  if(this.onmousedown)this.onmousedown(b)
};
DraggedObj.prototype.onDragEnd=function(b){
  //this.onStateChanged()
  //this.domNode.innerHTML = "onDragEnd";
  removeFromClassName(this.domNode,"Dragged");
};


//struct representing browser
function ci(vg,Pf,yh){
  this.type=vg;
  this.version=Pf;
  this.os=yh
};
var m=new ci(0,0,null);

var _timeoutCounter=0;
Object.prototype.setTimeout=function(Zd,Kh){
  var ae="tempVar"+_timeoutCounter;
  _timeoutCounter++;
  eval(ae+" = this;");
  var dh=Zd.replace(/\\/g,"\\\\").replace(/\"/g,'\\"');
  return window.setTimeout(ae+'._setTimeoutDispatcher("'+dh+'");'+ae+" = null;",Kh)
};
Object.prototype._setTimeoutDispatcher=function(Zd){
  eval(Zd)
};
Object.prototype.eventHandler=function(Rf){
  var h=this;
  h=h;
  return function(b){
    if(!b)b=window.event;
    if(b&&!b.target)b.target=b.srcElement;
    h[Rf](b)
  }
};

//helpers (abstract from browser) (RS)
function attachEvt(Ze,ia,pf){
  if(m.type==1){
    Ze.attachEvent("on"+ia,pf)
  }else{
    Ze.addEventListener(ia,pf,false)
  }
}
function cancelBubble(b){
  //if(m.type==1){
  if(window.event){
    window.event.cancelBubble=true
  }else{
    b.cancelBubble=true;
    b.preventDefault();
    b.stopPropagation()
  }
}

//classes Point, Size, Bounds (RS)
function Point(x,y){
  this.x=x;
  this.y=y
}
Point.prototype.toString=function(){
  return"("+this.x+", "+this.y+")"
};
Point.prototype.equals=function(Q){
  if(!Q)return false;
  return this.x==Q.x&&this.y==Q.y
};
Point.prototype.distanceFrom=function(Q){
  var va=this.x-Q.x;
  var za=this.y-Q.y;
  return Math.sqrt(va*va+za*za)
};
Point.prototype.approxEquals=function(Q){
  if(!Q)return false;
  return zc(this.x,Q.x)&&zc(this.y,Q.y)
};

//approx equal (RS)
function zc(lg,qh){
  return Math.round(lg*1000000)==Math.round(qh*1000000)
}

//class Dragger: handles mouse tracking (RS)
function Dragger(C,K,top,P){
  this.src=C;
  this.container=P;
  this.ondragstart=null;
  this.ondrag=null;
  this.ondragend=null;
  this.onmove=null;
  this.onclick=null;
  this.disabled=false;
  this.dragPoint=new Point(0,0);
  this.clickStartPos=new Point(0,0);
  this.src.style.position="absolute";
  this.moveTo(K,top);
  this.mouseDownHandler=this.eventHandler("onMouseDown");
  this.mouseMoveHandler=this.eventHandler("onMouseMove");
  this.mouseUpHandler=this.eventHandler("onMouseUp");
  if(m.type==2){
    window.addEventListener("mouseout",this.eventHandler("onWindowMouseOut"),false)
  }
  this.eventSrc=this.src.setCapture?this.src:window;
  if(this.src.addEventListener){
    this.src.addEventListener("mousedown",this.mouseDownHandler,false)
  }else{
    this.src.onmousedown=this.mouseDownHandler
  }

}
Dragger.prototype.moveTo=function(K,top){
  if(this.left!=K||this.top!=top){
    this.left=K;
    this.top=top;
    this.src.style.left=this.left+"px";
    this.src.style.top=this.top+"px";
    if(this.onmove)this.onmove()
  }
};
Dragger.prototype.onMouseDown=function(b){
  if(b.cancelDrag){
    return
  }
  var Ih=m.type==2?b.button==0:b.button==1;
  if(this.disabled){
    cancelBubble(b);
    return false
  }
  this.dragPoint.x=b.screenX;
  this.dragPoint.y=b.screenY;
  if(window.addEventListener){
    this.eventSrc.addEventListener("mousemove",this.mouseMoveHandler,false);
    this.eventSrc.addEventListener("mouseup",this.mouseUpHandler,false)
  }else{
    this.eventSrc.onmousemove=this.mouseMoveHandler;
    this.eventSrc.onmouseup=this.mouseUpHandler
  }
  if(this.src.setCapture)this.src.setCapture();
  this.clickStartTime=(new Date()).getTime();
  this.clickStartPos.x=b.screenX;
  this.clickStartPos.y=b.screenY;
  if(this.ondragstart)this.ondragstart(b);
  this.originalCursor=this.src.style.cursor;
  this.src.style.cursor="move";
  cancelBubble(b)
};
Dragger.prototype.onMouseMove=function(b){
  if(m.os==1){
    if(b==null)return;
    if(this.dragDisabled){
      this.savedMove=new Object();
      this.savedMove.screenX=b.screenX;
      this.savedMove.screenY=b.screenY;
      return
    }
    this.setTimeout("this.dragDisabled = false; this.onMouseMove(this.savedMove)",30);
    this.dragDisabled=true;
    this.savedMove=null
  }
  var ue=1;
  if(m.type==3&&m.version==1){
    ue=-1
  }
  var x=this.left+(b.screenX-this.dragPoint.x);
  var l=this.top+(b.screenY-this.dragPoint.y)*ue;
  var va=0;
  var za=0;
  if(this.container){
    var Ec=x;
    if(x<this.container.minX){
      Ec=this.container.minX
    }else{
      var ed=this.container.maxX-this.src.offsetWidth;
      if(x>ed){
        Ec=ed
      }
    }
    va=Ec-x;
    x=Ec;
    var Qc=l;
    if(l<this.container.minY){
      Qc=this.container.minY
    }else{
      var Jc=this.container.maxY-this.src.offsetHeight;
      if(l>Jc)Qc=Jc
    }
    za=Qc-l;
    l=Qc
  }
  this.moveTo(x,l);
  this.dragPoint.x=b.screenX+va;
  this.dragPoint.y=b.screenY+za;
  if(this.ondrag)this.ondrag(b)

};
Dragger.prototype.onMouseUp=function(b){
  if(this.eventSrc.removeEventListener){
    this.eventSrc.removeEventListener("mousemove",this.mouseMoveHandler,false);
    this.eventSrc.removeEventListener("mouseup",this.mouseUpHandler,false)
  }else{
    this.eventSrc.onmousemove=null;
    this.eventSrc.onmouseup=null
  }
  this.src.style.cursor=this.originalCursor;
  if(document.releaseCapture)document.releaseCapture();
  if(this.ondragend)this.ondragend(b);
  if(this.onclick){
    var Zg=(new Date()).getTime();
    if(Zg-this.clickStartTime<=500&&(Math.abs(this.clickStartPos.x-b.screenX)<=2&&Math.abs(this.clickStartPos.y-b.screenY)<=2)){
      this.onclick(b)
    }
  }
};
Dragger.prototype.onWindowMouseOut=function(b){
  if(!b.relatedTarget)this.onMouseUp(b)
};
Dragger.prototype.disable=function(){
  this.disabled=true
};
Dragger.prototype.enable=function(){
  this.disabled=false
};