/* =========================================================================================================== *
DOMAssistant is developed by Robert Nyman, http://www.robertnyman.com, and it is released according to the
Creative Commons Attribution-ShareAlike 2.5 license (http://creativecommons.org/licenses/by-sa/2.5/deed.en)
For more information, please see http://www.robertnyman.com/domassistant
* =========================================================================================================== */
var DOMAssistant = {

    methodsToAdd: [],

    init: function () {
        this.applyMethod.call(window, "$", this.$);
        window.DOMAssistant = this;
    },

    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; debugger;
        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;
    }
}
DOMAssistant.init();

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();

/* =================================================================== *
* DOM functions													   *
* =================================================================== */

// for old browsers that don't have the push method
Array.prototype.push = function (value) {
    this[this.length] = value;
}

// javascript trim() function
String.prototype.trim = function () {
    return this.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g, "");
}

// cookies
function setCookie(cookieName, cookieValue, cookieExpire) {
    var c = cookieName + "=" + cookieValue;
    if (cookieExpire) {
        c += ";expires=" + cookieExpire;
    }
    document.cookie = c;
}


function getCookie(c) {


    var cookies = document.cookie.split(";");
    for (var i = 0, j = cookies.length; i < j; i++) {
        var cookieCrumbs = cookies[i].split("=");
        var cookieName = cookieCrumbs[0].trim();
        var cookieValue = cookieCrumbs[1];
        if (cookieName == c) {
            if (cookieValue == "false") {
                return false;
            } else {
                return cookieValue;
            }
        }
    }
    return false;
}

/* addEvent() adapted from Dustin Diaz
* http://www.dustindiaz.com/rock-solid-addevent/
* used to add event listeners to the page
*/



function addEvent(obj, type, fn) {
    if (obj) {
        if (obj.addEventListener) {
            obj.addEventListener(type, fn, false);
            EventCache.add(obj, type, fn);
        }
        else if (obj.attachEvent) {
            obj["e" + type + fn] = fn;
            obj[type + fn] = function () { obj["e" + type + fn](window.event); }
            obj.attachEvent("on" + type, obj[type + fn]);
            EventCache.add(obj, type, fn);
        }
        else {
            obj["e" + type + fn] = fn;
            obj["on" + type] = obj["e" + type + fn];
        }
    }
}

var EventCache = function () {
    var listEvents = [];
    return {
        listEvents: listEvents,
        add: function (node, sEventName, fHandler) {
            listEvents.push(arguments);
        },
        flush: function () {
            var i, item;
            for (i = listEvents.length - 1; i >= 0; i = i - 1) {
                item = listEvents[i];
                if (item[0].removeEventListener) {
                    item[0].removeEventListener(item[1], item[2], item[3]);
                };
                if (item[1].substring(0, 2) != "on") {
                    item[1] = "on" + item[1];
                };
                if (item[0].detachEvent) {
                    item[0].detachEvent(item[1], item[2]);
                };
                item[0][item[1]] = null;
            };
        }
    };
} ();
addEvent(window, 'unload', EventCache.flush); // fixes IE memory leak issues

function preventDefault(e) {
    if (e.preventDefault) {
        e.preventDefault();
    } else {
        e.returnValue = false;
    }
}

function getElementsByClassName(strClass, strTag, objContElm, firstOnly) {
    strTag = strTag || "*";
    objContElm = objContElm || document;
    var objColl = (strTag == '*' && document.all) ? document.all : objContElm.getElementsByTagName(strTag);
    var arr = (firstOnly) ? false : new Array();
    var delim = strClass.indexOf('|') != -1 ? '|' : ' ';
    var arrClass = strClass.split(delim);
    elementsLoop:
    for (var i = 0, j = objColl.length; i < j; i++) {
        var arrObjClass = objColl[i].className.split(' ');
        if (delim == ' ' && arrClass.length > arrObjClass.length) continue;
        var c = 0;
        comparisonLoop:
        for (k = 0, l = arrObjClass.length; k < l; k++) {
            for (m = 0, n = arrClass.length; m < n; m++) {
                if (arrClass[m] == arrObjClass[k]) c++;
                if ((delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length)) {
                    if (firstOnly) {
                        return objColl[i];
                    } else {
                        arr.push(objColl[i]);
                        break comparisonLoop;
                    }
                }
            }
        }
    }
    return arr;
}

/* =================================================================== *
* International Drop-Down Menu IE6 Fix								   *
* =================================================================== */

function sfHover() {
    var intl = document.getElementById("intl");
    if (intl) {
        var sfEls = intl.getElementsByTagName("LI");
        for (var i = 0; i < sfEls.length; i++) {
            sfEls[i].onmouseover = function () {
                this.className += (this.className.length > 0 ? " " : "") + "sfhover";
            }
            sfEls[i].onmouseout = function () {
                this.className = this.className.replace(new RegExp("( ?|^)sfhover\\b"), "");
            }
        }
    }
    var mainnav = document.getElementById("mainnav");
    if (mainnav) {
        var sfEls = mainnav.getElementsByTagName("LI");
        for (var i = 0; i < sfEls.length; i++) {
            sfEls[i].onmouseover = function () {
                this.className += (this.className.length > 0 ? " " : "") + "sfhover";
            }
            sfEls[i].onmouseout = function () {
                this.className = this.className.replace(new RegExp("( ?|^)sfhover\\b"), "");
            }
        }
    }
}
addEvent(window, "load", sfHover);
