
/********************************************************************************
 * Common variables
 ********************************************************************************/

var cursorX = 0; var cursorY = 0;		// Cursor positioning vars


/********************************************************************************
 * function addEvent
 ********************************************************************************
 *
 * Description
 * ---------------
 * Helper function for attaching events to elements, removing the need to do 
 * browser-related checks everywhere.
 *
 * @param element: 	Element to attach this event to
 * @param event: 	Event trigger name to attach to
 * @param efunc: 	Function to attach to event
 * @param capture: 	Whether to capture or not for supported browsers
 *
 ********************************************************************************/

function addEvent(element, event, efunc, capture) {
	// If attachEvent exists, deal with IE, otherwise we're Gecko goodness
	if ( element.attachEvent ) {
		element.attachEvent("on" + event, efunc);
    } else if ( element.addEventListener ) {
		element.addEventListener(event, efunc, capture);
	}
}



/********************************************************************************
 * function getElementsByClass
 ********************************************************************************
 *
 * Description
 * ---------------
 * Helper function for getting elements within a node tree that use the specified 
 * class name
 *
 * @param getclass: 	Class name to return elements for
 * @param element: 		Element to search within, whole document assumed as default
 *
 * @return array:		Array of elements containing the specified class
 *
 ********************************************************************************/

function getElementsByClass(getclass, element) {
	var cElems = new Array();
	if ( element == null )
		element = document;
	var elems = element.getElementsByTagName('*');
	var pattern = new RegExp("(^|\\s)" + getclass + "(\\s|$)");
	for (i = 0, j = 0; i < elems.length; i++) {
		if (pattern.test(elems[i].className) ) {
			cElems[j] = elems[i];
			j++;
		}
	}
	return cElems;
}



/********************************************************************************
 * function setCookie
 ********************************************************************************
 *
 * Description
 * ---------------
 * Sets a cookie via JS
 *
 * @param cookie: 		Name of the cookie to set
 * @param value: 		Value to set for the cookie
 * @param expireDays:	Number of days before the cookie expires
 *
 ********************************************************************************/

function setCookie(cookie, value, expireDays) {
	var today = new Date();
	var expire = new Date();
	if (expireDays==null || expireDays==0) expireDays=1;
	expire.setTime(today.getTime() + 3600000 * 24 * expireDays);
	document.cookie = cookie + "=" + escape(value) + ";expires="+expire.toGMTString();
}



/********************************************************************************
 * function getCookie
 ********************************************************************************
 *
 * Description
 * ---------------
 * Gets a cookie value via JS
 *
 * @param cookie: 		Name of the cookie to set
 *
 * @return string:		Value contained within the specified cookie, if anything
 *
 ********************************************************************************/

function getCookie(cookie) {
	if ( document.cookie.length > 0 ) {
		c_start=document.cookie.indexOf(cookie + "=");
		
		if (c_start!=-1) { 
			c_start=c_start + cookie.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			
			if (c_end==-1) 
				c_end=document.cookie.length;
				return unescape(document.cookie.substring(c_start,c_end));
			} 
		}
	return "";
}



/********************************************************************************
 * function getX
 ********************************************************************************
 *
 * Description
 * ---------------
 * Gets the x-position of the supplied element
 *
 * @param obj: 			Element to get position for
 *
 * @return int:			x-position of the supplied element
 *
 ********************************************************************************/

function getX(obj) {
	var curleft = 0;

	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	} else if (obj.x)
		curleft += obj.x;

	return curleft;
}



/********************************************************************************
 * function getY
 ********************************************************************************
 *
 * Description
 * ---------------
 * Gets the y-position of the supplied element
 *
 * @param obj: 			Element to get position for
 *
 * @return int:			y-position of the supplied element
 *
 ********************************************************************************/

function getY(obj) {
	var curtop = 0;

	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}  else if (obj.y)
		curtop += obj.y;
	return curtop;
}



/********************************************************************************
 * function getCursorX
 ********************************************************************************
 *
 * Description
 * ---------------
 * Gets the x-position of the cursor
 *
 * @param obj: 			Element to get position for
 *
 * @return int:			x-position of the cursor
 *
 ********************************************************************************/

function getCursorX(obj) {
	obj = obj || window.event;
	if (obj.pageX) {
		return obj.pageX;
	} 
	else {
		var de = document.documentElement;
		var b = document.body;
		return (obj.clientX + (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0));
	}
}



/********************************************************************************
 * function getCursorY
 ********************************************************************************
 *
 * Description
 * ---------------
 * Gets the y-position of the cursor
 *
 * @param obj: 			Element to get position for
 *
 * @return int:			y-position of the cursor
 *
 ********************************************************************************/

function getCursorY(obj) {
	obj = obj || window.event;
	if (obj.pageY) {
		return obj.pageY;
	} 
	else {
		var de = document.documentElement;
		var b = document.body;
		return obj.clientY + (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
	}
}



/********************************************************************************
 * function getCSSRule
 ********************************************************************************
 *
 * Description
 * ---------------
 * Retrieves a CSS Rule from the DOM
 *
 * @param identifier: 	Identifier of the rule you are searching for
 *
 * @return cssRule:		cssRule object if found, null if not
 *
 ********************************************************************************/

function getCSSRule(identifier) { 
	var cssRules = new Array();
	var cssRule = new Array();
	cssRules = getCSSRules();
	
	
	for ( key in cssRules ) {
		// Find our #main style
		
		if ( cssRules[key].selectorText == identifier ) {
			return cssRules[key];
		}
	}
	
	return null;
}




/********************************************************************************
 * function getCSSRules
 ********************************************************************************
 *
 * Description
 * ---------------
 * Retrieves all CSS Rules from the DOM
 *
 * @return array:		Array of CSS Rules for the first stylesheet
 *
 ********************************************************************************/

function getCSSRules() {
	var cssRules = new Array();
	if (document.styleSheets[0].cssRules)
		cssRules = document.styleSheets[0].cssRules
	else if (document.styleSheets[0].rules)
		cssRules = document.styleSheets[0].rules
		
	return cssRules;
}
