///////////////////////////////////////////////////////////////////////
//     This script was designed by Erik Arvidsson for WebFX          //
//                                                                   //
//     For more info and examples see: http://webfx.eae.net          //
//     or send mail to erik@eae.net                                  //
//                                                                   //
//     Feel free to use this code as lomg as this disclaimer is      //
//     intact.                                                       //
///////////////////////////////////////////////////////////////////////

var checkZIndex = true;

var dragobject = null;
var tx;
var ty;

var ie5 = document.all != null && document.getElementsByTagName != null;

function getReal(e) {      
	
    var el;	
	
	if (isNav)
	{
	    temp = e.target;
	}
	else
	{		    
		temp = event.srcElement;
	}		

	while ((temp != null) && (temp.tagName != "BODY")) {
	    	    
		if ((temp.className == "moveme") || (temp.className == "handle")){
			el = temp;
			
			return el;
		}
		temp = temp.parentNode;
	}		
	
	return el;
}


function moveme_onmousedown(e) {
	el = getReal(e);	
	
	if (el.className == "moveme" || el.className == "handle") {
		if (el.className == "handle") {
			tmp = el.getAttribute("handlefor");			
			
			if (tmp == null) {
				dragobject = null;
				return;
			}
			else
				//dragobject = eval(tmp);
				dragobject = document.getElementById(tmp);
		}
		else 
			dragobject = el;
		
		if (checkZIndex) makeOnTop(dragobject);
		
		
	    var x;
	    var y;
			
	    // Translate the coordinates
	    if (isNav)
	    {
		    x=e.pageX;
		    y=e.pageY;		    		    
	    }
	    else
	    {	
		    //var scroll = getScrollXY();
    	    
    	    e=event;
    	    
		    x=event.clientX; // + scroll[0];
		    y=event.clientY; // + scroll[1];
	    }		
		
		ty = y - getTopPos(dragobject);
		tx = x - getLeftPos(dragobject);				
		
		e.returnValue = false;
		e.cancelBubble = true;
	}
	else {
		dragobject = null;
	}
}

function moveme_onmouseup() {
	if(dragobject) {
		dragobject = null;
	}
}

function moveme_onmousemove(e) {
	if (dragobject) {
	
	    var x;
	    var y;
			
	    // Translate the coordinates
	    if (isNav)
	    {
		    x=e.pageX;
		    y=e.pageY;		    		    
	    }
	    else
	    {	
		    //var scroll = getScrollXY();
    	    
    	    e=event;
    	    
		    x=event.clientX; // + scroll[0];
		    y=event.clientY; // + scroll[1];
	    }		    		     	
	
		if (x >= 0 && y >= 0) {
			dragobject.style.left = x - tx + "px";
			dragobject.style.top = y - ty + "px";
		}			
		
		e.returnValue = false;
		e.cancelBubble = true;
	}
}

function getLeftPos(el) {
	if (ie5) {
		if (el.currentStyle.left == "auto")
			return 0;
		else
			return parseInt(el.currentStyle.left);
	}
	else {
		//return el.style.pixelLeft;
		return parseInt(el.style.left);
	}
}

function getTopPos(el) {  

	if (ie5) {
		if (el.currentStyle.top == "auto")
			return 0;
		else
			return parseInt(el.currentStyle.top);
	}
	else {
		//return el.style.pixelTop;
		return parseInt(el.style.top);
	}
}

function makeOnTop(el) {
	var daiz;
	var max = 0;
	var da = document.all;	
	
	for (var i=0; i<da.length; i++) {
		daiz = da[i].style.zIndex;
		if (daiz != "" && daiz > max)
			max = daiz;
	}
	
	el.style.zIndex = max + 1;
}

//if (document.all) { //This only works in IE4 or better
//	document.onmousedown = moveme_onmousedown;
//	document.onmouseup = moveme_onmouseup;
//	document.onmousemove = moveme_onmousemove;
//}

document.write("<style>");
document.write(".moveme		{cursor: move;}");
document.write(".handle		{cursor: move;}");
document.write("</style>");