String.prototype.ltrim = function (chars) {
    chars = chars || "\\s";
	return this.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

String.prototype.rtrim = function (chars) {
    chars = chars || "\\s";
	return this.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

String.prototype.trim = function (chars) {
    return this.ltrim(chars).rtrim(chars);
}

String.prototype.toObject = function () {
    return eval('(' + this + ')');
}

function KF() {
    this.uAgent = navigator.userAgent.toLowerCase();
}

KF.prototype = {
    $: function (oid) {
        if ("string" === typeof oid) {
            oid = document.getElementById(oid);
        }
        
        if (oid && !oid.getStyle) {
            oid.getStyle = function (style) {
                return kf.getStyle(this, style);
            }
        }
        return oid;
    },
    $$: function (attribValue, tagName, ob, attribName) {
        var aEl, aREl = [], attrib;
        ob = this.$(ob);
        ob = (ob) ? ob : document;
        attribName = attribName || "class";
        tagName = tagName || "*";
        aEl = ob.getElementsByTagName(tagName);
        for (var i = 0, ln = aEl.length; i < ln; i++) {
            attrib = attribName == 'class' ? aEl[i].className : aEl[i].getAttribute(attribName);
            if (attrib && attrib.match(new RegExp("(^|\\s)"+attribValue+"(\\s|$)"))) {
                aREl[aREl.length] = aEl[i];
            }
        }
        return aREl;
    },
    isIE: function (version) {
        if (version) {
            return (this.uAgent.indexOf("msie " + version) > -1);
        }
        return (window.attachEvent && this.uAgent.indexOf("opera") === -1);
    },
    isOpera: function () {
        return (this.uAgent.indexOf("opera") > -1);
    },
    isWebKit: function () {
        return (this.uAgent.indexOf("applewebkit/") > -1);
    },
    isGecko: function () {
        return (this.uAgent.indexOf("gecko") > -1 && this.uAgent.indexOf("khtml") === -1);
    },
    isMobileSafari: function () {
        return !!navigator.userAgent.match(/Apple.*Mobile.*Safari/);
    },
    isMacOS: function () {
        if (this.uAgent.indexOf("mac") !== -1){
            return true;
        }
        return false;
    },
    isCssClass: function (oid, className) {
        oid = this.$(oid);
        return oid && oid.className.match(new RegExp("(^|\\s)" + className + "(\\s|$)"));
    },
    getCssClass: function (oid, asArray) {
        oid = this.$(oid);
        if (asArray) {
            return (oid ? oid.className.split(" ") : []);
        }
        return (oid ? oid.className : null);
    },
    setCssClass: function (oid, className) {
        oid = this.$(oid);
        return (oid ? oid.className = className : null);
    },
    addCssClass: function (oid, className) {
        oid = this.$(oid);
        if (!this.isCssClass(oid, className)) {
            oid.className = oid.className.trim() === "" ? className : oid.className + " " + className;
        }
        return oid.className;
    },
    removeCssClass: function (oid, className) {
        oid = this.$(oid);
        if (this.isCssClass(oid, className)) {
            oid.className = oid.className.replace(new RegExp("(^|\\s)" + className + "(\\s|$)"), " ").trim();
        }
        if("" === oid.className.trim()) {
            oid.removeAttribute("class");
        }
        return oid.className;
    },
    setStyleCss: function (oid, sStyle) {
        oid = this.$(oid);
        if (!oid) {
            return;
        }
        if (!sStyle) {
            oid.removeAttribute("style");
            return;
        }
        if (this.isIE()) {
            oid.style.setAttribute("cssText", sStyle);
        } else {
            oid.style.cssText = sStyle;
        }
    },
    newElement: function (tagName, attributes) {
        var el = document.createElement(tagName);
        for (var d in attributes) {
            if ("function" !== typeof attributes[d]) {
                switch (d) {
                    case "style":
                        this.setStyleCss(el, attributes[d]);
                        break;
                    case "class":
                        this.setCssClass(el, attributes[d]);
                        break;
                    default:
                        el.setAttribute(d, attributes[d]);
                        break;
                }
            }
        }
        return el;
    },
    addElement: function (element, oid, where) {
        oid = this.$(oid)||document.body;
        where = where||"lastChild";
        switch (where) {
            case "after":
                oid.parentNode.insertBefore(element, oid.nextSibling);
                break;
            case "before":
                oid.parentNode.insertBefore(element, oid);
                break;
            case "firstChild":
                oid.insertBefore(element, oid.firstChild);
                break;
            case "lastChild":
            default:
                oid.appendChild(element);
                break;
        }
        return element;
    },
    delElement: function (oid) {
        oid = this.$(oid);
        if (!oid) {
            return;
        }
        oid.parentNode.removeChild(oid);
    },
    getPosition: function (oid) {
        var l = 0, t = 0;
        oid = this.$(oid);
        if (oid.offsetParent) {
            l = oid.offsetLeft;
            t = oid.offsetTop;
            while ((oid = oid.offsetParent)) {
                l += oid.offsetLeft;
                t += oid.offsetTop;
            }
        }
        return {left:l, top:t};
    },
    setCookie: function (name, value, dateOrSec, cPath, cDomain, cSecure) {
        var b = [name + "=" + escape(value)], o;
        if (dateOrSec instanceof Date) {
            dateOrSec = dateOrSec.toUTCString();
        } else {
            if ("number" === typeof h) {
                dateOrSec = (new Date(+(new Date()) + (dateOrSec*1000))).toUTCString();
            }
        }
        o = {expires:dateOrSec||0, path:cPath||0, domain:cDomain||0, secure:cSecure||0};
        for (var f in o) {
            if (o[f] && "function" !== typeof o[f]) {
                b.push(f + "=" + o[f]);
            }
        }
        document.cookie = b.join(";");
    },
    getCookie: function (name) {
        var r = new RegExp("(?:;)?" + name + "=([^;]*);?");
        if(r.test(document.cookie)) {
            return decodeURIComponent(RegExp.$1);
        }
        return null;
    },
    delCookie: function (name, path, domain) {
        if (this.getCookie(name)) {
            document.cookie = name + "=" + (path ? ";path=" + path : "")
                + (domain ? ";domain=" + domain : "") + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
        }
    },
    setOpacity: function (oid, opacity) {
        oid = this.$(oid);
        if (oid) {
            oid.style.opacity = opacity;
            oid.style.filter = 'alpha(opacity=' + (opacity * 100)+ ')';
        }
    },
    changeOpacity: function (oid, diff) {
        var opacity = 1;
        oid = this.$(oid);
        if (oid) {
            if (oid.getStyle('opacity')) {
                opacity = oid.getStyle('opacity') * 1 + diff * 1;
            } else {
                opacity = opacity + diff * 1;
            }
                
            if (opacity > 1) {
                opacity = 1
            } else if (opacity < 0) {
                opacity = 0;
            }
            this.setOpacity(oid, opacity);
        }
        return opacity;
    },
    setTimeout: function (fn, mSec, id) {
        if (id) {
            window.clearTimeout(id);
        }
        if (fn && mSec && mSec > 0) {
            return window.setTimeout(fn, parseInt(mSec));
        }
        return null;
    },
    setPosition: function (oid, left, top) {
        oid = this.$(oid);
        if (!oid) {
            return;
        }
        left = left||0;
        top = top||0;
        if (oid.getStyle('position') != "absolute") {
            d(oid.getStyle('position'));
            oid.style.position = 'relative';
        }
        oid.style.left = left + 'px';
        oid.style.top = top + 'px';
    },
    changePosition: function (oid, diffLeft, diffTop) {
        var l = 0, t = 0;
        oid = this.$(oid);
        if (!oid) {
            return null;
        }
        l = parseInt(oid.getStyle('left'))||0;
        l = l + (diffLeft||0) * 1;
        t = parseInt(oid.getStyle('top'))||0;
        t = t + (diffTop||0) * 1;
        this.setPosition(oid, l, t);
        return {left:l, top:t};
    },
    addOnLoad: function (fn) {
        if ('function' === typeof fn) {
            onLoadFunctions[onLoadFunctions.length] = fn;
        }
    },
    getStyle: function (oid, style) {
        if ("object" !== typeof oid) {
            return "";
        }
        if (oid.style && oid.style[style]) {
            return oid.style[style];
        } else if (oid.currentStyle) {
            return oid.currentStyle[style];
        } else if (window.getComputedStyle) {
            return window.getComputedStyle(oid, null).getPropertyValue(style);
        }
        return '';
    }
};

function d(v) {
    console.log(v);
}

var kf = new KF();

function $(oid) {
    return kf.$(oid);
}
var onLoadFunctions = [];
window.onload = function () {
    var fn;
    for (var i = 0, ln = onLoadFunctions.length; i < ln; i++) {
        fn = onLoadFunctions[i];
        if ('function' === typeof fn) {
            fn();
        }
    }
}

