


function Drag ( dragobjekt, fromX, fromY, width, height ) { 
	
	this.constructor = Drag;
	this.onUpdate;
	
	var clsRef = this;
	
	var cX = 0;
	var cY = 0;
	
	// Mausposition
	var mouseX = 0;
	var mouseY = 0;
	
	// Maus fromPos
	var oldMouseX = 0;
	var oldMouseY = 0;
	
	var isDragging = false;
	
	function initDrag ( ) {
		
		document.onmousemove = drag;
		document.onmouseup = dragstop;
		dragobjekt.onmousedown = makeClick;
		dragobjekt.addEvent('click', makeClick);
	
	}
	
	this.getMouseX = function () {
		
		return mouseX;
		
	}

	this.getMouseY = function () {
		
		return mouseY;
		
	}
	
	function makeClick ( event ) {
		
		oldMouseX = document.all ? window.event.clientX : event.pageX;
		oldMouseY = document.all ? window.event.clientY : event.pageY;
		isDragging = true;
		
		if ( event != undefined ) event.cancelBubble = true;
		
		document.onselectstart = function () { return false; };
		
		return false;
		
	}
	
	function dragstop() {
		
		isDragging = false;
		
		document.onselectstart = function () { };
		
		cX = dragobjekt.style.left.split("px").join("");
		cY = dragobjekt.style.top.split("px").join("");
		  
	}
	
	this.setPercent = function ( num ) {
		
		var max = height - fromX ;
    	var nPos = num * ( max / 100 );
    	dragobjekt.style.top = nPos + "px";
    	cY = dragobjekt.style.top.split("px").join("");
    	
	}
	
	function drag(ereignis) {
		
		var ref = this;
		
		mouseX = document.all ? window.event.clientX : ereignis.pageX;
		mouseY = document.all ? window.event.clientY : ereignis.pageY;
		  
		  if( isDragging ) {
			
			var movedX = mouseX - oldMouseX;
			var movedY = mouseY - oldMouseY;
			
			var realX = ( cX -(- movedX) );
			var realY = ( cY -(- movedY) );
			
			if ( realX < fromX ) {
				realX = fromX;
			} else if ( realX > fromX+width ) {
				realX = fromX+width;
			}
			
			if ( realY < fromY ) {
				realY = fromY;
			} else if ( realY > fromY+height ) {
				realY = fromY+height;
			}
			
		    dragobjekt.style.left = realX + "px";
		    dragobjekt.style.top = realY + "px";
		    
		    dragobjekt.focus();
		    
		    if ( clsRef.onUpdate != undefined ) {
		    	
		    	var max = height - fromX ;
		    	var current = realY - fromX;
		    	var perc = current / ( max / 100 );
		    	
		    	clsRef.onUpdate ( perc );
		    	
		    }
		    
		  }
		  
	}
	
	function findPosX(obj)
	{
	  var curleft = 0;
	  if(obj.offsetParent)
	      while(1) 
	      {
	        curleft += obj.offsetLeft;
	        if(!obj.offsetParent)
	          break;
	        obj = obj.offsetParent;
	      }
	  else if(obj.x)
	      curleft += obj.x;
	  return curleft;
	}

	function findPosY(obj)
	{
	  var curtop = 0;
	  if(obj.offsetParent)
	      while(1)
	      {
	        curtop += obj.offsetTop;
	        if(!obj.offsetParent)
	          break;
	        obj = obj.offsetParent;
	      }
	  else if(obj.y)
	      curtop += obj.y;
	  return curtop;
	}
	
	initDrag ( );
	
};
