/* DOMAssistant
DOMAssistant is developed by Robert Nyman, http://www.robertnyman.com, and it is released according to the
Creative Commons Deed license (http://creativecommons.org/licenses/GPL/2.0/)
For more information, please see http://www.robertnyman.com/domassistant
*/
var DOMAssistant = {

	methodsToAdd : [],

	init : function (){
		this.applyMethod.call(window, "$", this.$);
		window.DOMAssistant = this;
		this.addBaseMethods();
	},

	addBaseMethods : function (){
		document.getElementsByClassName = this.getElementsByClassName;
		document.getElementsByAttribute = this.getElementsByAttribute;
		if(typeof HTMLElement == "function"){
			HTMLElement.prototype.getElementsByClassName = this.getElementsByClassName;
			HTMLElement.prototype.getElementsByAttribute = this.getElementsByAttribute;
		}
		this.methodsToAdd.push(["getElementsByClassName", this.getElementsByClassName]);
		this.methodsToAdd.push(["getElementsByAttribute", this.getElementsByAttribute]);
	},

	applyMethod : function (method, func){
		if(typeof this[method] != "function"){
			this[method] = func;
		}
	},

	addMethods : function (elm){
		if(elm){
			var elms = (elm.constructor == Array)? elm : [elm];
			for(var i=0; i<elms.length; i++){	
				for(var j=0; j<this.methodsToAdd.length; j++){
	            	this.applyMethod.call(elms[i], this.methodsToAdd[j][0], this.methodsToAdd[j][1]);
	            }
			}
		}
	},

	$ : function (){
		var elm = null;
		if(document.getElementById){
			elm = (arguments.length > 1)? [] : null;
			var current;
			for(var i=0; i<arguments.length; i++){
				current = arguments[i];
				if(typeof current != "object"){
					current = document.getElementById(current);
				}
				if(arguments.length > 1){
					elm.push(current);
				}
				else{
					elm = current;
				}
			}
			DOMAssistant.addMethods(elm);
		}
		return elm;
    },

	getElementsByClassName : function (className, tag){
		var elms = ((!tag || tag == "*") && this.all)? this.all : this.getElementsByTagName(tag || "*");
		var returnElms = [];
		var className = className.replace(/\-/g, "\\-");
		var regExp = new RegExp("(^|\\s)" + className + "(\\s|$)");
		var elm;
		for(var i=0; i<elms.length; i++){
			elm = elms[i];		
			if(regExp.test(elm.className)){
				returnElms.push(elm);
			}
		}
		return (returnElms);
	},

	getElementsByAttribute : function (attr, attrVal, tag){
	    var elms = ((!tag || tag == "*") && this.all)? this.all : this.getElementsByTagName(tag || "*");
	    var returnElms = [];
	    if(typeof attrVal != "undefined"){
			var attrVal = new RegExp("(^|\\s)" + attrVal + "(\\s|$)");
		}
	    var current;
	    var currentAttr;
	    for(var i=0; i<elms.length; i++){
	        current = elms[i];
	        currentAttr = current.getAttribute(attr);
	        if(typeof currentAttr == "string" && currentAttr.length > 0){	
	            if(typeof attrVal == "undefined" || (attrVal && attrVal.test(currentAttr))){
					returnElms.push(current);
	            }
	        }
	    }
	    return returnElms;
	}
}
DOMAssistant.init();

/* DOMAssistantContent */
DOMAssistant.initContent = function (){
	this.addContentMethods();
};

DOMAssistant.addContentMethods = function (){
	if(typeof HTMLElement == "function"){
		HTMLElement.prototype.prev = DOMAssistant.prev;
		HTMLElement.prototype.next = DOMAssistant.next;
		HTMLElement.prototype.create = DOMAssistant.create;
		HTMLElement.prototype.setAttributes = DOMAssistant.setAttributes;
		HTMLElement.prototype.addContent = DOMAssistant.addContent;
		HTMLElement.prototype.replaceContent = DOMAssistant.replaceContent;
		HTMLElement.prototype.remove = DOMAssistant.remove;
	}
	this.methodsToAdd.push(["prev", this.prev]);
	this.methodsToAdd.push(["next", this.next]);
	this.methodsToAdd.push(["create", this.create]);
	this.methodsToAdd.push(["setAttributes", this.setAttributes]);
	this.methodsToAdd.push(["addContent", this.addContent]);
	this.methodsToAdd.push(["replaceContent", this.replaceContent]);
	this.methodsToAdd.push(["remove", this.remove]);
};

DOMAssistant.prev = function (){
	var prevSib = this.previousSibling;
	while(prevSib && prevSib.nodeType != 1){
		prevSib = prevSib.previousSibling;
	}
	return prevSib;
};

DOMAssistant.next = function (){
	var nextSib = this.nextSibling;
	while(nextSib && nextSib.nodeType != 1){
		nextSib = nextSib.nextSibling;
	}
	return nextSib;
};

DOMAssistant.create = function (name, attr, append, content){
	var elm = document.createElement(name);
	elm = DOMAssistant.$(elm);
	if(attr){
		elm.setAttributes(attr);
	}
	if(typeof content != "undefined"){
		elm.addContent(content);
	}
	if(append){
		this.addContent(elm);
	}
	return elm;
};

DOMAssistant.setAttributes = function (attr){	
	for(var i in attr){
		if(/class/i.test(i)){
			this.className = attr[i];
		}
		else{
			this.setAttribute(i, attr[i]);
		}
	}
};

DOMAssistant.addContent = function (content){
	var retVal = null;
	if(typeof content == "string"){
		retVal = this.innerHTML += content;
	}
	else{
		retVal = this.appendChild(content);
	}
	return retVal;
};

DOMAssistant.replaceContent = function (newContent){
	for(var i=(this.childNodes.length - 1); i>=0; i--){
    	this.childNodes[i].parentNode.removeChild(this.childNodes[i]);
    }
	this.addContent(newContent);
};

DOMAssistant.remove = function (){
	this.parentNode.removeChild(this);
};

DOMAssistant.initContent();

/* DOMAssistantCSS */
DOMAssistant.initCSS = function (){
	this.addCSSMethods();
};

DOMAssistant.addCSSMethods = function (){
	if(typeof HTMLElement == "function"){
		HTMLElement.prototype.addClass = this.addClass;
		HTMLElement.prototype.removeClass = this.removeClass;
		HTMLElement.prototype.hasClass = this.hasClass;
		HTMLElement.prototype.getStyle = this.getStyle;
	}
	this.methodsToAdd.push(["addClass", this.addClass]);
	this.methodsToAdd.push(["removeClass", this.removeClass]);
	this.methodsToAdd.push(["hasClass", this.hasClass]);
	this.methodsToAdd.push(["getStyle", this.getStyle]);
};

DOMAssistant.addClass = function (className){
	var currentClass = this.className;
	if(!new RegExp(("(^|\\s)" + className + "(\\s|$)"), "i").test(currentClass)){
		this.className = currentClass + ((currentClass.length > 0)? " " : "") + className;
	}
	return this.className;
};

DOMAssistant.removeClass = function (className){
	var classToRemove = new RegExp(("(^|\\s)" + className + "(\\s|$)"), "i");
	this.className = this.className.replace(classToRemove, "").replace(/^\s+|\s+$/g, "");
	return this.className;
},

DOMAssistant.hasClass = function (className){
	return new RegExp(("(^|\\s)" + className + "(\\s|$)"), "i").test(this.className);
};

DOMAssistant.getStyle = function (cssRule){
	var cssVal = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		cssVal = document.defaultView.getComputedStyle(this, "").getPropertyValue(cssRule);
	}
	else if(this.currentStyle){
		cssVal = cssRule.replace(/\-(\w)/g, function (match, p1){
			return p1.toUpperCase();
		});
		cssVal = this.currentStyle[cssVal];
	}
	return cssVal;
};

DOMAssistant.initCSS();

/* DOMAssistantEvents */
DOMAssistant.initEvents = function (){
	this.addEventMethods();
};

DOMAssistant.addEventMethods = function (){
	if(typeof HTMLElement == "function"){
		HTMLElement.prototype.addEvent = DOMAssistant.addEvent;
		HTMLElement.prototype.handleEvent = DOMAssistant.handleEvent;
		HTMLElement.prototype.removeEvent = DOMAssistant.removeEvent;
	}
	this.methodsToAdd.push(["addEvent", this.addEvent]);
	this.methodsToAdd.push(["handleEvent", this.handleEvent]);
	this.methodsToAdd.push(["removeEvent", this.removeEvent]);
};

DOMAssistant.addEvent = function (evt, func){
	if(this.addEventListener){
		this.addEventListener(evt, func, false);
	}
	else{
		if(!this.events){
			this.events = {};
		}
		if(!this.events[evt]){
			this.events[evt] = [];
		}
		this.events[evt].push(func);
		this["on" + evt] = DOMAssistant.handleEvent;
		if(typeof this.window == "object"){
			this.window["on" + evt] = DOMAssistant.handleEvent;
		}
	}
	return true;
};

DOMAssistant.handleEvent = function (evt){
	var evt = evt || event;
	var eventType = evt.type;
	var eventColl = this.events[eventType];
	for (var i=0; i<eventColl.length; i++) {
		eventColl[i].call(this, evt);
	}
};

DOMAssistant.removeEvent = function (evt, func){
	if(this.removeEventListener){
		this.removeEventListener(evt, func, false);
	}
	else if(this.events){
		var eventColl = this.events[evt];
		for (var i=0; i<eventColl.length; i++) {
			if(eventColl[i] == func){
				delete eventColl[i]
				eventColl.splice(i, 1);
			}
		}
	}
};

DOMAssistant.preventDefault = function (evt){
	if(evt && evt.preventDefault){
		evt.preventDefault();
	}
	else{
		event.returnValue = false;
	}
};

DOMAssistant.cancelBubble = function (evt){
	if(evt && evt.stopPropagation){
		evt.stopPropagation();
	}
	else{
		event.cancelBubble = true;
	}
};

DOMAssistant.initEvents();

/* DOM AssistantLoad.js */
// Inspired and influenced by Dean Edwards, Matthias Miller, and John Resig: http://dean.edwards.name/weblog/2006/06/again/
DOMAssistant.functionsToCall = [
	/*
	functionName // name of function
	"functionName()" // name of function with parentheses and optional arguments
	*/

	// site specific js
	function siteSpecific(){
		DOMAssistant.$("text-medium").addEvent("click", function() {setActiveStyleSheet('Medium'),false;});
		DOMAssistant.$("text-large").addEvent("click", function() {setActiveStyleSheet('Large')});
		DOMAssistant.$("text-xlarge").addEvent("click", function() {setActiveStyleSheet('X-Large'); return false;});
		DOMAssistant.$("text-size").removeClass("hide");
	}
];

DOMAssistant.initLoad = function (){
	this.DOMLoaded = false;
	this.DOMLoadTimer = null;
};

DOMAssistant.DOMHasLoaded = function (){
	if(DOMAssistant.DOMLoaded) return;
	DOMAssistant.DOMLoaded = true;
	DOMAssistant.execFunctions();
};

DOMAssistant.execFunctions = function (){
	if(this.DOMLoaded){
		clearInterval(this.DOMLoadTimer);
	}
	var functionToCall;
	for(var i=0; i<this.functionsToCall.length; i++){
		try{
			functionToCall = this.functionsToCall[i];
			if(typeof functionToCall == "function"){
				functionToCall();
			}
			else if (typeof functionToCall == "string"){
				eval(this.functionsToCall[i]);
			}
		}
		catch(e){
			// Optional: handle error here
		}
	}
};
// ---
/* Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
	if(document.getElementById){
		document.write("<script id=\"ieScriptLoad\" defer src=\"//:\"><\/script>");
	    document.getElementById("ieScriptLoad").onreadystatechange = function() {
	        if (this.readyState == "complete") {
	            DOMAssistant.DOMHasLoaded();
	        }
	    };
	}
/*@end @*/
// ---
/* Mozilla/Opera 9 */
if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", DOMAssistant.DOMHasLoaded, false);
}
// ---
/* Safari */
if(navigator.userAgent.search(/WebKit/i) != -1){
    DOMAssistant.DOMLoadTimer = setInterval(function (){
		if(document.readyState.search(/loaded|complete/i) != -1) {
			DOMAssistant.DOMHasLoaded();
		}
	}, 10);
}
// ---
/* Other web browsers */
window.onload = DOMAssistant.DOMHasLoaded;
// ---
DOMAssistant.initLoad();