// 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(this); } }");
	}
	
	obj.__eventStack[event][obj.__eventStack[event].length] = handler;
}

// 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;
}

// 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>");
	}
}

// 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;
}

// Gets the absolute top offset within the HTML document of the specified HTML element
function getAbsOffsetTop(obj) {
	var top = parseInt(obj.offsetTop);
	while(obj.offsetParent != null) {
		top += parseInt(obj.offsetParent.offsetTop);
		obj = obj.offsetParent;
	}
	return top;
}

// Gets the absolute left offset within the HTML document of the specified HTML element
function getAbsOffsetLeft(obj) {
	var left = parseInt(obj.offsetLeft);
	while(obj.offsetParent != null) {
		left += parseInt(obj.offsetParent.offsetLeft);
		obj = obj.offsetParent;
	}
	return left;
}

// 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);

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","");
}

// Anti-Activate script which removes the "border" and activate click which IE6 and 7 now display
// around activex controls (mainly flash)
function ieActivateFix() {
	var nav = navigator.userAgent;
	var iever = nav.indexOf("MSIE");
	if((iever > 0) && (parseInt(nav.charAt(iever+5)) > 5) ) {
		var search = new Array("object","embed","applet");
		for(j=0; j < search.length; j++) {
			var elems = document.getElementsByTagName(search[j]);
			for(i=0; i < elems.length; i++) {
				var parent = elems[i].parentNode;
				var html = parent.innerHTML;
				parent.removeChild(elems[i]);
				parent.innerHTML = html;
			}
		}
	}
}

// Additional useful date functions

// Get the string representation of the day of the week
Date.prototype.getDayOfWeek = function() {
	var daysOfWeek = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
	return daysOfWeek[this.getDay()];
}

// Get the ordinal suffix for the day of the month
Date.prototype.getDateSuffix = function() {
	var date = this.getDate();
	switch((date >= 14) ? date % 10 : date) {
		case 1:
			return "st";
		case 2:
			return "nd";
		case 3:
			return "rd";
		case 11:
		case 12:
		case 13:
		default:
			return "th";
	}
}

// Get the string representation of the month
Date.prototype.getMonthName = function() {
	var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
	return months[this.getMonth()];
}

// 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;
}
