/*
----------------------------------------------------------------------------------------------
Framework library
Author:   Tribal DDB HK (Garfield Chan)
Created:  4 October 2007

History:

---------------------------------------------------------------------------------------------- */

// class inheritance
Object.prototype.Extends = function(oBaseClass) {
	for (sProperty in oBaseClass) {
		this[sProperty] = oBaseClass[sProperty];
	}
}

// remove leading and trailing whitespace
String.prototype.trim = function() {
	return this.replace( /^\s+/, "" ).replace( /\s+$/, "" );
}

// Array.indexOf(value, begin, strict) - Return index of the first element that matches value
Array.prototype.indexOf = function(v,b,s) {
	for (var i=+b||0, l=this.length; i<l; i++) {
		if (this[i]===v || s && this[i]==v ) {return i;}
	}
	return -1;
};

_isIE = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;

/* Ajax function
---------------------------------------------------------------------------------------------- */

// create the HTTP request object
var xmlHttp;
function getXmlHttpObject() {
	var xmlHttp = null;
	try {
		xmlHttp=new XMLHttpRequest();
	} catch (trymicrosoft) {
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (othermicrosoft) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	if (xmlHttp == null) {
		alert ("Your browser does not support AJAX!");
		return;
	} else {
		return xmlHttp;
	}
}

/* misc. functions
---------------------------------------------------------------------------------------------- */

// create event handler
function addEvent(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;
	}
}

// create event handler for onload
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

// shortcut to select element by ID
function $() {
	var elements = [];
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}

// get arry of elements by tag name or class name
function $$(node,tag,searchClass) {
	var elements = [];
	if (node == null) {node = document;}
	if (tag == null) {tag = '*';}
	var els = node.getElementsByTagName(tag);
	if (searchClass) {
		var elsLen = els.length;
		var pattern = new RegExp("(^|\s)"+searchClass+"(\s|$)");
		for (i=0, j=0; i<elsLen; i++) {
			if (pattern.test(els[i].className)) {
				elements[j] = els[i];
				j++;
			}
		}
	} else {
		elements = els;
	}
	return elements;
}

// popup window
function popPic(url) {
	var npop = window.open(url,'popwin','width='+screen.width+',height='+screen.height*0.8+',menubar=no,status=no,scrollbars=yes,resizable=yes,toolbar=no,location=no,directories=no');
}

// function to change images when mouse over
function ChangeImageOver() {
	if (document.images)
		var cate = (arguments[1]) ? "_" + arguments[1] : "";
		document.getElementById(arguments[0]).src = eval(arguments[0] + cate + "_over.src")
}

// function to change images when mouse out
function ChangeImageOut() {
	if (document.images)
		document.getElementById(arguments[0]).src = eval(arguments[0] + ".src")
}

//index rollover
function setIndexOn(indexObj) {
	document.getElementById(indexObj.id).className = "onindex";
}

function setIndexOff(indexObj) {
	document.getElementById(indexObj.id).className = "offindex";
}