masterHandOverride=false;

function getMasterHand(){
	var masterHand = document.getElementById("masterhand");
	return masterHand;
}

function masterHandMove(e) {	
	try {
		var posx = 0;
		var posy = 0;
		if (!e) var e = window.event;
		if (e.pageX || e.pageY) 	{
			posx = e.pageX;
			posy = e.pageY;
		}
		else if (e.clientX || e.clientY) 	{
			posx = e.clientX + document.body.scrollLeft
				+ document.documentElement.scrollLeft;
			posy = e.clientY + document.body.scrollTop
				+ document.documentElement.scrollTop;
		}
		
		// posx and posy are the position of the mouse
		//window.status = posx+","+posy;
		var masterHand = getMasterHand();
		var xOffset=-10.8;
		var yOffset=-2.6;
		var masterHandLeft = posx+xOffset;
		var masterHandTop = posy+yOffset
		masterHand.style.left=masterHandLeft + 'px';
		masterHand.style.top=masterHandTop + 'px';
		
		//window.status = masterHandLeft+","+masterHandTop;
		
		var windowWidth = document.body.clientWidth;
		var windowHeight  = document.body.clientHeight;
		
		//window.status = windowWidth+","+windowHeight;
		
		var wbuffer = 55;
		var hbuffer = 67;
		var rightEdge = posx + wbuffer;
		var bottomEdge = posy + hbuffer;
		if(rightEdge >= windowWidth || bottomEdge >= windowHeight /* too far right/bottom */
		   || posx < 5 || posy < 5 /* too far left/ top */
		   ) masterHandHide();
		else if(!masterHandOverride) masterHandShow();
		
	}
	catch(e){};
	
}
function masterHandShow(){
	var masterHand = getMasterHand();
	masterHand.style.display = "block";
}
function masterHandHide(){	
	var masterHand = getMasterHand();
	masterHand.style.display = "none";
}
function flashMasterHandEnter(){	
	var masterHand = getMasterHand();		
	masterHand.style.visibility = "hidden";
}
function flashMasterHandExit(){
	var masterHand = getMasterHand();	
	masterHand.style.visibility = "visible";
}

function flashMasterHandHide() {
	document.getElementById("application").masterHandHide();
}

function hookEvent(element, eventName, callback) {
  if(typeof(element) == "string")
    element = document.getElementById(element);
  if(element == null)
    return;
  if(element.addEventListener) {
    if(eventName == 'mousewheel') {
      element.addEventListener('DOMMouseScroll', callback, false); 
    }
    element.addEventListener(eventName, callback, false);
  }
  else if(element.attachEvent)
    element.attachEvent("on" + eventName, callback);
}

function unhookEvent(element, eventName, callback) {
  if(typeof(element) == "string")
    element = document.getElementById(element);
  if(element == null)
    return;
  if(element.removeEventListener) {
    if(eventName == 'mousewheel') {
      element.removeEventListener('DOMMouseScroll', callback, false); 
    }
    element.removeEventListener(eventName, callback, false);
  }
  else if(element.detachEvent)
    element.detachEvent("on" + eventName, callback);
}

function onMouseWheel(e){
	e = e ? e : window.event;
  	var wheelData = e.detail ? e.detail : e.wheelDelta / 40;	

	// hide the masterHand on a scroll wheel event
	// it will show up again when the mouse moves
	masterHandHide();
}

// handle mouse wheel
hookEvent(document, "mousewheel", onMouseWheel);

document.onblur=masterHandHide;
document.onmousemove=masterHandMove;

/** 
hide mousehand when moving over the activity iframe
**/

function hideMouseHandLegacyIFrame(){
	masterHandHide();
	flashMasterHandHide();
}


