var mX;
var mY;
var active_id;
var timeout_id;

var master_enable = null;

function activate_timeout()
{
	if(!master_enable)
		return;
		
	clear_timeout();
	if(active_id)
		timeout_id = setTimeout("now_timeout()", 50);
}

function update_coords()
{
	if(!master_enable)
		return;
		
	if(active_id)
	{
		elmnt = document.getElementById(active_id); 
		elmnt.style.left = mX+10+'px'; 
		elmnt.style.top = mY+10+'px';
	}
}

function clear_timeout()
{
	if(!master_enable)
		return;
		
	if(timeout_id)
	{
		clearTimeout(timeout_id);
		timeout_id = null;
	}
}

function now_timeout()
{
	if(!master_enable)
		return;
		
	if(active_id)
		hide(active_id);
}
			
function UpdateCoords(evt)
{
	mX = mouseX(evt);
	mY = mouseY(evt);
}
			
function mouseX(evt)
{
	if (!evt) evt = window.event;
	if (evt.pageX) return evt.pageX;
	if (evt.clientX) return evt.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
	else return null;
}
			
function mouseY(evt)
{
	if (!evt) evt = window.event;
	if (evt.pageY) return evt.pageY;
	if (evt.clientY) return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
	else return null;
}

function abs_show(id) {
	document.getElementById(id).style.visibility = 'visible';
}

function abs_hide(id) {
	document.getElementById(id).style.visibility = 'hidden';
}
		
function show(id)
{
	if(!master_enable)
		return;
		
	if(active_id != id)
		hide(active_id);

	if(id)
	{
		if((!active_id || active_id != id))
		{
			active_id = id;
			abs_show(id);
		}
		
		clear_timeout();		
		update_coords();
	}
}

function hide(id)
{
	if(!master_enable)
		return;
		
	if(id)
	{
		clear_timeout();
		abs_hide(id);
		active_id = null;
	}
}

function cleanup()
{
	hide(active_id);
}

function enable()
{
	master_enable = 1;
}

document.onmousemove = UpdateCoords;

//cookie functions from http://www.quirksmode.org/js/cookies.html#doccookie
//thanks =)

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}

//end borrowed code

function setDisplayMode(id, cookie_name) {
	var disp = readCookie(cookie_name);
	if(!disp)
		disp = document.getElementById(id).style.display;
	
	createCookie(cookie_name, disp, 30);
	document.getElementById(id).style.display = disp;
}

function toggleDisplayMode(id, cookie_name) {
	var disp = readCookie(cookie_name);

	if(!disp)
		disp = document.getElementById(id).style.display;

	if(disp != 'none') {
		disp = 'none';
	} else {
		disp = 'block';
	}
	
	document.getElementById(id).style.display = disp;
	createCookie(cookie_name, disp, 30);
}

