//declare some global vars
var offset_x = 0;
var offfset_y = 0;
var offset_known = false;
var popup_obj = null;
var mouse_is_down = false;
document.onmouseup = function() { mouse_is_down = false; }

function setCurser(cursorName){
 document.body.style.cursor = cursorName;	
}

function moveWindow(elementID){
mouse_is_down = true;	
document.onselectstart=function(){return false;} // yes, i know, this only works in IE
popup_obj = document.getElementById(elementID);
document.onmousemove = mouseMove;
}

function dropWindow(){
 document.onmousemove = null;
 document.onselectstart=function(){return true;} // IE only
 //document.body.style.cursor = "default";
 offset_known = false;
 popup_obj = null;
}

function mouseMove(ev){
	ev           = ev || window.event;
	var mousePos = mouseCoords(ev);
	
    if(!mouse_is_down){
	    dropWindow();
	    return false;
    }

   if(!offset_known){
	 //original X,Y mouse position when user first clicks down to drag
	 startX = mousePos.x;
	 startY = mousePos.y;

       //make sure pointer is still over the clickable area.
	   if(startX < (popup_obj.offsetLeft + 1) || 
		  startY < (popup_obj.offsetTop + 0) 
		  ){
		dropWindow();
		return false;
	   }

     //caclulate the X,Y distance of the pointer from the top-left corner of the element.
	 //   offsetLeft and offsetTop are the X,Y cords of the top-left corner of the element
	 offset_x = startX - popup_obj.offsetLeft;
	 offset_y = startY - popup_obj.offsetTop;
	 offset_known = true;
     }
	 
	 
	//find the new X,Y position of the element by using the current mouse position less the above calced distance of the pointer from the top left.
	var x = mousePos.x - offset_x;
    var y = mousePos.y - offset_y;

	if (x < 0){ x = 0; }
	if (y < 0){ y = 0; }
	popup_obj.style.position = "absolute";
	popup_obj.style.left = x+'px';
	popup_obj.style.top = y+'px';
	
}

function mouseCoords(ev){

    if(ev.pageX || ev.pageY){
		return {x:ev.pageX, y:ev.pageY};
	}

	return {
		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop  - document.body.clientTop
	};
}