// Global Namespace

var AppGlobal;
if (!AppGlobal) AppGlobal = {};

// Sets a given element to a text value
AppGlobal.setText = function(elem, text) {
	while (elem.firstChild) elem.removeChild(elem.firstChild);
	elem.appendChild(document.createTextNode(text));
}

// Image button rollover/press handler handler
AppGlobal.addRollover = function(img, normalURL, rolloverURL) {
	(new Image()).src = rolloverURL;
	img.onmouseover = function() { img.src = rolloverURL; }
	img.onmouseout = function() { img.src = normalURL; }
	if (arguments.length > 3) {
		var hitURL = arguments[3];
		(new Image()).src = hitURL;
		img.onmousedown = function() { img.src = hitURL; }
		img.onmouseup = function() { img.src = rolloverURL; }
	}
}

// Sets up image rollovers
AppGlobal.setupRolloverImages = function() {
	// Get all of the images on this page and iterate through them to determine if any are rollovers
	var pageImages = document.getElementsByTagName("img");
	for (var x = 0; x < pageImages.length; x++) {
		// Get the base name of the current image
		var thisImage = pageImages[x].src.replace(location.protocol + '//' + location.hostname, '');
		for (var i = 0; i < arguments.length; i++) {
			if (thisImage.indexOf('/' + arguments[i] + '/') != -1 && thisImage.indexOf('_dis.') == -1) {
				// Preload rollover image
				var normalURL = thisImage;
				var rolloverURL = thisImage.replace('.', '_over.');
				// Setup rollover events
				AppGlobal.addRollover(pageImages[x], normalURL, rolloverURL);
			}
		}
	}
	// Get all of the image buttons on this page and iterate through them to determine if any are rollovers
	var inputElements = document.getElementsByTagName("input");
	for (var x = 0; x < inputElements.length; x++) {
		if (inputElements[x].getAttribute("type") == "image") {
			var thisImage = inputElements[x].src.replace(location.protocol + '//' + location.hostname, '');
			for (var i = 0; i < arguments.length; i++) {
				if (thisImage.indexOf('/' + arguments[i] + '/') != -1 && thisImage.indexOf('_dis.') == -1) {
					// Preload rollover image
					var normalURL = thisImage;
					var rolloverURL = thisImage.replace('.', '_over.');
					// Setup rollover events
					AppGlobal.addRollover(inputElements[x], normalURL, rolloverURL);
				}
			}
		}
	}
}
// Pricing OnLoad directive
AppGlobal.onLoad = function() {
	// Sets up image rollovers
	AppGlobal.setupRolloverImages('navtabs', 'rollovers');
}

// Assign onload parameter
if (window.addEventListener) window.addEventListener("load", AppGlobal.onLoad, false);
else if (window.attachEvent) window.attachEvent("onload", AppGlobal.onLoad);
else window.onload = AppGlobal.onLoad;
