/* * * * * * * * * * * * * * * * * * * * * * * * *
 *	 Seir Anphin JavaScript Utility Library	     *
 * Copyright Darrel Grant unless otherwise noted *
 *			  http://www.anphin.com	             *
 * * * * * * * * * * * * * * * * * * * * * * * * */

// Provide getElementById shorthand
function $(id) {
	if (!document.getElementById) {
		document.getElementById = function(id) {
			if (document.all) {
				return document.all[id];
			} else if (document.layers) {
				return document.layers[id];
			}
		}
	} else {
		return document.getElementById(id);
	}
}

/*		 "ultimate" getElementsbyClassName()
	Written by Jonathan Snook, http://www.snook.ca/jonathan
	Add-ons by Robert Nyman, http://www.robertnyman.com
*/
function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}



sa2 = {
	// Holds an element ID to be set as "off" when using ajax menues
	oldLinkId:'',

	// Add a function to be run when the current page has finished rendering
	onLoad : function(func) {
		var oldonload = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = func;
		} else {
			window.onload = function() {
				// All functions previously set to execute at window.onload
				// will do so before this one.
				oldonload();
				func();
			}
		}
	},

	copyInnerHTML : function(layerFrom, layerTo, linkId) {
		if (linkId != '') {
			if (this.oldLinkId != '') {
				this.setClassName(this.oldLinkId, 'menu_item_off');
			}
			this.setClassName(linkId, 'menu_item_on');
			this.oldLinkId = linkId;
		}

		src = $(layerFrom);
		dst = $(layerTo);
		dst.innerHTML = src.innerHTML;
	},

	swapInnerHTML : function(layer1, layer2) {
		src = $(layer1);
		dst = $(layer2);
		tmp = '';
		tmp = dst.innerHTML;
		dst.innerHTML = src.innerHTML;
		src.innerHTML = tmp;
	},

	// Checks if the element id exists and sets the classname
	setClassName : function(id, className) {
		var element;
		if (element = $(id)) {
			element.className = className;
		}
	},

	// Appends a classname, ensuring there are no leading or trailing slashes
	appendClassName : function(id, newClassName) {
		var element;

		if (typeof id == "string") {
			// "id" is a string
			element = $(id);

		} else {
			// id is already an object
			element = id;
		}

		if (element.className != '') {
			element.className += " " + newClassName;
		} else {
			element.className = newClassName;
		}
	},

	removeClassName : function(id, thisClassName) {
		var element;

		if (typeof id == "string") {
			// "id" is a string
			element = $(id);

		} else {
			// id is already an object
			element = id;
		}

		if (element.className == thisClassName) {
			element.className = '';
		} else {
			element.className  = element.className.replace(' ' + thisClassName, '');
		}
	},

	// Add support for popup menus where CSS :hover pseudo selectors
	// aren't supported (looking at you IE)
	init_menus : function() {
		var elements = getElementsByClassName(document, 'LI', 'popup_menu');
		for (var i=0; i<elements.length; i++) {
			dom.addEvent(elements[i], 'mouseover', sa2.menuShow, false);
			dom.addEvent(elements[i], 'mouseout', sa2.menuHide, false);
		}
	},

	menuShow : function(e) {
		sa2.appendClassName(e.target, 'popupclass');
	},
	menuHide : function(e) {
		sa2.removeClassName(e.target, 'popupclass');
	}
}

sa2.onLoad(sa2.init_menus);

