jQuery.extend({
  getCookie : function(sName) {
    var aCookie = document.cookie.split("; ");
    for (var i=0; i < aCookie.length; i++){
      var aCrumb = aCookie[i].split("=");
      if (sName == aCrumb[0]) return decodeURIComponent(aCrumb[1]);
    }
    return '';
  },
  setCookie : function(sName, sValue, sExpires) {
    var sCookie = sName + "=" + encodeURIComponent(sValue);
    if (sExpires != null) sCookie += "; expires=" + sExpires;
    document.cookie = sCookie;
  },
  removeCookie : function(sName) {
    document.cookie = sName + "=; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
  }
});
function drop_confirm(msg, url){
    if(confirm(msg)){
        window.location = url;
    }
}

/* 显示Ajax表单 */
function ajax_form(id, title, url, width)
{
    if (!width)
    {
        width = 400;
    }
    var d = DialogManager.create(id);
    d.setTitle(title);
    d.setContents('ajax', url);
    d.setWidth(width);
    d.show('center');

    return d;
}
function go(url){
    window.location = url;
}

function change_captcha(jqObj){
    jqObj.attr('src', 'index.php?app=captcha&' + Math.round(Math.random()*10000));
}

/* 格式化金额 */
function price_format(price){
    if(typeof(PRICE_FORMAT) == 'undefined'){
        PRICE_FORMAT = '&yen;%s';
    }
    price = number_format(price, 2);

    return PRICE_FORMAT.replace('%s', price);
}

function number_format(num, ext){
    if(ext < 0){
        return num;
    }
    num = Number(num);
    if(isNaN(num)){
        num = 0;
    }
    var _str = num.toString();
    var _arr = _str.split('.');
    var _int = _arr[0];
    var _flt = _arr[1];
    if(_str.indexOf('.') == -1){
        /* 找不到小数点，则添加 */
        if(ext == 0){
            return _str;
        }
        var _tmp = '';
        for(var i = 0; i < ext; i++){
            _tmp += '0';
        }
        _str = _str + '.' + _tmp;
    }else{
        if(_flt.length == ext){
            return _str;
        }
        /* 找得到小数点，则截取 */
        if(_flt.length > ext){
            _str = _str.substr(0, _str.length - (_flt.length - ext));
            if(ext == 0){
                _str = _int;
            }
        }else{
            for(var i = 0; i < ext - _flt.length; i++){
                _str += '0';
            }
        }
    }

    return _str;
}

/* 收藏商品 */
function collect_goods(id)
{
    var url = SITE_URL + '/index.php?app=my_favorite&act=add&type=goods&ajax=1';
    $.getJSON(url, {'item_id':id}, function(data){
        alert(data.msg);
    });
}

/* 收藏店铺 */
function collect_store(id)
{
    var url = SITE_URL + '/index.php?app=my_favorite&act=add&type=store&jsoncallback=?&ajax=1';
    $.getJSON(url, {'item_id':id}, function(data){
        alert(data.msg);
    });
}
/* 火狐下取本地全路径 */
function getFullPath(obj)
{
    if(obj)
    {
        //ie
        if (window.navigator.userAgent.indexOf("MSIE")>=1)
        {
            obj.select();
            return document.selection.createRange().text;
        }
        //firefox
        else if(window.navigator.userAgent.indexOf("Firefox")>=1)
        {
            if(obj.files)
            {
                return obj.files.item(0).getAsDataURL();
            }
            return obj.value;
        }
        return obj.value;
    }
}

/**
 *    启动邮件队列
 *
 *    @author    Garbin
 *    @param     string req_url
 *    @return    void
 */
function sendmail(req_url)
{
    $(function(){
        var _script = document.createElement('script');
        _script.type = 'text/javascript';
        _script.src  = req_url;
        document.getElementsByTagName('head')[0].appendChild(_script);
    });
}
/* 转化JS跳转中的 ＆ */
function transform_char(str)
{
    if(str.indexOf('&'))
    {
        str = str.replace(/&/g, "%26");
    }
    return str;
}


(function() {
    $namespace("gzhelper.index");
    gzhelper.index.bindTab = function(id) {
        $tabExchange({
            titleId: id,
            titleTag: "li",
            contentId: id+"Contents",
            contentTag: "ul",
            initIndex: 0,
            timeLag: 200
        });
    };

    function $id(id) {
        return document.getElementById(id)
    };
    function $namespace(str) {
        var arr = str.split(',');
        for (var i = 0,
        len = arr.length; i < len; i++) {
            var arrJ = arr[i].split("."),
            parent = {};
            for (var j = 0,
            jLen = arrJ.length; j < jLen; j++) {
                var name = arrJ[j],
                child = parent[name];
                j === 0 ? eval('(typeof ' + name + ')==="undefined"?(' + name + '={}):"";parent=' + name) : (parent = parent[name] = (typeof child) === 'undefined' ? {}: child);
            };
        }
    };
    function $getY(e) {
        var t = e.offsetTop;
        while (e = e.offsetParent) {
            t += document.body != e ? e.offsetTop: 0;
        }
        return t;
    }
    function $formatDate(date, formatStr) {
        var arrWeek = ['日', '一', '二', '三', '四', '五', '六'],
        str = formatStr.replace(/yyyy|YYYY/, date.getFullYear()).replace(/yy|YY/, (date.getFullYear() % 100) > 9 ? (date.getFullYear() % 100).toString() : "0" + (date.getFullYear() % 100)).replace(/mm|MM/, date.getMonth() > 8 ? (date.getMonth() + 1).toString() : "0" + (date.getMonth() + 1)).replace(/m|M/g, date.getMonth() + 1).replace(/dd|DD/, date.getDate() > 9 ? date.getDate().toString() : "0" + date.getDate()).replace(/d|D/g, date.getDate()).replace(/hh|HH/, date.getHours() > 9 ? date.getHours().toString() : "0" + date.getHours()).replace(/h|H/g, date.getHours()).replace(/ii|II/, date.getMinutes() > 9 ? date.getMinutes().toString() : "0" + date.getMinutes()).replace(/i|I/g, date.getMinutes()).replace(/ss|SS/, date.getSeconds() > 9 ? date.getSeconds().toString() : "0" + date.getSeconds()).replace(/s|S/g, date.getSeconds()).replace(/w|W/g, arrWeek[date.getDay()]);
        return str;
    };
    function $addEvent(obj, type, handle) {
        if (window.addEventListener) {
            obj.addEventListener(type, handle, false);
        } else if (window.attachEvent) {
            obj.attachEvent("on" + type, handle);
        } else {
            obj["on" + type] = handle;
        }
    };
    function $delClass(ids, cName) {
        $setClass(ids, cName, "remove");
    };
    function $setClass(ids, cName, kind) {
        if (typeof(ids) == "string") {
            var arrDom = ids.split(",");
            for (var i = 0,
            len = arrDom.length; i < len; i++) {
                setClass($id(arrDom[i]), cName, kind);
            }
        } else {
            setClass(ids, cName, kind);
        };
        function setClass(obj, cName, kind) {
            var oldName = obj.className,
            arrName = oldName.split(' ');
            if (kind == "add") {
                if (!$hasClass(oldName, cName)) {
                    arrName.push(cName);
                    obj.className = arrName.join(' ');
                }
            } else if (kind == "remove") {
                var newName = [];
                for (var i = 0,
                len = arrName.length; i < len; i++) {
                    if (cName != arrName[i] && ' ' != arrName[i]) {
                        newName.push(arrName[i]);
                    }
                };
                obj.className = newName.join(' ');
            }
        }
    };
    function $delEvent(obj, type, handle) {
        if (window.removeEventListener) {
            obj.removeEventListener(type, handle, false);
        } else if (window.detachEvent) {
            obj.detachEvent("on" + type, handle);
        }
    };
    function $randomInt(num) {
        return Math.floor(Math.random() * num);
    };
    function $tabExchange(obj) {
        var option = {
            titleId: "",
            titleTag: "",
            contentId: "",
            contentTag: "",
            className: "current",
            initIndex: 0,
            timeLag: 0
        };
        for (var i in obj) {
            option[i] = obj[i];
        }
        var tabs = $id(option.titleId).childNodes,
        contents = $id(option.contentId).childNodes,
        _cont = [],
        _tabs = [],
        conTentTag = option.contentTag.toLowerCase(),
        titleTag = option.titleTag.toLowerCase();
        for (var i = 0,
        len = contents.length; i < len; i++) {
            if (conTentTag == contents[i].nodeName.toLowerCase()) {
                _cont.push(contents[i]);
            }
        }
        for (var i = 0,
        len = tabs.length; i < len; i++) {
            if (titleTag == tabs[i].nodeName.toLowerCase()) {
                _tabs.push(tabs[i]);
            }
        }
        if (_tabs.length != _cont.length) return;
        for (var j = 0,
        len = _cont.length; j < len; j++) {
            _cont[j].style.display = (j == option.initIndex) ? "block": "none";
        }
        if (_tabs[option.initIndex]) {
            var images = _cont[option.initIndex].getElementsByTagName("IMG");
            _tabs[option.initIndex].className = option.className;
            for (var i = 0,
            len = images.length; i < len; i++) {
                var oImg = images[i],
                oImgBack = oImg.getAttribute("back_src");
                if ('' == oImg.src && oImgBack) {
                    oImg.src = oImgBack;
                }
            }
        }
        for (var i = 0,
        len = _tabs.length; i < len; i++) {
            var oTab = _tabs[i];
            oTab.setAttribute("tabIndex", i);
            oTab.onmouseover = function() {
                var _i = parseInt(this.getAttribute("tabIndex"));
                window.activeHover = setTimeout(function() {
                    for (var j = 0,
                    len = _cont.length; j < len; j++) {
                        var images = _cont[j].getElementsByTagName("IMG");
                        for (var i = 0,
                        lenI = images.length; i < lenI; i++) {
                            var oImg = images[i],
                            oImgBack = oImg.getAttribute("back_src");
                            if ('' == oImg.src && oImgBack) {
                                oImg.src = oImgBack;
                            }
                        }
                        _cont[j].style.display = (j == _i) ? "block": "none";
                        _tabs[j].className = (j == _i) ? option.className: "";
                    }
                },
                option.timeLag);
            }
            oTab.onmouseout = function() {
                clearTimeout(window.activeHover);
            }
        }
    };
    function $hasClass(old, cur) {
        if (!old || !cur) return null;
        var arr = old.split(' ');
        for (var i = 0,
        len = arr.length; i < len; i++) {
            if (cur == arr[i]) {
                return cur;
            }
        };
        return null;
    } 
	
	function $mouseover(obj, func) {
        obj.onmouseover = function(e) {
            var e = window.event || e,
            target = e.fromElement || e.relatedTarget,
            parent = target;
            while (parent && parent !== this) {
                parent = parent.parentNode;
            }
            if (parent !== this) {
                func(this);
            }
        }
    }
    function $mouseout(obj, func) {
        obj.onmouseout = function(e) {
            var e = window.event || e,
            target = e.toElement || e.relatedTarget,
            parent = target;
            while (parent && parent !== this) {
                parent = parent.parentNode;
            }
            if (parent !== this) {
                func(this);
            }
        }
    }
    function $addZero(v, size) {
        for (var i = 0,
        len = size - (v + "").length; i < len; i++) {
            v = "0" + v;
        };
        return v;
    }
    function $attr(attr, val, node) {
        var results = [],
        node = node || document.body;
        walk(node,
        function(n) {
            var actual = n.nodeType === 1 && (attr === "class" ? n.className: n.getAttribute(attr));
            if (typeof actual === 'string' && (actual === val || typeof val !== 'string')) {
                results.push(n);
            }
        });
        return results;
        function walk(n, func) {
            func(n);
            n = n.firstChild;
            while (n) {
                walk(n, func);
                n = n.nextSibling;
            }
        }
    }
    function $each(jn, fn) {
        var len = jn.length;
        if ("number" === typeof len) {
            for (var i = 0; i < len; i++) {
                fn(jn[i], i);
            }
        } else {
            for (var k in jn) {
                fn(jn[k], k);
            }
        }
    };
    function $child(node, val, fn) {
        var results = [],
        node = node || document.body;
        walk(node.firstChild,
        function(n) {
            var actual = n.nodeType === 1 && n.nodeName.toLowerCase();
            if (typeof actual === 'string' && (actual === val || typeof val !== 'string')) {
                results.push(n);
                fn && fn(n);
            }
        });
        return results;
        function walk(n, func) {
            func(n);
            while (n = n.nextSibling) {
                func(n, func);
            }
        }
    }
})();
