//BASE for creating classes


//Class.create and ah.extend methods borrowed from prototype.js 
//but put in ah namespace due to prototypes problems with for in loops with arrays

//creates ah namespace
var ah = {
	//copies all properties from a source object to a destination object
	extend: function(destination, source) {
		for (property in source) destination[property] = source[property];
		return destination;
	}
}

ah.Class = {
	//adds initialize constructor method to a new class
	create: function() {
		return function() {
			this.initialize.apply(this, arguments);
		}
	},
	
	//allows a new class to be created extending another class
	//extend an object literal to create a brand new empty class
    extend: function(source,additions) {
        var newclass = this.create();
        if (source.prototype) {
            ah.extend(newclass.prototype,source.prototype);
        }
        ah.extend(newclass.prototype, additions);
        return newclass;
    }
}

// borrowed from prototype.js library
// allows you to bind a function to another object 
//so 'this' keyword within refers to your chosen object
Function.prototype.bind = function(object) {
	var __method = this;
	return function() {
		return __method.apply(object, arguments);
	}
}

//*****************************************************************/

//Generic onload event handler
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}


//A function to get elements by class (does what it says on the tin) - (http://www.dustindiaz.com/getelementsbyclass)
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	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) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}


//Change the text of an element
function changeText(obj, newText){
	var theObject = document.getElementById(obj);
	var theNewText = document.createTextNode(newText);
	theObject.replaceChild(theNewText, theObject.childNodes[0]);
} 