/**
 * Function to add events to objects.
 * @see http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
 * @see http://ejohn.org/projects/flexible-javascript-events/
 * @see http://dean.edwards.name/weblog/2005/12/js-tip1
 * @param {Object} obj object to add the event to
 * @param {String} type event type, like "load", "click"
 * @param {Function} fn the function that should be run when the event fires
 * @param {boolean} capture if true, indicates that the user wishes to initiate capture
 * @member event
 */
function addEvent ( obj, type, fn, capture ) {};
/**
 * Function to remove events from objects.
 * You should use the same parameters as in {@link event#addEvent} to remove the same event listener.
 * @see http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
 * @see http://ejohn.org/projects/flexible-javascript-events/
 * @see http://dean.edwards.name/weblog/2005/12/js-tip1
 * @param {Object} obj object to add the event to
 * @param {String} type event type, like "load", "click"
 * @param {Function} fn the function that should be run when the event fires
 * @param {boolean} capture if true, indicates that the user wishes to initiate capture
 * @member event
 */
function removeEvent ( obj, type, fn, capture ) {};


if (document.addEventListener)
{
	addEvent = function ( obj, type, fn, capture )
	{
		obj.addEventListener( type, fn, capture );
	};
	removeEvent = function ( obj, type, fn, capture )
	{
		obj.removeEventListener( type, fn, capture );
	};
}
else if (document.attachEvent)
{
	addEvent = function( obj, type, fn )
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	};
	removeEvent = function( obj, type, fn )
	{
		obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
		obj["e"+type+fn] = null;
	}
}
else
{
	// sorry, no support!
	addEvent = Function;
	removeEvent = Function;
}

/*
AddEvent Manager (c) 2005 Angus Turnbull http://www.twinhelix.com
Free usage permitted as long as this credit notice remains intact.
*/
// ** REST OF SCRIPT NOT USED HERE **
/**
 * Function to stop normal browser action on events.
 * Function you can call within your event handlers to stop them performing
 * the normal browser action or kill the event entirely.
 * @see http://www.twinhelix.com
 * @param {Event} e the event object
 * @param {boolean} c set to true to cancel event bubbling
 * @member event
 */
function cancelEvent(e, c)
{
 e.returnValue = false;
 if (e.preventDefault) e.preventDefault();
 if (c)
 {
  e.cancelBubble = true;
  if (e.stopPropagation) e.stopPropagation();
 }
};


/**
 * Function to create a browser cookie.
 * @param {String} name name of cookie
 * @param {String} value value of cookie
 * @param {Number} days number of days browser should save the cookie
 * @member cookie
 */
function createCookie (name, value, days)
{
	var expire;
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expire = "; expires="+date.toGMTString();
	}
	else
	{
		expire = "";
	}
	document.cookie = name+"="+value+expire+"; path=/";
}

/**
 * Function to read a browser cookie.
 * @param {String} name name of cookie
 * @member cookie
 * @return value of cookie
 * @type String
 */
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 '';
}

/**
 * Function to erase a browser cookie.
 * @param {String} name name of cookie
 * @member cookie
 */
function eraseCookie(name)
{
	createCookie(name,"",-1);
}

/**
 * Function that reads the text content of a DOM node.
 * IE FIX for .textContent
 * @param {Node} node DOM node to get text content from
 * @return text content of node
 * @type String
 */
function getTextContent (node)
{
	if (typeof node.textContent != "undefined")
	{
		return node.textContent;
	}
	else
	{
		return node.innerText;
	}
}

/**
 * String function that removes whitespace from the start and end of a string.
 * @param {String} s the string to use
 * @return the string with whitespace removed
 * @type String
 */
function trim(s)
{
	return s.replace(/^\s*|\s*$/g, "");
}

/**
 * Function to swap, remove or add a class name on an object.
 * @param {HTMLElement} el the object to work on
 * @param {String} oldstr the old class -- set to null to add class
 * @param {String} newstr the new class -- leave empty to remove class
 */
function swapClasses (el, oldstr, newstr)
{
	if (el.className.length === 0)
	{
		el.className = newstr ? newstr : "";
		return;
	}
	var arr = el.className.split(" ");
	for (var i=0, t; t=arr[i]; i++)
	{
		if (t === oldstr)
		{
			if (newstr)
			{
				arr[i] = newstr;
				el.className = arr.join(" ");
				return;
			}
			else
			{
				delete arr[i];
				el.className = arr.join(" ");
				return;
			}
		}
		else if (t === newstr)
		{
				el.className = arr.join(" ");
				return;
		}
	}
	if (newstr) arr.push(newstr);
	el.className = arr.join(" ");
}
