﻿var he = {
    get : function (e) {
        if (typeof e === "object") {
            return e;
        } else if (typeof e === "string") {
            if (e === "body") {
                return document.body;
            } else {
                return document.getElementById(e);
            }
        } else {
			return false;
        }
    },
    getEs : function (tn) {
        return document.getElementsByTagName(tn);
    },
    getNs : function (n) {
		return document.getElementsByName(n);
    },
    create : function (tn, id, att, pos, attrs) {
        if (!this.get(id)) {
            var nt = document.createElement(tn);
            var t = document.body;
            if (id) { 
                nt.id = id; 
            }
            if (att) {
                if (typeof att === "object") {
                    t = att;
                } else if (typeof att === "string") {
                    t = document.getElementById(att);
                }
            } 
            if (attrs) {
				for (var a = 0; a < attrs.length; a += 1) {
					nt[attrs[a][0]] = attrs[a][1];
				}
            }
            if (!t) {
				t = document.body;
            }
            this.add(nt, t, pos);
        }
    },
    add : function (nn, pn, pos) {
        var n = this.get(nn);
        var p = this.get(pn);
		
        if (!pos || typeof pos === "string" || typeof pos !== "number") {
            if (!pos || pos !== "first") {
                pos = "last";
            }
        }
        if (pos === "first" || pos === 0) {
            p.insertBefore(n, p.firstChild);
        } else if (pos === "last" || !isNaN(pos) || pos >= p.childNodes.length) {
            p.appendChild(n);
        } else {
            p.insertBefore(n, p.childNodes[pos]);
        }
    },
    remove : function (e) {
		var cn = he.get(e);
		if (cn && he.get(cn.parentNode)) {
			he.get(cn.parentNode).removeChild(cn);
		}
    },
    write : function (str, e, w) {
        if (this.get(e)) {
            if (w && w === "add") {
                this.get(e).innerHTML += str;
            } else {
                this.get(e).innerHTML = str;
            }
        }
    },
    getClass : function (cl, e) {
		if (typeof cl === "object") {
			return he.get(cl).className;
		} else {
			var oc = he.get(e).className;
			var re = new RegExp("(^|\\s)(" + cl + ")(\\s|$)","g");
			return oc.match(re);
        }
    },
    addClass : function (nc, e) {
        var oc = this.get(e).className;
        var re = new RegExp("(^|\\s)(" + nc + ")(\\s|$)","g");
        if (!oc) {
            this.get(e).className = nc;
        } else if (oc && !oc.match(re)) {
            this.get(e).className = oc + " " + nc;
        }
    },
    removeClass : function (nc, e) {
        var oc = this.get(e).className;
        var re = new RegExp("(^|\\s)(" + nc + ")(\\s|$)","g");
        this.get(e).className = oc.replace(re, " ").replace(/(\s{2,})/g, " ").replace(/^\s/, "").replace(/\s$/, "");
        if (this.get(e).className === "") {
			this.remAttr("class", e);
        }
    },
    replaceClass : function (nc, oc, e) {
        this.addClass(nc, e);
        this.removeClass(oc, e);
    },
    getEsClass : function (cn) {
		he.cClass = cn;
		he.ebc = [];
		if (document.getElementsByClassName) {
			return document.getElementsByClassName(cn);
		} else {
			he.wtd(document.body, he.addToEC);
			return he.ebc;
		}
    },
    addToEC : function (n) {
		var re = new RegExp("(^|\\s)(" + he.cClass + ")(\\s|$)","g");
		var oc = he.get(n).className;
		if (oc && oc.match(re)) {
			he.ebc.push(n);
		}
    },
    setAttr : function (atn, atv, e) {
        if (this.get(e)) {
            this.get(e).setAttribute(atn, atv);
        }
    },
    getAttr : function (atn, e) {
        if (this.get(e)) {
            return this.get(e).getAttribute(atn);
        } else {
			return false;
        }
    },
    getEsAttr : function (n) {
        he.attrs = [];
        he.cAttr = n;
        he.wtd(document.body, he.addToAttr);
        return he.attrs;
    },
    addToAttr : function (e) {
        if( e.nodeType !== 3){
            if(e[he.cAttr]) {
                he.attrs.push(e);
            }
        }
    },
    getAttrVAl : function (a, v) {
        ha.attrVal = [];
        he.cAttr = a.toLowerCAse();
        he.cAttrVal = v.toLowerCAse();
        he.wtd(document.body, he.addToAttrVal);
        return ha.attrVal;
    },
    addToAttrVal : function (e) {
        if (e.getAttr(he.cAttr) && e.getAttr(he.cAttr).value === he.cAttrVal) {
            ha.attrVal.push();
        }
    },
    remAttr : function (atn, e) {
		if (this.get(e)) {
            this.get(e).removeAttribute(atn);
        }
    },
    addEvent : function (evt, func, e) {
        if (window.attachEvent) {
            this.get(e).attachEvent("on" + evt, func);
        } else if (window.addEventListener) {
            this.get(e).addEventListener(evt, func, false);
        }
    },
    removeEvent : function (evt, func, e) {
        if (window.attachEvent) {
            this.get(e).detachEvent("on" + evt, func);
        } else if (window.addEventListener) {
            this.get(e).removeEventListener(evt, func, false);
        }
    },
    wtd : function (node, func) {
		// Taken from Douglas Crockfords "The Theory of the DOM"
		// Originally called "walkTheDom" 
		func(node);
		node = node.firstChild;
		while (node) {
			he.wtd(node, func);
			node = node.nextSibling;
		}
    }
};