﻿var $id = function(id) {
    if (typeof id == "string") {
        if (id == "body") {
            return document.body;
        } if (id == "") {
            return null;
        } else {
            return document.getElementById(id);
        }
    } else {
        return id;
    }
};

var $tag = function(tag) {
    return document.getElementsByTagName(tag);
}

var $val = function(id, value) {
    var dom = $id(id);
    if (dom) {
        if (typeof value == "string") {
            dom.value = value;
            return dom;
        } else {
            return dom.value;
        }
    } else {
        return "";
    }
}

var $html = function(id, value) {
    var dom = $id(id);
    if (dom) {
        if (typeof value == "string") {
            dom.innerHTML = value;
            return dom;
        } else {
            return dom.innerHTML;
        }
    } else {
        return "";
    }
}

var $height = function(id, value) {
    var e = $id(id);
    var offset = 0;
    if (e != null) {
        if (value) {
            e.style.width = value;
            return e;
        } else {
            offset = Math.max(e.offsetHeight, e.clientHeight);
            var c = e.childNodes;
            var tmpoffset = 0;
            for (var i = c.length - 1; i >= 0; i--) {
                tmpoffset += Math.max(c[i].offsetHeight, c[i].clientHeight);
            }
            offset = Math.max(tmpoffset, offset);
        }
    }
    return offset;
}

var $left = function(id, value) {
    var e = $id(id);
    if (!value) {
        var offset = 0;
        while (e != null) {
            offset += e.offsetLeft;
            e = e.offsetParent;
        }
        return offset;
    } else {
        e.style.left = value;
        return e;
    }
}

var $top = function(id, value) {
    var e = $id(id);
    if (!value) {
        var offset = 0;
        while (e != null) {
            offset += e.offsetTop;
            e = e.offsetParent;
        }
        return offset;
    } else {
        e.style.top = value;
        return e;
    }
}

var $childTag = function(id, tagName) {
    var dom = $id(id);
    if (dom) {
        return dom.getElementsByTagName(tagName);
    }
}

var $parentsTag = function(id, tagName) {
    var dom = $id(id);
    if (dom) {
        var tmpParent = dom.parentNode;
        while (tmpParent) {
            var tmptagName = tmpParent.tagName.toLowerCase();
            if (tmptagName == "body") {
                return document.body;
            } else if (tmptagName == tagName) {
                return tmpParent;
            } else {
                tmpParent = tmpParent.parentNode;
            }
        }
    } else {
        return null;
    }
}

var $hide = function(id) {
    var dom = $id(id);
    if (dom) {
        dom.style.display = "none";
        return dom;
    } else {
        return false;
    }
}

var $show = function(id, display) {
    if (!display) { display = ""; }
    var dom = $id(id);
    if (dom) {
        dom.style.display = display;
        return dom;
    } else {
        return false;
    }
}

var $class = function(id, classname) {
    var dom = $id(id);
    if (dom) {
        if (typeof classname == "string") {
            dom.className = classname;
            return dom;
        } else {
            return dom.className;
        }
        return true;
    } else {
        return false;
    }
}

var $addClass = function(id, classname) {
    var dom = $id(id);
    if (dom) {
        dom.className += " " + classname;
        return true;
    } else {
        return false;
    }
}

var $removeClass = function(id, classname) {
    var dom = $id(id);
    if (dom) {
        dom.className = dom.className.replace(classname);
        return true;
    } else {
        return false;
    }
}

var $click = function(doms, fn) {
    var domlen = doms.length;
    for (var index = 0; index < domlen; index++) {
        if (doms[index] != null) {
            if (!doms[index].onclick) {
                doms[index].onclick = function() { fn(this) };
            }
        }
    }
}

var $queryString = function(key, href) {
    if (typeof href == "string" || !window.QueryString) {
        window.QueryString = new Array();
        var str = "";
        if (typeof href == "string") {
            if (href.indexOf("?") >= 0) {
                str = href.substring(href.indexOf("?"));
            }
        } else {
            str = location.search;
        }
        str = str.replace("#", "");
        if (str.length > 0) {
            str = str.substring(1);
            var tmparr = str.split("&");
            for (var index = tmparr.length - 1; index >= 0; index--) {
                var tmparr1 = tmparr[index].split("=");
                if (tmparr1.length == 2) {
                    window.QueryString[tmparr1[0]] = tmparr1[1];
                }
            }
        }
    }
    var querystring = window.QueryString[key];
    if (querystring) {
        return querystring;
    } else {
        return "";
    }
}

var $cookie = function(name, value, options) {//options.expires : expireSecond
    if (typeof value != 'undefined') {
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString();
        }
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = cookies[i];
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
}


var $extend = function(parent, obj) {
    obj.prototype = new parent();
    obj.prototype.constructor = obj;
}

var $resizeIframe = function(id, minheight) {
    try {
        var iframe = $id(id);
        if (iframe) {
            if (iframe.src == "about:blank;") { $hide(iframe); return; }
            iframe.style.height = minheight + "px";
            var bHeight = iframe.contentWindow.document.body.scrollHeight;
            var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
            var height = Math.max(bHeight, dHeight);
            if (height < minheight) { height = minheight };
            iframe.style.height = height + "px";
            return iframe;
        } else {
            return false;
        }
    } catch (e) {
        return false;
    }
}

var $each = function(list, callback) {
    if (list) {
        var len = list.length;
        for (var i = 0; i < len; i++) {
            var val = callback(i, list[i]);
            if (val == false) {
                return false;
            }
        }
        return true;
    } else {
        return false;
    }
}

var $next = function(id) {
    var node = $id(id);
    if (node) {
        var tempLast = node.parentNode.lastChild;
        if (node == tempLast)
            return null;
        var tempObj = node.nextSibling;
        while (tempObj.nodeType != 1 && tempObj.nextSibling != null)
            temObj = tempObj.nextSibling;
        return (tempObj.nodeType == 1) ? tempObj : null;
    } else {
        return null;
    }
}

String.prototype.trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
