(function(win, J, undefined) {
    if (win[J] === undefined) win[J] = {};
    J = win[J];
    J.version = "1.1.0"; (function() {
        var initializing = false,
        fnTest = /xyz/.test(function() {
            xyz;
        }) ? /\b_super\b/: /.*/;
        function Class() {}
        Class.extend = function(prop) {
            var _super = this.prototype;
            initializing = true;
            var prototype = new this();
            initializing = false;
            for (var name in prop) {
                prototype[name] = typeof prop[name] == "function" && typeof _super[name] == "function" && fnTest.test(prop[name]) ? (function(name, fn) {
                    return function() {
                        var tmp = this._super;
                        this._super = _super[name];
                        var ret = fn.apply(this, arguments);
                        this._super = tmp;
                        return ret;
                    };
                })(name, prop[name]) : prop[name];
            }
            function Klass() {
                if (!initializing && this.init) this.init.apply(this, arguments);
            }
            Klass.prototype = prototype;
            Klass.constructor = Class;
            Klass.extend = arguments.callee;
            return Klass;
        };
        J.Class = Class;
    })();
})(window, "J16");
J16.io = window.J16.io || {};
J16.io.set = function(url, data, fn, callbackName) { (data.replace(/[^\x00-\xff]/g, "xx").length > 1350) ? $.post('/_proxy.php?url=' + encodeURIComponent(url), data, fn) : J16.io.get(url + (/\?/.test(url) ? '&': '?') + data, fn, callbackName, false);
};
J16.io.get = function(url, fnback, callbackName, isSimple) {
    var s, c, fn = 'jsonp' + ( + new Date()) + (this.jsonpid = ++this.jsonpid || 0),
    d = document,
    cbk = callbackName || 'callback',
    src = (url.indexOf("?") == -1 ? url + (isSimple ? "": "?") : url) + (isSimple ? '': '&' + cbk + '=' + fn),
    h = d.getElementsByTagName("head")[0]; (s = d.createElement("script")).setAttribute("type", "text/javascript");
    s.src = src;
    h.insertBefore(s, h.firstChild);
    window[fn] = function() {
        fnback && fnback.apply(null, arguments);
        c = 1
    };
    function done() {
        s.parentNode.removeChild(s);
        c || fnback && fnback()
    }
    s.onload = s.onreadystatechange = function() {
        s.readyState ? s.readyState.toLowerCase() == 'loaded' && (s.onreadystatechange = null, done()) : done()
    };
};
J16.util = window.J16.util || {
    console: function(type, message) {
        if (window.console) window.console[type](message);
    },
    getType: function(o) {
        var s = Object.prototype.toString,
        t = {
            'undefined': 'undefined',
            'number': 'number',
            'boolean': 'boolean',
            'string': 'string',
            "[object Function]": "function",
            "[object RegExp]": "regexp",
            "[object Array]": "array",
            "[object Date]": "date",
            "[object Error]": "error"
        };
        return t[typeof o] || t[s.call(o)] || (o ? 'object': 'null');
    },
    getDomain: function() {
        return "." + location.host.split(".").slice( - 2).join(".");
    },
    setHome: function(o, r) {
        o.target = "_self";
        var a = "url(#default#homepage)",
        b = "http://bbs.1616.net/thread-1913-1-4.html";
        try {
            if (document.all && !external.max_version) {
                o.style.behavior = a;
                o.setHomePage(r ? r: "http://www" + this.getDomain());
            } else window.open(b);
        } catch(e) {
            if (document.all) {
                o.style.behavior = a;
                o.setHomePage(r ? r: "http://www" + this.getDomain());
            } else window.open(b);
        }
        return false;
    },
    getQueryString: function(name) {
        var reg = new RegExp("[?&]" + name + "=([^&]*)"),
        h = window.location.href,
        r;
        h = h.substr(h.indexOf("?"));
        r = h.match(reg);
        return r ? r[1] : "";
    },
    hitTest: function(elem1, elem2) {
        var pos1 = elem1.getBoundingClientRect(),
        pos2 = elem2.getBoundingClientRect(),
        x1 = pos1.left,
        y1 = pos1.top,
        x2 = pos2.left,
        y2 = pos2.top,
        w1 = elem1.offsetWidth,
        h1 = elem1.offsetHeight,
        w2 = elem2.offsetWidth,
        h2 = elem2.offsetHeight;
        return (x1 < x2 ? x1 + w1 > x2: x1 < x2 + w2) && (y1 < y2 ? y1 + h1 > y2: y1 < y2 + h2);
    },
    mt: (function() {
        function parse(s) {
            var a = [];
            var re_t = /\{\$((?:[^\\}]|\\[\s\S])+)\}/;
            var re_s = /\\([\s\S])/g;
            var esc = function(s) {
                return s.replace(re_s, '$1');
            };
            var re_c = /(?:^|[^\\])(?:\\\\)*$/;
            var t, m, n;
            while (t = re_t.exec(s)) {
                n = (m = t.index) + t[0].length;
                if (re_c.test(s.substr(0, m))) {
                    a.push(esc(s.substr(0, m)));
                    a.push([esc(t[1])]);
                } else a.push(esc(s.substr(0, n)));
                s = s.substr(n);
            }
            return a;
        }
        function show(s, o) {
            var a = parse(s);
            for (var i = 0; i < a.length; i++) if (typeof a[i] == 'object') a[i] = o[a[i]] || '';
            return a.join('');
        }
        function show2(s, o) {
            return s.replace(/\{\$([^}]+)\}/g,
            function(a, b) {
                return o[b] || '';
            });
        }
        return function(template, data, adv) {
            var f = adv ? show: show2;
            if (! (data instanceof Array)) return f(template, data);
            var s = '';
            for (var i = 0; i < data.length; i++) s += f(templet, data[i]);
            return s;
        };
    })(),
    guid: (function() {
        if (typeof document.documentElement.uniqueID !== 'undefined') {
            return function(element) {
                return element.uniqueID;
            };
        }
        var uid = 0;
        return function(element) {
            return element.__uniqueID || (element.__uniqueID = 'uniqueID__' + uid++);
        };
    })()
};
J16.util.cookie = {
    set: function(n, v, e, p, d, s) {
        if (!e) e = 365;
        if (!p) p = '/';
        e = e * 1000 * 60 * 60 * 24;
        var e_date = new Date(new Date().getTime() + (e));
        document.cookie = n + '=' + encodeURIComponent(v) + ((e) ? ';expires=' + e_date.toGMTString() : '') + ((p) ? ';path=' + p: '') + ((d) ? ';domain=' + d: '') + ((s) ? ';secure': '');
    },
    get: function(n) {
        var c = document.cookie.split("; ");
        for (var i = 0; i < c.length; i++) {
            var p = c[i].split("=");
            if (n == p[0]) try {
                return decodeURIComponent(p[1])
            } catch(e) {
                return null
            }
        }
        return null;
    },
    del: function(name, path, domain) {
        document.cookie = name + "=1" + ((path) ? ";path=" + path: "") + ((domain) ? ";domain=" + domain: "") + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
    }
};
J16.util.json = {
    obj2str: function(o) {
        var r = [];
        if (typeof o == "string") return quote(o);
        if (typeof o == "undefined") return "undefined";
        if (typeof o == "object") {
            if (o === null) return "null";
            else if (!o.sort) {
                for (var i in o) r.push(this.obj2str(i) + ":" + this.obj2str(o[i]));
                r = "{" + r.join() + "}";
            } else {
                for (var i = 0; i < o.length; i++) r.push(this.obj2str(o[i]));
                r = "[" + r.join() + "]";
            }
            return r;
        }
        return o.toString();
        function quote(string) {
           // var escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
			var escapable = /[\\\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
            var meta = {
                '\b': '\\b',
                '\t': '\\t',
                '\n': '\\n',
                '\f': '\\f',
                '\r': '\\r',
                '"': '\\"',
                '\\': '\\\\'
            };
            escapable.lastIndex = 0;
            return escapable.test(string) ? '"' + string.replace(escapable,
            function(a) {
                var c = meta[a];
                return typeof c === 'string' ? c: '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice( - 4);
            }) + '"': '"' + string + '"';
        }
    },
    str2obj: function(str) {
        var undefined;
        try {
            if (str) {
                return eval('(' + str + ')');
            }
        } catch(ex) {}
        return null;
    }
};
J16.util.string = {
    cut: function(s, length, postfix) {
        if (typeof(s) != "string") return s;
        postfix = postfix || "";
        if (!s || !length) {
            return "";
        }
        var len = 0,
        temp = "";
        for (var i = 0; i < s.length; i++) {
            if (s.charCodeAt(i) > 255) {
                len += 2;
            } else {
                len++;
            }
            if (len > length) {
                return temp + postfix;
            }
            temp += s.charAt(i);
        }
        return s;
    }
};
J16.util.array = {
    removeItem: function(array, dx) {
        if (isNaN(dx) || dx > array.length || dx < 0) {
            return false;
        }
        return array.splice(dx, 1);
    }
};
void
function($) {
    $.fn.tab = function(obj) {
        var a = this.children().tab_blur();
        var c = $(obj.cnt).hide();
        var k = obj.cur || 0,
        n = a.length;
        var f = obj.onChange;
        $(a[k]).tab_focus();
        $(c[k]).show();
        for (var i = 0; i < n; i++) $(a[i]).click(Go(i));
        return {
            go: go,
            cur: function() {
                return k;
            }
        };
        function go(i) {
            if (k > -1) {
                $(a[k]).tab_blur();
                $(c[k]).hide();
            }
            $(a[i]).tab_focus();
            $(c[i]).show();
            k = i;
            if (f) f(i);
        }
        function Go(i) {
            return function() {
                if (k != i) go(i);
                return false;
            };
        }
    };
    $.fn.tab_blur = function() {
        return this.attr('class', '');
    };
    $.fn.tab_focus = function() {
        return this.attr('class', 'app_bt_sc');
    };
} (jQuery);
parent.J16.i.core.regAndStart("movie",function(coreW) {
    var app = coreW.newApp(),
    J16_i_video_config = null;
    app.name = "影视";
    app.cfg = {
        isLight: 0,
        history: []
    };
    app.init = function() {
        var T = this;
        T.render();
        T.events();
    };
    app.events = function() {
        var T = this;
        $('div.app_tabarea').tab({
            cnt: '.main',
            onChange: _updateHeight
        });
        $("body").mousedown(function() {
            $("#history").attr("class", "movie_gylist_close")
        });
        $("#movie_guanying").click(function() {
            if ($("#history").hasClass("movie_gylist_close")) {
                coreW.getCfg(function(d) {
                    if (d.code == 1) {
                        J16_i_video_config = d.desc;
                        _renderHistory(d.desc.history);
                        $("#history").attr("class", "movie_gylist")
                    } else alert("读取历史数据失败，请重试")
                },
                true);
            } else {
                $("#history").attr("class", "movie_gylist_close")
            }
            isFirst = 0;
            return false;
        }).mousedown(function() {
            return false
        });
        $("#history>dl").mousedown(function() {
            return false
        });
        $("#movie_del a").live("click",
        function() {
            J16_i_video_config.history.length = 0;
            _save();
            $("#history").attr("class", "movie_gylist_close")
        });
        $("a.movie_lpp,a.movie_lpimg,.movie_listtext a,#history>dl>dd:not(#movie_del) a").live("mousedown",
        function() {
            coreW.s16("http://apps.1616.net/video/index.php?a=player&id=" + $(this).attr("_id"), $(this).text(), $(this).parents(".movie_list1").find(">h2").text(), "");
        });
    };
    app.render = function() {
        _updateHeight();
    };
    function _updateHeight() {
        coreW.notify(coreW.et("menu", "UPDATE-HEIGHT"));
    }
    function _save(fn) {
        coreW.setCfg(J16_i_video_config,
        function(d) {
            if (d.code == 1) {
                fn && fn(d.desc);
            } else if (d.code != 1) alert("保存数据失败，请稍后重试");
        })
    }
    function _renderHistory(h) {
        $("#history>dl").html("");
        if (!h.length) {
            $("#history>dl").html("<dd><b>暂无观看记录</b></dd>");
        } else {
            var html = "<dt>最近我看过</dt>";
            for (var i = h.length; i--;) {
                html += "<dd><label><a _id=" + h[i].id + " href='?a=player&id=" + h[i].id + "' title='" + h[i].title + "'>" + h[i].title + "</a></label><b>" + _modDate(h[i].date) + "</b></dd>";
            }
            html += "<dd id=\"movie_del\"><a>清除历史</a></dd>";
            $("#history>dl").html(html);
        }
    }
    function _modDate(s) {
        var d = new Date();
        if (s == (d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate())) s = "今天";
        return s;
    };
    return app;
});