
var grabx = 0;
var graby = 0;
var orix = 0;
var oriy = 0;
var elex = 0;
var eley = 0;

var dragobj = null;

// Empty Variables to hold the mouse position and the window size
var mousePos = null;
var winSize = null;

// Set events to catch mouse position and window size
document.onmousemove = mouseMove;
window.onresize = windowResize;

var isDrag = false;

function falsefunc() { return false; } // used to block cascading events


function grab(context)
{
  document.onmousedown = falsefunc; // in NS this prevents cascading of events, thus disabling text selection
  dragobj = context;
  dragobj.style.zIndex = 10; // move it to the top
  document.onmousemove = drag;
  document.onmouseup = drop;
  grabx = mousePos.x;
  graby = mousePos.y;
  orix = dragobj.offsetLeft;
  oriy = dragobj.offsetTop;
  elex = orix;
  eley = oriy;
  mouseMove();
}

function drag(e) // parameter passing is important for NS family
{
  if (dragobj)
  {
    isDrag = true;
    elex = orix + (mousePos.x-grabx);
    eley = oriy + (mousePos.y-graby);
    dragobj.style.position = "absolute";
    dragobj.style.left = (elex).toString(10) + 'px';
    dragobj.style.top  = (eley).toString(10) + 'px';
  }
  mouseMove(e);
  return false; // in IE this prevents cascading of events, thus text selection is disabled
}

function drop()
{
  if (dragobj)
  {
    dragobj.style.zIndex = 0;
    dragobj = null;
  }
  mouseMove();
  document.onmousemove = mouseMove;
  document.onmouseup = null;
  document.onmousedown = null;   // re-enables text selection on NS
}




// The mouseMove and mouseCoords function track the mouse position for us
function mouseMove(ev)
{
	ev = ev || window.event;
	mousePos = mouseCoords(ev);
}
function mouseCoords(ev)
{


	if(ev.pageX || ev.pageY){
		return {x:ev.pageX, y:ev.pageY};
	}
	if (!ev) ev = window.event;



	xx = ev.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
	yy = ev.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	//if (mousePos != null && xx - mousePos.x > 10)	{
		//alert("1, 2: " + ev.clientX + ", " + document.body.scrollLeft +  ", " + document.documentElement.scrollLeft + ", " + xx);
	//}
	return {
		x: xx,
		y: yy
	};
}

// The windowResize function keeps track of the window size for us
function windowResize( )
{
var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }



	winSize = {
		x: myWidth,
		y: myHeight
	}
}

// This function shows our tool-tips
function showTip(anchor)
{
	var tip = document.getElementById('t'+anchor.id);
	tip.style.position = "absolute";
	var newTop = mousePos.y - tip.clientHeight - 10;
	var newLeft = mousePos.x - ( tip.clientWidth / 2 );
	if( newTop < 0 )
		newTop = mousePos.y + 20;
	if( newLeft < 0 )
		newLeft = 0;
	//if( ( mousePos.x + ( tip.clientWidth / 2 ) ) >= winSize.x - 1 )
	//	newLeft = winSize.x - tip.clientWidth - 2;

	var elem = document.getElementById("page");



	tip.style.top = newTop + "px";
	tip.style.left = (newLeft - elem.offsetLeft) + "px";
	tip.style.display = "block";
}


// This function hides the tool-tips
function hideTip(anchor)
{
	var tip = document.getElementById('t'+anchor.id);
	tip.style.display = "none";
}
