// Global object containing commonoly used funcitons
var g = {
	
	// Helper methods, Add Event Listeners
	getTarget:function(e){
		var target = window.event ? window.event.srcElement : e ? e.target : null;
		if (!target){return false;}
		while(target.nodeType!=1 && target.nodeName.toLowerCase()!='body'){
			target=target.parentNode;
		}
		return target;
	},
	
	
	stopBubble:function(e){
		if(window.event){
			window.event.cancelBubble = true;
		} 
		if (e && e.stopPropagation){
			e.stopPropagation();
		}
	},
		
	stopDefault:function(e){
		if(window.event){
			window.event.returnValue = false;
		} 
		if (e && e.preventDefault){
			e.preventDefault();
		}
	},
	
	cancelClick:function(e){
		if (window.event){
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
		if (e && e.stopPropagation && e.preventDefault){
			e.stopPropagation();
			e.preventDefault();
		}
	},
	
	addEvent: function(elm, evType, fn, useCapture){
		if (elm.addEventListener){
			elm.addEventListener(evType, fn, useCapture);
			return true;
		} else if (elm.attachEvent) {
			var r = elm.attachEvent('on' + evType, fn);
			return r;
		} else {
			elm['on' + evType] = fn;
		}
	},
	
    safariClickFix:function(){
      return false;
    },
	
	addClass:function(target, classValue) {
		var pattern = new RegExp("(^| )" + classValue + "( |$)");
		if (!pattern.test(target.className)) {
			if (target.className == "") {
				target.className = classValue;
			} else {
				target.className += " " + classValue;
			}
		}
		return true;
	},

	removeClass:function(target, classValue) {
		var removedClass = target.className;
		var pattern = new RegExp("(^| )" + classValue + "( |$)");
		removedClass = removedClass.replace(pattern, "$1");
		removedClass = removedClass.replace(/ $/, "");
		target.className = removedClass;
		return true;
	},
	
	// Add Load 
	addLoadEvent:function(func) {

		var oldonload = window.onload;				
		if (typeof window.onload != 'function') {	
			window.onload = func;
			} else {
			window.onload = function() {
				oldonload();
				func();
			}
		}
	},
	
	// Insert After
	insertAfter:function (newElement, targetElement) {
		var parent = targetElement.parentNode;
		if (parent.lastChild == targetElement) {
			parent.appendChild(newElement);
		} else {
			parent.insertBefore(newElement, targetElement.nextSibling);
		}
	},

	// Get Parent until UL
	
	getPrevSib:function(node) {
		if ( node.previousSibling.nodeType == 1 ) {
			return node.previousSibling;
		}
		if ( node.previousSibling.nodeName != 1 ) { 
			return g.getPrevSib(node.previousSibling);
		}
		return null;
	},	

	// Get Parent until UL
	getParentUL:function(node) {
		if ( node.nodeName == "UL" ) {
			return node;
		}
		if ( node.nodeName != "UL" ) { 
			return g.getParentUL(node.parentNode);
		}
		return null;
	},
	
	// Get next sibling until DD
	getNextSib:function(node, toggleElement) {
		//alert(node.id + " " +toggleElement)
		//alert(node.nodeName)
		
		if ( node.nextSibling.nodeName == toggleElement) {
			return node.nextSibling;
		}
		if ( node.nextSibling.nodeName != toggleElement) { 
			return g.getNextSib(node.nextSibling, toggleElement);
		}
		return null;
		
		
	},	
		
	// Get elements by their Id
	id:function(strId){
		return document.getElementById(strId);
	},	
	
	// Get elements by their tag name
	tn:function(strId){
		return document.getElementsByTagName(strId);
	},
	
	// Get tags within a certain element
	idTn:function(strId, strTn){
		return document.getElementById(strId).getElementsByTagName(strTn);
	},
	
	// Create and element
	ce:function(strId){
		return document.createElement(strId);
	},
	
	// Create an element with an attribute
	ceA:function(strId, ele, eleName){
		var newEle = document.createElement(strId);
		newEle.setAttribute(ele, eleName);
		return newEle;
	},
	
	// Create an element with a className
	ceCn:function(strId, cName){
		var newEle = document.createElement(strId);
		newEle.className = cName;
		return newEle;
	},
	
	// Create element with an attribue and className
	ceACn:function(strId, ele, eleName, cName){
		var newEle = document.createElement(strId);
		newEle.setAttribute(ele, eleName);
		newEle.className = cName;
		return newEle;
	},
	
	/* Wrap all children in a surrounding wrapper */
	elementWrap:function(container) {
	var content = container.childNodes;
	var contentArray = new Array();
	for (var i = 0; i < content.length; i++) {
		if (content[i].nodeType == 1) {
			contentArray.push(content[i]);
		}
	}
	return contentArray;
	}

}	


/* Global variables for IE mac detection */
	var agt = navigator.userAgent.toLowerCase();
	var is_mac = (agt.indexOf("mac")!=-1);
	var is_ie  = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));

	

	
	function getElementsByClassName(oElm, strTagName, oClassNames){
		var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
		var arrReturnElements = new Array();
		var arrRegExpClassNames = new Array();
		if(typeof oClassNames == "object"){
			for(var i=0; i<oClassNames.length; i++){
				arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)"));
			}
		}
		else{
			arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames.replace(/\-/g, "\\-") + "(\\s|$)"));
		}
		var oElement;
		var bMatchesAll;
		for(var j=0; j<arrElements.length; j++){
			oElement = arrElements[j];
			bMatchesAll = true;
			for(var k=0; k<arrRegExpClassNames.length; k++){
				if(!arrRegExpClassNames[k].test(oElement.className)){
					bMatchesAll = false;
					break;                      
				}
			}
			if(bMatchesAll){
				arrReturnElements.push(oElement);
			}
		}
		return (arrReturnElements)
	}
	
	if(typeof Array.prototype.push != "function"){
		Array.prototype.push = ArrayPush;
		function ArrayPush(value){
			this[this.length] = value;
		}
	}


