//////////////////////////////////////////////////////////
/*
Adds some new functionality to existing functions
*/

/*
Removes an element from the array
*/
Array.prototype.remove = function (index) {
	this.splice(index, 1);
}

/*
Searches an array for a certain value and returns the index
*/
Array.prototype.search = function (value) {
	for (var i=0; i<this.length; i++) {
		if (this[i] == value) {
			return i;
		}
	}
}
//////////////////////////////////////////////////////////

/*
@desc Shows/hides an element depengin on display:
@param elemID The elementID
@return null
*/
function showHide(elemID) {
	d = document.getElementById(elemID);
	
	if (d.style.display == 'none') {
		d.style.display = '';
	} else {
		d.style.display = 'none';
	}
}


/*
@desc Finds all elements within a given element with the given classname
@param elem The element to search in
@param className The classname to look for
@return An array of html objects
*/
function getElementsByClass(elem, className) {
	var cArray = []; // Array of elements
	var z = new RegExp('\\b' + className + '\\b'); // RegExp for the classname
	
	function doTree(elem) {
		if (elem.className && z.test(elem.className)){
			cArray.push(elem); // Match found so add
		}
		
		// Loop through each child node of elem
		for (var i=0, len=elem.childNodes.length; i<len; i++) {
			doTree(elem.childNodes[i]);
		}
	}
	
	doTree(elem);
	return cArray;
} 


/*
@desc Checks if an object is an array or not
@param obj The object to check
@return True if an array
*/
function isArray(obj) {
	// Check the initial constructor to see if it is an array
	if (obj.constructor.toString().indexOf("Array") == -1) {
		return false;
	} else {
		return true;
	}
}


/*
@desc Adds hover to an element
*/
function setHover(elem, className) {
	// Get an array of objects with the className
	var o = getElementsByClass(document.getElementById(elem), className);

	// Loop through each object
	for (var i=0; i<o.length; i++) {
		addListener(o[i], "mouseover", function(e) {alert("over");}, false);
		addListener(o[i], "mouseout", function(e) {alert("out");}, false);
	}
}


/*
@desc Adds an event listener to an element
@param element The element to apply it to
@param eventType The event type. ie. click,mouseover
@param expression What to do
@param bubbling Allow the event to 'bubble' up to the next level
@return True if succeed
*/
function addListener(element, eventType, expression, bubbling) {
	bubbling = bubbling || false;

	// If addEventListener is supported
	if (window.addEventListener)	{
		element.addEventListener(eventType, expression, bubbling);
		return true;
	// IE doesnt support it
	} else if (window.attachEvent) {
		element.attachEvent('on' + eventType, expression);
		return true;
	} else {
		return false;
	}
}

