// Base Scripts
// Scripts For Events And Mouse Positions
var offsetfrommouse=[15,5];			//object x,y offsets from cursor position in pixels. Enter 0,0 for no offset
var Mouse_XPos;						// x position of mouse cursor
var Mouse_YPos;						// y position of mouse cursor
//===================================
//			All Initials
//===================================
//document object
function getDocumentBody()
{
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat") ? document.documentElement : document.body;
}
//Screen Width
function DocumentWidth()
{
	return document.all? getDocumentBody().scrollLeft + getDocumentBody().clientWidth : pageXOffset + window.innerWidth - 15;
}
//Screen Height
function DocumentHeight()
{
	return document.all? Math.min(getDocumentBody().scrollHeight, getDocumentBody().clientHeight) : Math.min(window.innerHeight);
}

// this function ads an event to a document element
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;
	}
}

//initialize the variables and events
function DocInit()
{
	AddEvent(getDocumentBody(), "mousemove", MonitorMouse);
}

// monitoring the mouse position
function MonitorMouse(e)
{
    var evt = e || window.event; 
    // getting the event target object
    //var obj = evt.target || evt.srcElement 
    
    Mouse_XPos = evt.pageX ? evt.pageX : evt.clientX;
    Mouse_YPos = evt.pageY ? evt.pageY : evt.clientY;
    
}

// move the object near mouse cursor
function MoveToCursor(MObject, objectWidth, objectHeight)
{
	var xcoord=offsetfrommouse[0];
	var ycoord=offsetfrommouse[1];
	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;
	}

	MObject.left=xcoord+"px"
	MObject.top=ycoord+"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"
	}
	

}










