﻿//(function() {
//    if (window.J)
//        return;
var global = false;
var j = window.j = {
    version: "0.0.1",
    C: {},
    Browser: (function() {
        var ua = navigator.userAgent;
        var isOpera = window.opera && ua.match(/opera/gi) ? true : false;
        var isIE = !isOpera && document.all && ua.match(/msie/gi) ? true : false;
        return {
            supportW3C: (document.getElementById) ? true : false,
            isOpera: isOpera,
            isIE: isIE,
            isIE6: isIE && ([/MSIE (\d)\.0/i.exec(ua)][0][1] == 6),
            isNS: (navigator.appName == "Netscape") ? true : false,
            isSafari: !isIE && ua.match(/safari/gi) ? true : false,
            isGecko: !isIE && ua.match(/gecko/gi) ? true : false,
            isFirefox: !isIE && ua.match(/firefox/gi) ? true : false,
            isFF3: !isIE && ua.match(/Firefox\/3/gi) ? true : false
        };
    })(),
    extend: function(destination, source) {
        for (var property in source)
            destination[property] = source[property];
    },
    math: {
        scaleRectZoomOut: function(width, height, scale) {
            if (!isNaN(scale) && scale > 0) {
                var iwidth = width, iheight = width * scale;
                var xs = 1, ys = iheight / height;
                var ts;
                if (xs >= 1 && ys >= 1) {
                    ts = Math.max(xs, ys);
                } else {
                    ts = Math.max(xs, ys);
                }
                width = iwidth / ts;
                height = iheight / ts;
            }
            //返回尺寸对象
            return { Width: width, Height: height }
        }
    }
    ,
    //_J: j.prototype,
    box: function(selector) {
        if (selector) {
            return $(selector);
        }
        else
            return this;
    },
    dom: function(selector) {
        if (selector) {
            return $(selector);
        }
        else
            return this;
    },
    once: function() {
        var returnValue;

        for (var i = 0, length = arguments.length; i < length; i++) {
            var lambda = arguments[i];
            try {
                returnValue = lambda();
                break;
            }
            catch (e) {
            }
        }

        return returnValue;
    },
    $: function(element) {
        if (arguments.length > 1) {
            for (var i = 0, elements = [], length = arguments.length; i < length; i++)
                elements.push($(arguments[i]));
            return elements;
        }
        if ("string" == typeof element)
            element = document.getElementById(element);
        else if (element != null && (element.parentNode == null || element.parentNode.nodeName == "#document-fragment"))
            element = document.getElementById(element.id);

        return element;
    },
    Class: function() {
        //Class: {
        //   create: function() {
        //       return function() { this.initialize.apply(this, arguments); }
        //   }
        //},
        var _class = function() {
            j.extend(this, _class)
            return this.initialize.apply(this, arguments);
        };
        for (i = 0; i < arguments.length; i++) {
            superClass = arguments[i];
            j.extend(_class.prototype, superClass.prototype);
        }
        j.extend(_class, {
            initialize: function(options) {
                this.SetOption(options);
            },
            extend: function(f) {
                j.extend(this, f);
                return this;
            },
            option: {},
            SetOption: function(options) {
                this.option = {};
                j.extend(this.option, _class.option);
                j.extend(this.option, options || {});
            }
        });
        return _class;
    },
    getBrowserWindowSize: function() {
        var de = document.documentElement;
        return {
            'width': (window.innerWidth || (de && de.clientWidth) || document.body.clientWidth),
            'height': (window.innerHeight || (de && de.clientHeight) || document.body.clientHeight)
        }
    },
    bind: function(object, fun) {
        return function() {
            return fun.apply(object, arguments);
        }
    },
    BindAsEventListener: function(object, fun) {
        var args = Array.prototype.slice.call(arguments).slice(2);
        return function(event) {
            return fun.apply(object, [event || window.event].concat(args));
        }
    },
    CurrentStyle: function(element) {
        return element.currentStyle || document.defaultView.getComputedStyle(element, null);
    },
    addEventHandler: function(oTarget, sEventType, fnHandler) {
        if (oTarget.addEventListener) {
            oTarget.addEventListener(sEventType, fnHandler, false);
        } else if (oTarget.attachEvent) {
            oTarget.attachEvent("on" + sEventType, fnHandler);
        } else {
            oTarget["on" + sEventType] = fnHandler;
        }
    },
    removeEventHandler: function(oTarget, sEventType, fnHandler) {
        if (oTarget.removeEventListener) {
            oTarget.removeEventListener(sEventType, fnHandler, false);
        } else if (oTarget.detachEvent) {
            oTarget.detachEvent("on" + sEventType, fnHandler);
        } else {
            oTarget["on" + sEventType] = null;
        }
    },
    each: function(list, fun) {
        for (var i = 0, len = list.length; i < len; i++) { fun(list[i], i); }
    },

    Contains: function(a, b) {
        return a.contains ? a != b && a.contains(b) : !!(a.compareDocumentPosition(b) & 16);
    },
    setcookie: function(name, value) {
        var expdate = new Date();
        var argv = arguments;
        var argc = arguments.length;
        var expires = (argc > 2) ? argv[2] : null;
        var path = (argc > 3) ? argv[3] : null;
        var domain = (argc > 4) ? argv[4] : null;
        var secure = (argc > 5) ? argv[5] : false;
        if (expires != null) expdate.setTime(expdate.getTime() + (expires * 1000));
        document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : (";  expires=" + expdate.toGMTString()))
        + ((path == null) ? "" : (";  path=" + path)) + ((domain == null) ? "" : (";  domain=" + domain))
        + ((secure == true) ? ";  secure" : "");
    },
    getcookie: function(name) {
        var a = document.cookie.split("; ");
        name += "=";
        for (var i = 0; i < a.length; i++)
            if (a[i].indexOf(name) == 0)
            return unescape(a[i].substr(name.length));
        return "";

    },
    delcookie: function(name) {
        var exp = new Date();
        exp.setTime(exp.getTime() - 1);
        var cval = j.getcookie(name);
        var argv = arguments;
        var argc = arguments.length;
        var path = (argc > 1) ? argv[1] : null;
        var domain = (argc > 2) ? argv[2] : null;
        var secure = (argc > 3) ? argv[3] : false;

        document.cookie = name + "=" + escape(cval) + "; Expires=" + exp.toGMTString()
        + ((path == null) ? "" : (";  path=" + path)) + ((domain == null) ? "" : (";  domain=" + domain))
        + ((secure == true) ? ";  secure" : "");
    },
    preload: function(img, callback) {
        if (typeof img == "string")
            img = [img];
        var j = img.length;
        for (var i = 0; i < img.length; i++) {
            var img = document.createElement("img");
            img.onload = function() {
                if (! --j) { if (callback) callback(); }
            }
            img.src = img[i];
        }
    }
};
Function.prototype.bind = function(object) {
    var __method = this;
    return function() {
        __method.apply(object, arguments)
    }
};
Function.prototype.bindAsEventListener = function(object) {
    var __method = this;
    return function(event) {
        __method.call(object, event || window.event)
    }
};


var arrayProto = Array.prototype,
      slice = arrayProto.slice,
      _each = arrayProto.forEach; // use native browser JS 1.6 implementation if available

function each(iterator) {
    for (var i = 0, length = this.length; i < length; i++)
        iterator.bind(this)(this[i], i);
}
if (!_each) _each = each;

j.extend(arrayProto, { each: _each,
    remove: function(obj) { this.each(function(sub, index) { if (obj === sub) { this.splice(this, index); } }); }
});

if (global === true) {
    j.extend(window, j);
}

j.sim = {
    clicka: function(a) {
        if (a && a.nodeName == "A") {
            if (j.Browser.isIE)
                a.click();
            else {
                var evt = document.createEvent("MouseEvents");
                evt.initEvent("click", true, true);
                if (a.dispatchEvent(evt) == true)
                    window.location = a.href;
            }
        }
    }
};

j.C.AutoHideTopAd = new j.Class().extend({
    option: {
        box: null, //保存广告的ID
        show: 19000, //显示的时间
        height: 200,
        width: "100%"
    }, //全局默认值。
    __timer: 0,
    initialize: function(box, cookieName, options) {
        this.box = j.$(box);
        this.cookieName = cookieName;
        this.SetOption(options);
        this.box.style.display = "none";
        if (j.getcookie(this.cookieName) != "closed") {
            jQuery().ready(j.BindAsEventListener(this, function() { this.display(1); }));
        }
    },
    display: function(type) {
        if (type) {
            jQuery("#" + this.box.id).slideDown(1000);
            //this.box.style.display = "";
            this.__timer = window.setTimeout(j.BindAsEventListener(this, function() { this.display(0); }), this.option.show);
        }
        else {
            if (this.__timer > 0)
                window.clearTimeout(this.__timer);
            jQuery("#" + this.box.id).slideUp(1000);
            j.setcookie(this.cookieName, "closed");
            //this.box.style.display = "none";

        }
    }
});
//})();

j.overLayer = new j.Class().extend({
    option: {
        Lay: null, //覆盖层对象或ID
        color: "#fff", //背景色
        Opacity: 50, //透明度(0-100)
        zIndex: 1000//层叠顺序
    }, //全局默认值。
    initialize: function(options) {
        this.SetOption(options);
        var body = document.body || document.documentElement;
        this.Lay = j.$(this.option.Lay) || body.insertBefore(document.createElement("div"), body.childNodes[0]);
        this.Opacity = parseInt(this.option.Opacity);
        this.zIndex = parseInt(this.option.zIndex);
        with (this.Lay.style) { display = "none"; zIndex = this.zIndex; left = top = 0; position = "fixed"; width = height = "100%"; }
        if (j.Browser.isIE6) {
            this.Lay.style.position = "absolute";
            //ie6设置覆盖层大小程序
            this._resize = j.bind(this, function() {
                this.Lay.style.width = Math.max(document.documentElement.scrollWidth, document.documentElement.clientWidth) + "px";
                this.Lay.style.height = Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight) + "px";
            });
            //遮盖select
            this.Lay.innerHTML = '<iframe style="position:absolute;top:0;left:0;width:100%;height:100%;filter:alpha(opacity=0);"></iframe>'
        }
    },
    show: function() {//显示
        if (j.Browser.isIE6) { this._resize(); window.attachEvent("onresize", this._resize); } //兼容ie6
        with (this.Lay.style) { //设置样式
            //设置透明度
            j.Browser.isIE ? filter = "alpha(opacity:" + this.Opacity + ")" : opacity = this.Opacity / 100;
            backgroundColor = this.option.color; display = "block";
        }
    },
    hide: function() {//关闭
        this.Lay.style.display = "none";
        if (j.Browser.isIE6) { window.detachEvent("onresize", this._resize); }
    }
});
j.layer = new j.Class().extend({
    option: {
        Over: true, //是否显示覆盖层
        Fixed: false, //是否固定定位
        Center: false, //是否居中
        onShow: function() { } //显示时执行
    },
    initialize: function(layer, options) {
        this.Box = j.$(layer); //显示层

        this.OverLay = new j.overLayer(options); //覆盖层
        this.SetOption(options);
        this.Fixed = !!this.option.Fixed;
        this.Over = !!this.option.Over;
        this.Center = !!this.option.Center;
        this.onShow = this.option.onShow;
        this.Box.style.zIndex = this.OverLay.zIndex + 1;
        this.Box.style.display = "none";
        //兼容ie6用的属性
        if (j.Browser.isIE6) {
            this._top = this._left = 0; this._select = [];
            this._fixed = j.bind(this, function() { this.Center ? this.SetCenter() : this.SetFixed(); });
        }
    },
    show: function(options) {
        j.extend(this.option, options || {});
        this.Fixed = !!this.option.Fixed;
        this.Over = !!this.option.Over;
        this.Center = !!this.option.Center;

        this.Box.style.position = this.Fixed && !j.Browser.isIE6 ? "fixed" : "absolute"; //固定定位
        this.Over && this.OverLay.show(); //覆盖层
        this.Box.style.display = "block";
        //居中
        if (this.Center) {
            this.Box.style.top = this.Box.style.left = "50%";
            //设置margin
            if (this.Fixed) {
                this.Box.style.marginTop = -this.Box.offsetHeight / 2 + "px";
                this.Box.style.marginLeft = -this.Box.offsetWidth / 2 + "px";
            } else {
                this.SetCenter();
            }
        }
        if (j.Browser.isIE6) {//兼容ie6
            if (!this.Over) {
                //没有覆盖层ie6需要把不在Box上的select隐藏
                this._select.length = 0;
                j.each(document.getElementsByTagName("select"), j.bind(this, function(o) {
                    if (!Contains(this.Box, o)) { o.style.visibility = "hidden"; this._select.push(o); }
                }))
            }
            //设置显示位置
            this.Center ? this.SetCenter() : this.Fixed && this.SetFixed();
            //设置定位
            this.Fixed && window.attachEvent("onscroll", this._fixed);
        }
        this.onShow();
    },
    hide: function() {
        this.Box.style.display = "none";
        this.OverLay.hide();
        if (j.Browser.isIE6) {
            window.detachEvent("onscroll", this._fixed);
            j.each(this._select, function(o) { o.style.visibility = "visible"; });
        }

    },
    //兼容ie6的固定定位程序
    SetFixed: function() {
        this.Box.style.top = document.documentElement.scrollTop - this._top + this.Box.offsetTop + "px";
        this.Box.style.left = document.documentElement.scrollLeft - this._left + this.Box.offsetLeft + "px";
        this._top = document.documentElement.scrollTop; this._left = document.documentElement.scrollLeft;
    },
    //兼容ie6的居中定位程序
    SetCenter: function() {
        this.Box.style.marginTop = document.documentElement.scrollTop - this.Box.offsetHeight / 2 + "px";
        this.Box.style.marginLeft = document.documentElement.scrollLeft - this.Box.offsetWidth / 2 + "px";
    }
});



