﻿
/*
* @author: Chinajmz
* @email : jmz2k@163.com
* @msn   : cn_jmz@hotmail.com
* 
* EXP:JS框架
* 
*/
var jmz = Object();
jmz.fn = Object();
jmz.ui = Object();

jmz.GetObjs = function(parentName, childPath, showType) {
    switch (showType) {
        case 1:
            return $("#" + parentName).find(childPath + ":visible");
            break;
        case 0:
            return $("#" + parentName).find(childPath + ":hidden");
            break;
        default:
            return $("#" + parentName).find(childPath);
            break;
    }
};

jmz.viewClass = "current";
jmz.ChangeView = function(elem, needClass) {

    if (needClass != true) {
        $(elem).css("display") == "none" ? $(elem).show() : $(elem).hide();
    }
    else {
        $(elem).toggleClass(jmz.viewClass);
    }
};

jmz.HashTable = function() {
    var _content = Object(), _count = 0;

    function GetCount() {
        return _count;
    };
    function CheckContentCount() {
        _count = 0;
        for (var i in _content) _count++;
        return _count;
    };
    function GetKeys() {
        var arr = Array();
        for (var key in _content)
            arr.push(key);
        return arr;
    };
    function GetValues() {
        var arr = Array();
        for (var key in _content)
            arr.push(ProcessKey(key));
        return arr;
    };
    function ProcessKey(key, value) {
        try {
            if (value == null) {
                if (typeof (eval("_content." + key + "")) == "undefined")
                    return null;
                else return eval("_content." + key + "");
            }
            else {
                eval("_content." + key + " = " + value + "");
                _count++;
            }
        }
        catch (e) {
        }
    };
    function RemoveKeys(keys) {
        try {
            if (keys instanceof Array) {
                for (var i in keys) {
                    eval("delete _content." + keys[i] + "");
                }
            }
            else {
                eval("delete _content." + keys + "");
            }
            return true;
        }
        catch (e) {
            return false;
        }
    };
    function ClearContent() {
        _content = null;
        _content = Object;
    };
    this.Count = function() {
        return GetCount();
    };
    this.CheckCount = function() {
        return CheckContentCount();
    };
    this.Get = function(key) {
        return ProcessKey(key);
    };
    this.Set = function(key, value) {
        ProcessKey(key, value);
    };
    this.Keys = function() {
        return GetKeys();
    };
    this.Values = function() {
        return GetValues();
    };
    this.Remove = function(keys) {
        return RemoveKeys(keys);
    };
    this.Clear = function() {
        ClearContent();
    }
};

jmz.ToolTip = function(obj, childStr) {
    var obj = $(obj);
    var childNode = obj.find(childStr);
    if ($.browser.msie) {
        childNode.css("bottom", "5px");
    }
    if (childNode.length == 1 && childNode[0].nodeName == "DIV") {
        obj.mouseover(function() {
            childNode.show();
        });
        obj.mouseout(function() {
            childNode.hide();
        });
    }
};

jmz.GetWH = function() {
    var windowWidth, windowHeight;
    if (self.innerHeight) {
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight) {
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    }
    else if (document.body) {
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }
    return [windowWidth, windowHeight];
};

jmz.fn.ParamUrl = function() {
    function getParam(o, l, r) {
        var url = window.location.toString();
        if (url != null && url.indexOf("?") != -1) {
            var paramStr = url.split('?')[1];
            if (paramStr != null && paramStr.length > 0) {
                if (r == true)
                    paramStr = paramStr.replace(/&amp;/g, "&");
                if (o == null && l == true) {
                    return paramStr.split('&');
                }
                else if (o != null && o.length > 0) {
                    var reg = new RegExp("(^|\\?|&)" + o + "=([^&]*)(\\s|&|$)", "i");
                    if (reg.test(paramStr)) {
                        return unescape(RegExp.$2.replace(/\+/g, " "));
                    }
                }
            }
        }
        return null;
    }

    function delParam(o, r) {
        var url = window.location.toString();
        if (url != null && url.indexOf("?") != -1) {
            if (r == true)
                url = url.replace(/&amp;/g, "&");
            if (o != null && o.length > 0) {
                if (o.indexOf(',') == -1) {
                    var reg = new RegExp("(^|\\?|&)" + o + "=([^&]*)(\\s|&|$)", "i");
                    if (reg.test(url)) {
                        if (RegExp.rightContext.length == 0)
                            return unescape(RegExp.leftContext);
                        return unescape(RegExp.leftContext + "&" + RegExp.rightContext);
                    } else {
                        return url;
                    }
                }
                else {
                    var reg = null;
                    var arr = o.split(',');
                    for (var i = 0; i < arr.length; i++) {
                        reg = new RegExp("(^|\\?|&)" + arr[i] + "=([^&]*)(\\s|&|$)", "i");
                        if (reg.test(url)) {
                            if (RegExp.rightContext.length == 0)
                                url = RegExp.leftContext;
                            else
                                url = RegExp.leftContext + "&" + RegExp.rightContext;
                        }
                    }
                    return unescape(url);
                }
            }
        }
    }

    this.GetParam = function(o, l, r) {
        return getParam(o, l, r);
    };
    this.DelParam = function(o, r) {
        return delParam(o, r);
    };
};

jmz.ui.TurnClick = function(name, childTag, passNum) {
    var _objName = name, _childTag = childTag, _passNum = passNum, _needClass = false,_cyc = true;
    function init(name, childTag, passNum) {
        _objName = name, _childTag = childTag, _passNum = passNum;
    }
    function turnRight() {
        try {
            var allarr = jmz.GetObjs(_objName, _childTag), sarr = jmz.GetObjs(_objName, _childTag, 1);
            var sindex = checkIndex(sarr, allarr, 0);
            var hindex = sindex + _passNum;
            var len = allarr.length - 1;
            if(!_cyc&&(sindex >= len))
            {
                alert('这是最后一页：）');
                return;
            }
            for (var i = 0; i < _passNum; i++) {
                if (sindex <= len) {
                    jmz.ChangeView(allarr[sindex + i], _needClass);
                }
                else {
                    jmz.ChangeView(allarr[0], _needClass);
                    sindex = 0;
                }
                if (hindex <= len) {
                    jmz.ChangeView(allarr[hindex + i], _needClass);
                }
                else {
                    jmz.ChangeView(allarr[0], _needClass);
                    hindex = 0;
                }
            }
        }
        catch (e) {
        }
        finally {
            allarr = null;
            sarr = null;
        }
    }
    function turnLeft() {
        try {
            var allarr = jmz.GetObjs(_objName, _childTag), sarr = jmz.GetObjs(_objName, _childTag, 1);
            var sindex = checkIndex(sarr, allarr, 0) + (_passNum - 1);
            var hindex = sindex - _passNum;
            var len = allarr.length - 1;
            if(!_cyc&&sindex == 0)
            {
                alert('这已经是最上一页了：）');
                return;
            }
            for (var i = 0; i < _passNum; i++) {
                if (sindex >= 0) {
                    jmz.ChangeView(allarr[sindex - i], _needClass);
                }
                else {
                    jmz.ChangeView(allarr[0], _needClass);
                    sindex = 0;
                }
                if (hindex >= 0) {
                    jmz.ChangeView(allarr[hindex - i], _needClass);
                }
                else {
                        jmz.ChangeView(allarr[len], _needClass);
                        hindex = len;
                }
            }
        }
        catch (e) {
        }
        finally {
            allarr = null;
            sarr = null;
        }
    }
    function checkIndex(sarr, allarr, type) {
        var sindex = allarr.index(sarr[0]), eindex = allarr.index(sarr[sarr.length - 1]);
        if (type == 0) {
            if (eindex - sindex == (_passNum - 1))
                return sindex;
            else
                return eindex;
        }
    }
    function setNeedClass(boolType) {
        _needClass = true;
    }
    this.Init = function(name, childTag, passNum) {
        init(name, childTag, passNum);
    };
    this.Right = function() {
        turnRight();
    };
    this.Left = function() {
        turnLeft();
    };
    this.SetClass = function(boolType) {
        setNeedClass(boolType);
    };
    this.SetNoCyc = function(boolType)
    {
        _cyc = false;
    }
};

jmz.ui.Domvisible = function(clickClassName, contentClassName, defaultObj, noRepeat) {
    var _prevObj = null, _bool = true, _contentClassName = contentClassName, _clickClassName = clickClassName;
    if (typeof (defaultObj) == "object" || typeof (defaultObj) == "string") defaultViewObj(defaultObj);
    if (typeof (noRepeat) == "boolean") _bool = noRepeat;
    function change(obj) {
        if (_prevObj != null) {
            if (_prevObj == obj && _bool == true) {
                $(_prevObj).toggleClass(_clickClassName);
                $("#" + _prevObj.id + "_content").toggleClass(_contentClassName);
            }
            else if (_prevObj != obj) {
                $(_prevObj).removeClass(_clickClassName);
                $("#" + _prevObj.id + "_content").removeClass(_contentClassName);
                $(obj).addClass(_clickClassName);
                $("#" + obj.id + "_content").addClass(_contentClassName);
                _prevObj = obj;
            }
        }
        else {
            $(obj).addClass(_clickClassName);
            $("#" + obj.id + "_content").addClass(_contentClassName);
            _prevObj = obj;
        }
    }
    function defaultViewObj(obj) {
        if (typeof (obj) == "object")
            _prevObj = obj;
        else
            _prevObj = $("#" + obj)[0];
    }
    function setNoRepeat(bool) {
        _bool = bool;
    }
    this.Change = function(obj) {
        change(obj);
    };
    this.DefaultViewObj = function(obj) {
        defaultViewObj(obj);
    };
    this.SetNoRepeat = function(bool) {
        setNoRepeat(bool);
    };
};

jmz.ui.ImageJsTurn = function(parentId, childTag, Time, childClass) {
    var _i = 0, _listChild = null, _len = 0, _interVal = null, _time = 1000, _childClass = "a_curr";
    if (parentId != null && childTag != null) init(parentId, childTag);
    if (typeof (Time) == "number") _time = Time;
    if (typeof (childClass == "string")) _childClass = childClass;
    function init(parentId, childTag, Time, childClass) {
        if (typeof (childTag) == "string") {
            if (typeof (parentId) == "object") {
                _listChild = $(parentId).children(childTag);
            }
            else if (typeof (parentId) == "string") {
                _listChild = $("#" + parentId).children(childTag);
            }
        }
        _i = 0;
        _len = _listChild.length;
        _listChild.hover(function() { abort() }, function() { run() });
    }

    function run() {
        if (_len > 1) _interVal = window.setInterval(processVisible, _time);
    }

    function abort() {
        if (_interVal != null) window.clearInterval(_interVal);
    }

    function processVisible() {
        _i++;
        if (_i == _len) {
            _i = 0;
            $(_listChild[_len - 1]).hide();
            $(_listChild[_i]).fadeIn("slow");
        }
        else {
            $(_listChild[_i - 1]).hide();
            $(_listChild[_i]).fadeIn("slow");
        }
    }

    this.Init = function(parentId, childTag) {
        init(parentId, childTag);
    };
    this.Run = function() {
        run();
    };
    this.Abort = function() {
        abort();
    };
};

jmz.ui.EasySlider = function(objName, passNum,params) {
    var defaults = {
        prevId: "_left",
        nextId: "_right",
        vertical: true,
        speed: 800,
        auto: false,
        pause: 2000,
        continuous: true,
        passNum: 1,
        ulPath: "ul > li"
    };
    var _options = defaults;
    if (typeof (params) == "object") {
        for (var key in params) {
            tmpVal = eval("params." + key + "");
            if (tmpVal == undefined || tmpVal == null) continue;
            switch(typeof(tmpVal)){
                case "string":eval("_options." + key + " = '" + tmpVal + "'");break;
                default:eval("_options." + key + " = " + tmpVal + "");break;
            }
        }
    }

    var _ts = 0, _t = 0, _w = 0, _h = 0, _obj = null, _contentCTag = _options.ulPath;
    if (typeof (passNum) == "number") _options.passNum = passNum;
    if (typeof (objName) == "string") {
        _obj = $("#" + objName);
        init(_obj);
    }
    function init(objName, passNum) {
        if (typeof (objName) == "string") _obj = $("#" + objName);
        if (typeof (objName) == "number") _options.passNum = passNum;
        var s = _obj.find(_contentCTag).length;
        _w = _obj.find(_contentCTag).width(); _h = _obj.find(_contentCTag).height();
        _ts = parseInt((s % _options.passNum == 0) ? (s / _options.passNum) : (s / _options.passNum + 1)) - 1; _t = 0;
        $("#" + _obj.attr("id") + _options.nextId).click(function() {
            process("next", true);
        });
        $("#" + _obj.attr("id") + _options.prevId).click(function() {
            process("prev", true);
        });
    }

    function process(dir, clicked) {
        var ot = _t + 1;
        switch (dir) {
            case "next": _t = (ot > _ts) ? (_options.continuous ? 0 : _ts) : _t + 1; break;
            case "prev": _t = (_t <= 0) ? (_options.continuous ? _ts : 0) : _t - 1; break;
            default: break;
        }
        var speed = _options.speed;
        if (_options.vertical == false) {
            p = (_t * 55 * _options.passNum * -1);
            _obj.find("ul").animate({ marginLeft: p }, speed);
        } 
        else if(_options.vertical == true){
            p = (_t * _h * -1);
            _obj.find("ul").animate({ marginTop: p }, speed);
        }
    }
    this.Init = function(objName) {
        init(objName);
    };
};
function TagClickView(name_ClickParent, name_ContentParent, clickTag, contentTag) {
    this._cliPName = name_ClickParent;
    this._conPName = name_ContentParent;
    this._cliTag = clickTag;
    this._conTag = contentTag;
    this.Init = function(name_ClickParent, name_ContentParent, clickTag, contentTag) {
        this._cliPName = name_ClickParent;
        this._conPName = name_ContentParent;
        this._cliTag = clickTag;
        this._conTag = contentTag;
    };
    this.Change = function(obj) {
        var sarr = jmz.GetObjs(this._cliPName, "p > " + this._cliTag + ".clickShow"), allarr = jmz.GetObjs(this._cliPName, "p > " + this._cliTag);
        if (sarr.length == 1) {
            var sindex = allarr.index(sarr[0]), hindex = allarr.index(obj);
            if (sindex != hindex) {
                var conarr = jmz.GetObjs(this._conPName, "center > " + this._conTag);
                allarr[sindex].className = "clickHidden";
                allarr[hindex].className = "clickShow";
                conarr[sindex].className = "contentHidden";
                conarr[hindex].className = "contentShow";
            }
        }
    }
}

function fn_headerSearch(pid) {
    var formObj = $("#" + pid).find("form");
    var type = formObj.find("select[name='p'] option:selected").attr("value"),
        key = formObj.find("input[name='keywords']").attr("value"),
        cate = formObj.find("select[name='cate_id'] option:selected").attr("value");
    window.location.href = "index.php?p=" + type + "&cate_id=" + cate + "&search_key=" + key;
}

function fn_msgBox(objid, msg) {
    var obj = $("#" + objid);
    obj.text(msg);
    obj.show();
    setTimeout('document.getElementById("' + objid + '").style.display="none";', 3000);
}

function fn_ajaxCall(method, url, data, callback, obj) {
    $.ajax({
        type: ((method == null) ? "POST" : method),
        dataType: "json",
        url: url,
        data: data,
        success: function(msg) {
            if ($.isFunction(callback)) {
                if (obj) callback(msg, obj);
                else callback(msg);
            }
        },
        error: function(msg) {
            if ($.isFunction(callback)) {
                if (obj) callback(msg, obj);
                else callback(msg);
            }
        }
    });
}
jQuery.fn.floatdiv = function(location) {
    var isIE6 = false;
    if ($.browser.msie && $.browser.version == "6.0") {
        $("html").css("overflow-x", "auto").css("overflow-y", "hidden");
        isIE6 = true;
    };
    $("body").css({ margin: "0px", padding: "0 10px 0 10px",
        border: "0px",
        height: "100%",
        overflow: "auto"
    });
    return this.each(function() {
        var loc;
        if (location == undefined || location.constructor == String) {
            switch (location) {
                case ("rightbottom"):
                    loc = { right: "0px", bottom: "0px" };
                    break;
                case ("leftbottom"):
                    loc = { left: "0px", bottom: "0px" };
                    break;
                case ("middlebottom"):
                    var l = 0;
                    var wh = uiBase.GetWH();
                    l = wh[0] / 2 - $(this).width() / 2;
                    loc = { left: l + "px", bottom: "0px" };
                    break;
                case ("lefttop"):
                    loc = { left: "0px", top: "0px" };
                    break;
                case ("righttop"):
                    loc = { right: "0px", top: "0px" };
                    break;
                case ("middlebottom"):
                    var l = 0;
                    var wh = uiBase.GetWH();
                    l = wh[0] / 2 - $(this).width() / 2;
                    loc = { left: l + "px", top: "0px" };
                    break;
                    break;
                case ("middle"):
                    var l = 0;
                    var t = 0;
                    var wh = uiBase.GetWH();
                    l = wh[0] / 2 - $(this).width() / 2;
                    t = wh[1] / 2 - $(this).height() / 2;
                    loc = { left: l + "px", top: t + "px" };
                    break;
                default:
                    loc = { right: "0px", bottom: "0px" };
                    break;
            }
        } else {
            loc = location;
        }
        $(this).css("z-index", "9999").css(loc).css("position", "fixed");
        if (isIE6) {
            if (loc.right != undefined) {
                if ($(this).css("right") == null || $(this).css("right") == "") {
                    $(this).css("right", "18px");
                }
            }
            $(this).css("position", "absolute");
        }
    });
};

(function($) {
    $.fn.wresize = function(f) {
        version = '1.1';
        wresize = { fired: false, width: 0 };

        function resizeOnce() {
            if ($.browser.msie) {
                if (!wresize.fired) {
                    wresize.fired = true;
                }
                else {
                    var version = parseInt(jQuery.browser.version, 10);
                    wresize.fired = false;
                    if (version < 7) {
                        return false;
                    }
                    else if (version == 7) {
                        var width = $(window).width();
                        if (width != wresize.width) {
                            wresize.width = width;
                            return false;
                        }
                    }
                }
            }

            return true;
        }

        function handleWResize(e) {
            if (resizeOnce()) {
                return f.apply(this, [e]);
            }
        }

        this.each(function() {
            if (this == window) {
                $(this).resize(handleWResize);
            }
            else {
                $(this).resize(f);
            }
        });

        return this;
    };

})(jQuery);


/*
window.open("http://192.168.1.110", "popWin", "height=700, width=500, top=0, left=1000, toolbar=yes, menubar=yes, scrollbars=yes, resizable=yes, location=yes, status=yes"); 
window.focus(); 
*/

$().ready(function() {
    if ($("#tool").length > 0)
        j().ui.floatDiv("#tool", "rightbottom");
});

function guanggao() {
    window.open("http://192.168.1.110", "popWin", "height=700, width=900, top=0, left=200, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=yes");
    window.focus();
}

var j = window.j = function() {
    return new j.base.init();
};

j.base = j.prototype = {
    init: function() {
        return this;
    },
    getUrlPara: function() {
        return new jmz.fn.ParamUrl();
    },
    toolTip: function(obj, childStr) {
        jmz.ToolTip(obj, childStr);
    },
    ajax: function(method, url, data, callback, obj) {
        fn_ajaxCall(method, url, data, callback, obj);
    },
    fn: {
        hashtable: function() {
            return new jmz.HashTable();
        },
        headerSearch: function(pid) {
            fn_headerSearch(pid);
        },
        msgBox: function(objid, msg) {
            fn_msgBox(objid, msg);
        }
    },
    ui: {
        floatDiv: function(id, location) {
            $(id).floatdiv(location);
        },
        specalClick: function(name, childTag, passNum) {
            return new jmz.ui.TurnClick(name, childTag, passNum);
        },
        tagClickView: function(name_ClickParent, name_ContentParent, clickTag, contentTag) {
            return new TagClickView(name_ClickParent, name_ContentParent, clickTag, contentTag);
        },
        domVisible: function(clickClassName, contentClassName, currobj, noRepeat) {
            return new jmz.ui.Domvisible(clickClassName, contentClassName, currobj, noRepeat);
        },
        imageJsTurn: function(parentId, childTag, Time, childClass) {
            return new jmz.ui.ImageJsTurn(parentId, childTag, Time, childClass);
        },
        easySlider: function(objName, passNum,params) {
            return new jmz.ui.EasySlider(objName, passNum,params);
        }
    }
};
j.base.init.prototype = j.base;