/* utility.js
 * A few things to make using Javascript easier, by Eric Poggel */


// A shortened form of document.getElementById(id);
function getElem(id)
{	return document.getElementById(id);
}

// Alias of getElem()
function getId(id)
{	return document.getElementById(id);
}

// Set the visibility of an element
function setVisible(elem, status)
{	if (status)
		elem.style.display ='';
	else elem.style.display ='none';
}

// get the visibility of an element
function getVisible(elem)
{	return elem.style.display!='none'
}

// Hopefully this will one day cause things to slide in and out,
// but for now it just toggles visibility.
function slide(elem)
{	setVisible(elem, !getVisible(elem));
}


// Toggle an element to fade in or out
// Set style="display: none" to set initially faded out
// Containing elements of text must have background-color to work in IE
function fade(elem)
{	var was_invisible = elem.style.display=='none';
	var effect = new fx.Opacity(elem , {duration: 500, onComplete: function()
	  	{	if (elem.style.opacity ==0)
	  			elem.style.display = 'none';
	  	}
	});	
	if (was_invisible)
	{	elem.style.display = '';
		effect.setOpacity(0);
		effect.custom(0, 1);
	}else
		effect.custom(1, 0);
}

// Used internally to record the position of the mouse.
var __mousex = 0;
var __mousey = 0;
function getMouseVars(e) // works on IE6,FF,Moz,Opera7
{	if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)	
	if (e)
	{	if (e.pageX || e.pageY)	// this doesn't work on IE6!! (works on FF,Moz,Opera7)
		{ 	__mousex = e.pageX + document.body.scrollLeft;
			__mousey = e.pageY + document.body.scrollTop;
		}
		else if (e.clientX || e.clientY)// works on IE6,FF,Moz,Opera7
		{	__mousex = e.clientX + document.body.scrollLeft;
			__mousey = e.clientY + document.body.scrollTop;
		}  
	}
}

// Must be a better way?
window.onload = function() {
	document.onmousemove=getMouseVars;
}

function getMouseX()
{	document.onmousemove=getMouseVars;
	getMouseVars();
	return __mousex;
}

function getMouseY()
{	document.onmousemove=getMouseVars;
	getMouseVars();
	return __mousey;
}


// Get the X and Y position of an element in abs screen coordinates
// This doesn't quite work in IE?
function findPosX(elem)
{	var dist = 0;
	while (elem.offsetParent)
	{	dist += elem.offsetLeft;
		elem = elem.offsetParent;
	}
	return dist;
}

function findPosY(elem)
{	var curtop = 0;
	if (elem.offsetParent)
	{	while (elem.offsetParent)
		{	curtop += elem.offsetTop
			elem = elem.offsetParent
	}	}
	else if (elem.y)
		curtop += elem.y;
	return curtop;
}


function isArray(a)
{	return isObject(a) && a.constructor == Array;
}

function isBoolean(a)
{	return typeof a == 'boolean';
}

// isEmpty(a) returns true if a is an object or array or function containing no enumerable members.
function isEmpty(o)
{	var i, v;
	if (isObject(o))
	{	for (i in o)
		{	v = o[i];
			if (isUndefined(v) && isFunction(v))
				return false;			
		}
	}
	return true;
}

function isFunction(a)
{	return typeof a == 'function';
}

function isNull(a)
{	return typeof a == 'object' && !a;
}

function isNumber(a)
{	return typeof a == 'number' && isFinite(a);
}

function isObject(a)
{	return (a && typeof a == 'object') || isFunction(a);
}

function isString(a)
{	return typeof a == 'string';
}

/* isUndefined(a) returns true if a is the undefined value.
 * You can get the undefined value from an uninitialized variable or from a missing member of an object.*/
function isUndefined(a)
{	return typeof a == 'undefined';
} 