var _startX = 0; // mouse starting positions 
var _startY = 0; 
var _offsetX = 0; // current element offset 
var _offsetY = 0; 
var _dragElement; // needs to be passed from OnMouseDown to OnMouseMove 
var _oldZIndex = 0; // we temporarily increase the z-index during drag 
var _dragObject;
var _lastZIndex = 10000;

function def_OnMouseUP(e){
	 if (e == null) 
        e = window.event; 
        
	_startX = e.clientX;
	_startY = e.clientY;
}

document.onmouseup = def_OnMouseUP;

function higestZIndex(){
	_lastZIndex+=1;
	return _lastZIndex;
}

function InitDragDrop(obj){ 
	_dragObject = document.getElementById(obj);
	document.onmousedown = OnMouseDown; 
	document.onmouseup = OnMouseUp; 
}

function rmDragDrop(){
	_dragObject = null;
	document.onmousedown = null; 
	document.onmouseup = def_OnMouseUP;
}

function OnMouseDown(e){
    // IE is retarded and doesn't pass the event object
    if (e == null) 
        e = window.event; 
    
    // IE uses srcElement, others use target
    var target = _dragObject;
	
    // for IE, left click == 1
    // for Firefox, left click == 0
    if (true)
    {
        // grab the mouse position
        _startX = e.clientX;
        _startY = e.clientY;
        
        // grab the clicked element's position
        _offsetX = ExtractNumber(target.style.left);
        _offsetY = ExtractNumber(target.style.top);
        
        // bring the clicked element to the front while it is being dragged
        _oldZIndex = target.style.zIndex;
        _lastZIndex+=1;
        target.style.zIndex = _lastZIndex;
        
        
        // we need to access the element in OnMouseMove
        _dragElement = target;

        // tell our code to start moving the element with the mouse
        document.onmousemove = OnMouseMove;
        
        // cancel out any text selections
        document.body.focus();

        // prevent text selection in IE
        document.onselectstart = function () { return false; };
        // prevent IE from trying to drag an image
        target.ondragstart = function() { return false; };
        
        // prevent text selection (except IE)
        return false;
    }
}

function OnMouseMove(e){
	
    if (e == null) 
        var e = window.event; 
    // this is the actual "drag code"

    _dragElement.style.left = (_offsetX + e.clientX - _startX) + 'px';
    _dragElement.style.top = (_offsetY + e.clientY - _startY) + 'px';
    
}

function OnMouseUp(e){
    if (_dragElement != null){
       // _dragElement.style.zIndex = 9999;

        // we're done with these events until the next OnMouseDown
        document.onmousemove = null;
        document.onselectstart = null;
        _dragElement.ondragstart = null;

        // this is how we know we're not dragging      
        _dragElement = null;
         rmDragDrop();
    }
  
}

function Scale(object){
	if(!_dragElement)
	 var _dragElement = document.getElementById(object);
     
	 document.onselectstart = function () { return false; };
    _dragElement.ondragstart = function() { return false; };
	 
    document.onmousemove = function(e){  	
    	if (e == null) 
    		var e = window.event; 
    	
		_dragElement.style.width = (_offsetX + e.clientX - _startX) + 'px';
		_dragElement.style.height = (_offsetY + e.clientY - _startY) + 'px';
    }
	
	document.onmouseup = function(){
		 document.onmousemove = null;
	}
}

function ExtractNumber(value)
{
    var n = parseInt(value);
	
    return n == null || isNaN(n) ? 0 : n;
}

// this is simply a shortcut for the eyes and fingers
function $(id)
{
    return document.getElementById(id);
}