/*****************************************************************************************
 *
 *							Standard Areeba js functions
 *
 ****************************************************************************************/

// ---------------------------------------------------------------------
// 	EventWorker.addHandler
// 	add the given event handler, preserving existing event handler functions
//  eg/  EventWorker.addHandler('window.onload', setupForms);
// ---------------------------------------------------------------------

// event worker object constructor
function EventWorker(){
	 this.addHandler = EventWorker.addHandler;
}

// event worker static method
EventWorker.addHandler =
 function (eventRef, func) {
	var eventHandlers = eval(eventRef);
	if (typeof eventHandlers == 'function') { // not first handler
		eval(eventRef + " = function(event) {eventHandlers(event); func(event);}");	
	} else { // first handler
		eval(eventRef + " = func;");
	}
 }
 
// ---------------------------------------------------------------------
//	addClass()
//	appends the specified class to the given element
// ---------------------------------------------------------------------
function addClass(theNode, theClass) {
  if (theNode.className != '') {
	theNode.className += ' ' + theClass;
  } else {
	theNode.className = theClass;
  }
}

// ---------------------------------------------------------------------
//	removeClass()
//	removes the specified class to the given element
// ---------------------------------------------------------------------
function removeClass(theNode, theClass) {
  var oldClass = theNode.className;
  var regExp = new RegExp('\\s?'+theClass+'\\b');
  if (oldClass.indexOf(theClass) != -1) {
	theNode.className = oldClass.replace(regExp,'');
  }
}

// ---------------------------------------------------------------------
//							   inArray()
//						   [Port from PHP]
//			   Hunts for a value in the specified array
// ---------------------------------------------------------------------
Array.prototype.inArray = function inArray(needle) {
  for (var i=0; i < this.length; i++) {
	if (this[i] === needle) {
	  return i;
	}
  }
  return false;
}

// ---------------------------------------------------------------------
//							   debug()
//			   Print messages to an onscreen box
// ---------------------------------------------------------------------

function debug(strMessage) {
	var debugBox = document.getElementById('debugBox');
	var msgP = document.createElement('DIV');
	msgP.appendChild(document.createTextNode(strMessage));
	
	if(!debugBox) {
		debugBox = document.createElement('DIV');
		debugBox.style.position = 'absolute';
		debugBox.style.width = '250px';
		debugBox.style.height = '250px';
		debugBox.style.top = '0';
		debugBox.style.backgroundColor = "#FFFFFF";
		debugBox.style.overflow = 'auto';
		debugBox.onclick = function(){this.style.display = 'none';};
		debugBox.id = 'debugBox';
		document.body.appendChild(debugBox);
	}
	debugBox.style.display = 'block';
	debugBox.insertBefore(msgP, debugBox.firstChild);
}


// ---------------------------------------------------------------------
//							   getElementStyle()
//	read the value of the style for the given element.
//  - IEStyleProp is camel cased property name, eg/ margin-left -> marginLeft;
//	- CSSStyleProp is the actual css property name;			
// ---------------------------------------------------------------------

function getElementStyle(elem, IEStyleProp, CSSStyleProp) {
    if (elem.currentStyle) {
        return elem.currentStyle[IEStyleProp];
    } else if (window.getComputedStyle) {
        var compStyle = window.getComputedStyle(elem, "");
        return compStyle.getPropertyValue(CSSStyleProp);
    }
    return "";
}

// ---------------------------------------------------------------------
//							   expandMenus()
// ---------------------------------------------------------------------

function expandMenus(id) {
	if(!document.getElementById) {
		return true;
	}
	
	var menu = document.getElementById(id);
	if(!menu) return true;
	
	var menuItems = menu.getElementsByTagName("a");
	var activeMenuItem;
	
	// set the class of "menusExpanded" on the top level containers direct children
	for(var i=(menu.childNodes.length-1); i>-1; i--) {
		if(menu.childNodes[i].nodeType == 1) {
			addClass(menu.childNodes[i], "menusExpanded");
		}
	}
	
	// find the currently active menu item
	for (var i=(menuItems.length -1); i>-1; i--){
		if (menuItems[i].href == document.location.href) {
			activeMenuItem = menuItems[i];
			break;
		}
	}
	
	// put a class of 'active' on the active menu item, and 'expanded' on it's parent items
	if(activeMenuItem) {
		var parent = activeMenuItem.parentNode;  // our enclosing 'li'
		addClass(parent, 'active');
		while(parent != menu) {
			if(parent.nodeName == "LI") {
				addClass(parent, 'expanded');
			}
			parent = parent.parentNode;
		}
	}
}

function matchingURL(sLeftURL, sRightURL) {
	return sLeftURL.split("#")[0] == sRightURL.split("#")[0];
}
