
//  This drag-and-drop logic is slightly modified and cleaned up from
//  the excellent JavaScript in a tutorial by Luke Breuer.
//  Thank you, Luke!
//  John Oberschelp

var debug_mode = false;
if (debug_mode) document.writeln('<pre id="debug">(debug mode)</pre>');

var mouse_start_X = 0;
var mouse_start_Y = 0;

var element;
var element_X = 0;
var element_Y = 0;
var element_Z = 0;

document.onmousedown = our_onmousedown;
document.onmouseup = our_onmouseup;
//---------------------------------------------------
function $(id) { return document.getElementById(id); }
//---------------------------------------------------
function debug_display(m) { if (debug_mode) $('debug').innerHTML = m; }
//---------------------------------------------------
function int_from_string(value)// (ex) 100px
{
	var n = parseInt(value);
	return n == null || isNaN(n) ? 0 : n;
}
//---------------------------------------------------
function our_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 = e.target != null ? e.target : e.srcElement;

	debug_display((target.className == 'drag' ? '':'NON-')+'draggable element clicked');






	var ctrlPressed=0;
	var altPressed=0;
	var shiftPressed=0;

	if (parseInt(navigator.appVersion) > 3) {
		
//		var evt = navigator.appName=="Netscape" ? e:event;
var evt = e;		
		if (navigator.appName=="Netscape" && parseInt(navigator.appVersion)==4) {
			// NETSCAPE 4 CODE
			var mString =(e.modifiers+32).toString(2).substring(3,6);
			shiftPressed=(mString.charAt(0)=="1");
			ctrlPressed =(mString.charAt(1)=="1");
			altPressed  =(mString.charAt(2)=="1");
			//self.status="modifiers="+e.modifiers+" ("+mString+")"
		}
		else {
			// NEWER BROWSERS [CROSS-PLATFORM]
			shiftPressed=evt.shiftKey;
			altPressed  =evt.altKey;
			ctrlPressed =evt.ctrlKey;
			//self.status=""
			//+  "shiftKey="+shiftPressed 
			//+", altKey="  +altPressed 
			//+", ctrlKey=" +ctrlPressed 
		}
	}








	// For IE, left click == 1.
	// For Firefox, left click == 0.
	if (	(e.button == 1 && window.event != null || e.button == 0)
		&&	target.className == 'drag'
//		&&	ctrlPressed
	)
	{
	
	
		if (ctrlPressed)
		{
			// Grab the mouse position.
			mouse_start_X = e.clientX;
			mouse_start_Y = e.clientY;
			
			// Grab the clicked element's position.
			element_X = int_from_string(target.style.left);//assumes using px
			element_Y = int_from_string(target.style.top );//assumes using px
			element_Z = target.style.zIndex;
			
			// Bring the clicked element to the front while it is being dragged.
			target.style.zIndex = 10000;
			
			// We need to access the element in our_onmousemove.
			element = target;

			// Tell our code to start moving the element with the mouse.
			document.onmousemove = our_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;
		}
//		alert(target.id);
		document.location.href = target.id;
		
	}
}
//---------------------------------------------------
function our_onmousemove(e)
{
	if (e == null) 
		var e = window.event; 

	// This is the actual drag code.
	element.style.left = (element_X + e.clientX - mouse_start_X) + 'px';
	element.style.top  = (element_Y + e.clientY - mouse_start_Y) + 'px';

	debug_display('(' + element.style.left + ', ' + element.style.top + ')');	
}
//---------------------------------------------------
function our_onmouseup(e)
{
	if (element != null)
	{
		element.style.zIndex = element_Z;

		// We're done with these events until the next our_onmousedown.
		document.onmousemove = null;
		document.onselectstart = null;
		element.ondragstart = null;

		// This is how we know we're not dragging.
		element = null;
	}
	debug_display('mouse up');
}
//---------------------------------------------------
