// utils.js
// Some standard JavaScript utility functions
// by Greg Poole | greg@webengine.com.au | www.webengine.com.au

// Add a function which is to be called upon a certain event being fired. The process is one of
// essentially stacking events so that more than one can be assigned to an object.
function addEventHandler(obj,event,handler) {
	if(!obj)
		throw new Exception("Cannot add handler for " + event + " to null object.");
	
	if(!obj.__eventStack)
		obj.__eventStack = new Array();
	
	if(!obj.__eventStack[event]) {
		obj.__eventStack[event] = new Array();
		var existingFunc = eval("obj." + event);
		if(existingFunc)
			obj.__eventStack[event][0] = existingFunc;
		eval("obj." + event + "=function() { for(var i=0;i<this.__eventStack['" + event + "'].length;i++) { var tmp = this.__eventStack['" + event + "'][i]; if(tmp) tmp(); } }");
	}
	
	obj.__eventStack[event][obj.__eventStack[event].length] = handler;
}

// Include a javascript file into the document so that it may be used. Note that this function must be called
// after the page has loaded or the code must be placed inside the body tag.
function include(src) {
	if(document.createElement) {
		var inc = document.createElement("script");
		inc.type = "text/javascript";
		inc.src = src;
		document.body.appendChild(inc);
	} else {
		document.write("<scr" + "ipt type=\"text/javascript\" src=\"" + src + "\"></sc" + "ript>");
	}
}

// Generate a random 32-character name
function generateRandomName() {
	var length = 32;
	var name = "";
	for(var l=1;l<=length;l++)
		name += String.fromCharCode((Math.random() * 96) + 32);
	return name;
}

// Add an endswith method to the strign class, to allow for testing against the end of a string.
String.prototype.endsWith = function(string) {
	if(this.length < string.length)
		return false;
	
	var end = this.substring(this.length - string.length);
	if(string == end)
		return true;
	return false;
}

// Allows the setting of a height of a div as a percentge where the height of the parent container is
// not known before-hand.
function fillToPercent(element,percentage) {
	if(element.parentNode && element.parentNode.style)
		element.parentNode.style.height = parseInt(element.parentNode.clientHeight) + "px";
	element.style.height = percentage;
}

// Mouseovers
// Automatic mouseover application script that will apply the effect to all
// images marked by a "#mo" in the name
var p_Images=new Array();
var p_Img_Pointer=0;

function doImageLoad() {
	var images=document.getElementsByTagName("img");
	for(var i=0;i<images.length;i++) {
		if(images[i].name=="#mo") {
			images[i].onmouseover=highlight;
			images[i].onmouseout=unhighlight;
			preload(getMouseOverName(images[i].src));
		}
	}
}
// addEventHandler(window,'onload',doImageLoad); -- Not used here

function preload(image) {
	p_Images[p_Img_Pointer]=new Image();
	p_Images[p_Img_Pointer++].src=image;
}

function highlight(e,img) {
	if(!img)
		img=this;
	img.src=getMouseOverName(img.src);
}

function unhighlight(e,img) {
	if(!img)
		img=this;
	var ext=img.src.substring(img.src.lastIndexOf('.'));
	img.src=getMouseOutName(img.src);
}

function getMouseOverName(name) {
	var ext=name.substring(name.lastIndexOf('.'));
	return name.replace(ext,"_mo"+ext);
}

function getMouseOutName(name) {
	var ext=name.substring(name.lastIndexOf('.'));
	return name.replace("_mo","");
}

// Expand and hide a div
function expand(item) {
	var obj=document.getElementById(item);
	if(!obj)
		return;
	if(obj.style.display!="")
		obj.style.display="";
	else
		obj.style.display="none";
}

// Sniff out the browser version
function getBrowser() {
	var agt=navigator.userAgent.toLowerCase();
	if (agt.indexOf("opera") != -1) return 'Opera';
	if (agt.indexOf("staroffice") != -1) return 'Star Office';
	if (agt.indexOf("webtv") != -1) return 'WebTV';
	if (agt.indexOf("beonex") != -1) return 'Beonex';
	if (agt.indexOf("chimera") != -1) return 'Chimera';
	if (agt.indexOf("netpositive") != -1) return 'NetPositive';
	if (agt.indexOf("phoenix") != -1) return 'Phoenix';
	if (agt.indexOf("firefox") != -1) return 'Firefox';
	if (agt.indexOf("safari") != -1) return 'Safari';
	if (agt.indexOf("skipstone") != -1) return 'SkipStone';
	if (agt.indexOf("msie") != -1) return 'Internet Explorer';
	if (agt.indexOf("netscape") != -1) return 'Netscape';
	if (agt.indexOf("mozilla/5.0") != -1) return 'Mozilla';
	if (agt.indexOf('\/') != -1) {
		if (agt.substr(0,agt.indexOf('\/')) != 'mozilla') 
			return navigator.userAgent.substr(0,agt.indexOf('\/'));
		else
			return 'Netscape';
	} else if (agt.indexOf(' ') != -1)
		return navigator.userAgent.substr(0,agt.indexOf(' '));
	else
		return navigator.userAgent;
}