﻿var Mouse_XPos;
var Mouse_YPos;
function getDocumentBody()
{
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}
function DocumentWidth()
{
	return document.all? getDocumentBody().scrollLeft + getDocumentBody().clientWidth : window.innerWidth - 15;
}
function DocumentHeight()
{
	return document.all? Math.min(getDocumentBody().scrollHeight, getDocumentBody().clientHeight) : Math.min(window.innerHeight);
}

function AddEvent(ObjectVar, EventName, EventFunction)
{
	if (ObjectVar.addEventListener)
	{
		ObjectVar.addEventListener(EventName, EventFunction, true);
		return true;
	}
	else if (ObjectVar.attachEvent)
	{
		var r = ObjectVar.attachEvent("on"+EventName, EventFunction);
		return r;
	}
	else
	{
		return false;
	}    
}

function RemoveEvent(ObjectVar, EventName, EventFunction)
{
	if (ObjectVar.removeEventListener)
	{
		ObjectVar.removeEventListener(EventName, EventFunction, false);
		return true;
	}
	else if (ObjectVar.detachEvent)
	{
		var r = ObjectVar.detachEvent("on"+EventName, EventFunction);
		return r;
	}
	else
	{
		return false;
	}
}

function DocInit()
{
	AddEvent(getDocumentBody(), "mousemove", MonitorMouse);
}

function MonitorMouse(e)
{
    var evt = e || window.event; 
    Mouse_XPos = evt.pageX ? evt.pageX : evt.clientX;
    Mouse_YPos = evt.pageY ? evt.pageY : evt.clientY;
}

function MoveToCursor(MObject, objectWidth, objectHeight)
{
	
	var xcoord='20';
	var ycoord='20';
	var docwidth = DocumentWidth();
	var docheight = DocumentHeight();
	if (docwidth - Mouse_XPos < objectWidth + 2*offsetfrommouse[0]){
		xcoord = Mouse_XPos - xcoord - objectWidth; // Move to the left side of the cursor
	} else {
		xcoord += Mouse_XPos;
	}
	if (docheight - Mouse_YPos < (objectHeight + 2*offsetfrommouse[1])){
		ycoord += Mouse_YPos - 8*ycoord - objectHeight;//Math.max(0,(2*offsetfrommouse[1] + objectHeight + Mouse_YPos - docheight));
	} else {
		ycoord += Mouse_YPos;
	}
	if (typeof window.event != "undefined")
	{
		xcoord += getDocumentBody().scrollLeft;
		ycoord += getDocumentBody().scrollTop;
	}
	if(MObject.left)
	{
		MObject.left=xcoord+"px"
		MObject.top=ycoord+"px"
	}
	else
	{
		
	}
	
}

function movePopupToCenter(ids,objectWidth, objectHeight)
{
	var xcoord = 0;
	var ycoord = 0;
	var docwidth = DocumentWidth();
	var docheight = DocumentHeight();
	var MObject = document.getElementById(ids+'_b');
	var SObject = document.getElementById(ids+'_s');
	
	if(!objectWidth)
	{
		var objectWidth = MObject.style.width.toLowerCase().replace('px','');
	}
	if(!objectHeight)
	{
		var objectHeight = MObject.style.height.toLowerCase().replace('px','');
	}
	
	xcoord = docwidth/2 - objectWidth/2;
	ycoord = docheight/2 - objectHeight/2;
	
	
	if (typeof window.event != "undefined")
	{
		xcoord += getDocumentBody().scrollLeft;
		ycoord += getDocumentBody().scrollTop;
	}
	if(MObject.style)
	{
		MObject.style.left=xcoord+"px"
		SObject.style.left=xcoord+8+'px';
		
		MObject.style.top=ycoord+"px"
		SObject.style.top=ycoord+8+'px';
	}
	else
	{
		MObject.left=xcoord+"px"
		SObject.left=xcoord+8+'px';
		
		MObject.top=ycoord+"px"
		SObject.top=ycoord+8+'px';
	}
	


/*

	if(w3c)
	{
		var idb=document.getElementById(ids+'_b');
		var ids=document.getElementById(ids+'_s');
		idb.style.left=x+'px';
		ids.style.left=x+8+'px';
		idb.style.top=y+'px';
		ids.style.top=y+8+'px';
	}
*/
}



function MoveToCenter(MObject, objectWidth, objectHeight)
{
	var xcoord = 0;
	var ycoord = 0;
	var docwidth = DocumentWidth();
	var docheight = DocumentHeight();
	
	if(!objectWidth)
	{
		var objectWidth = MObject.style.width.toLowerCase().replace('px','');
	}
	if(!objectHeight)
	{
		var objectHeight = MObject.style.height.toLowerCase().replace('px','');
	}
	
	xcoord = docwidth/2 - objectWidth/2;
	ycoord = docheight/2 - objectHeight/2;
	
	
	if (typeof window.event != "undefined")
	{
		xcoord += getDocumentBody().scrollLeft;
		ycoord += getDocumentBody().scrollTop;
	}
	if(MObject.style)
	{
		MObject.style.left=xcoord+"px"
		MObject.style.top=ycoord+"px"
	}
	else
	{
		MObject.left=xcoord+"px"
		MObject.top=ycoord+"px"
	}
	

}
DocInit();