(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.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 + "=([^&]*)");
        var r = location.search.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);
    }
};
var proxy = function() {
    var CALLBACK = {};
    window.addEventListener ? window.addEventListener('message', callback, false) : window.attachEvent('onmessage', callback);
    function callback(e) {
        e = e.data || e;
        e = e.split('$$$');
        CALLBACK[e[1]](e[0]);
        destroyProxy(e[1]);
    };
    function destroyProxy(id) {
        var div = document.getElementById(id),
        ifr = div.firstChild;
        div.innerHTML = '';
        div.parentNode.removeChild(div);
        delete CALLBACK[id];
    };
    function createProxy(src, id) {
        var div = document.createElement('DIV');
        div.style.display = 'none';
        div.id = id;
        div.innerHTML = '<iframe src="' + src + '"></iframe>';
        document.body.appendChild(div);
        return div.firstChild;
    }
    function Proxy() {}
    Proxy.prototype = {
        bind: function(proxyUrl) {
            this.proxyUrl = proxyUrl;
        },
        call: function(func, params, fn) {
            var RID = '$' + ( + new Date),
            ifr = createProxy(this.proxyUrl || 'http://api.1616.net/proxy.htm', RID);
            CALLBACK[RID] = fn || new Function;
            params = params.concat(RID).concat(func).join('$$$');
            ifr.addEventListener ? ifr.onload = loaded: ifr.attachEvent('onload', loaded);
            ifr.contentWindow.postMessage || (ifr.contentWindow.opener = {
                data: params,
                callback: CALLBACK[RID]
            });
            function loaded() {
                ifr.contentWindow.postMessage && ifr.contentWindow.postMessage(params, '*');
            }
        },
        post: function(url, data, fn) {
            this.call('__send', [url, data], fn);
        }
    };
    return new Proxy();
} ();
var idx1, i1;
function sli(a, added) {
    idx1[a.app_id] = i1++;
    return '<li appid=' + a.app_id + '><span class="icon"><img src="'+G_dirUrl+'images/manager/small/'+ a.app_big_image + '"></span><span class="title">' + a.app_name + (a.app_id > 19 ? '<sup>&nbsp;<font color=red>New!</font></sup>': '') + '</span><span class="int">' + a.app_description + '</span>' + (a.added == 'no' ? '<span class="add appicon-add"><a href="#" onclick="return false;">添加应用</a></span>': '<span class="add">已添加</span>') + '</li>';
}
var idx2, i2;
function sliadded(a, up, dn) {
    idx2[a.app_id] = i2++;
    /*return '<li appid=' + a.app_id + '><span class="icon"><img src="'+G_dirUrl+'images/manager/small/' + a.app_big_image + '"></span><span class="title">' + a.app_name + '</span><span class="int">' + a.app_description + '</span><a href="#" onclick="return false;"><span class="sort appicon-up' + (up ? '': '2') + '" title="上移"></span></a><a href="#" onclick="return false;"><span class="sort appicon-dn' + (dn ? '': '2') + '" title="下移" ></span></a>' + ( + a.allow_delete ? '<span class="del appicon-del"><a href="#" onclick="return false;">删除应用</a></span>': '') + '</li>';*/
	return '<li appid=' + a.app_id + '><span class="icon"><img src="'+G_dirUrl+'images/manager/small/' + a.app_big_image + '"></span><span class="title">' + a.app_name + '</span><span class="int">' + a.app_description + '</span>' + ( + a.allow_delete ? '<span class="del appicon-del"><a href="#" onclick="return false;">删除应用</a></span>': '') + '</li>';
}
var data = null;
var datamy = null;
var theappid;
var lastc;
var thec;
var isbusy = false;
var modified = true;
var menu = parent.getIns('www').get('menu');
function updateHeight() {
    parent.getIns('manager').__updateHeight(true);
}
function cbget(a) {
    if (a) data = a;
    else a = data;
    var s = '';
    idx1 = [];
    i1 = 0;
    for (var i = 0; i < a.length; i++) s += sli(a[i]);
    $('#ul1').html(s);
    updateHeight();
}
function cbgetadded(a, m) {
    if (a) datamy = a;
    else a = datamy;
    var s = '';
    idx2 = [];
    i2 = 0;
    for (var i = 0; i < a.length; i++) s += sliadded(a[i], i > 0, i < a.length - 1);
    $('#ul2').html(s);
    if (m) $('#ul2 li[appid=' + theappid + ']').addClass('cur');
    updateHeight();
}
function cbadd(d) {
    modified = true;
    if (d == 1) {
        setaddy(theappid);
        var obj = data[idx1[theappid]];
        var app = {
            appurl: obj.app_url,
            ver: obj.app_version,
            mode: obj.iframe_mode,
            appid: obj.app_id,
            id: obj.app_english_name,
            name: obj.app_name,
            pic: obj.app_small_image,
            stickable: obj.stickable
        };
        menu.add(app);
    } else alert('添加出错');
    isbusy = false;
}
function cbdel(d) {
    if (d == 1) {
        var i = idx2[theappid];
        var appname = datamy[i].app_english_name;
        datamy.splice(i, 1);
        cbgetadded();
        setaddn(theappid);
        menu.del(appname);
        updateHeight()
    } else alert('删除出错');
    isbusy = false;
}
function cbup(d) {
    if (d == 1) {
        var i = idx2[theappid];
        var appname = datamy[i].app_english_name;
        var t = datamy[i];
        datamy[i] = datamy[i - 1];
        datamy[i - 1] = t;
        idx2 = [];
        i2 = 0;
        cbgetadded(null, 1);
        menu.moveUp(appname);
    } else alert('移动出错');
    isbusy = false;
}
function cbdown(d) {
    if (d == 1) {
        var i = idx2[theappid];
        var appname = datamy[i].app_english_name;
        var t = datamy[i];
        datamy[i] = datamy[i + 1];
        datamy[i + 1] = t;
        idx2 = [];
        i2 = 0;
        cbgetadded(null, 1);
        menu.moveDown(appname);
    } else alert('移动出错');
    isbusy = false;
}
function setaddn(appid) {
    $('#ul1 li[appid=' + appid + '] span.add').replaceWith('<span class="add appicon-add"><a href="#" onclick="return false;">添加应用</a></span>');
}
function setaddy(appid) {
    $('#ul1 li[appid=' + appid + '] span.add').replaceWith('<span class="add">已添加</span>');
}
function cbsetc(d) {
    if (d) {
        lastc = thec;
        Tip.show('设置成功！') ;
		menu.check(d);
    } else {
        $('#select2')[0].selectedIndex = lastc;
        alert('设置出错！');
    }
}
var cgetted = false;
function cbgetc(d) {
    var sel = $('#select2')[0];
    sel.selectedIndex = lastc = +d ? d / 5 - 1 : 0;
    cgetted = true;
}
function cbreset(d) {
    if (d == 1) top.location.reload();
    else alert('恢复默认失败：' + d);
}
var Tip = function() {
    var div = document.getElementById('prompt_box');
    var t = 0,
    th = 0;
    function show(s) {
        clearTimeout(th);
        clearInterval(t);
        div.innerHTML = s;
        div.style.opacity = 1;
        div.style.filter = 'alpha(opacity=100)';
        div.style.display = 'block';
        th = setTimeout(hide, 1000);
    }
    var n = 100,
    v = 10,
    a = 2;
    function _hide() {
        n -= v;
        if (n <= 0) {
            clearInterval(t);
            div.style.display = 'none';
            return;
        }
        div.style.opacity = n / 100;
        div.style.filter = 'alpha(opacity=' + n + ')';
        v += a;
    }
    function hide() {
        n = 100;
        v = 1;
        t = setInterval(_hide, 50);
    }
    return {
        show: show,
        hide: hide
    };
} ();
var index = {
    baseUrl: 'http://www.1616.net/',
    add: function(appid) {
        $.getScript(G_dirUrl + 'images/manager/Ajax_value.asp?callback=cbadd&appId=' + appid+"&t="+new Date());
    },
    del: function(appid) {
        $.getScript(G_dirUrl + 'images/manager/Ajax_value.asp?callback=cbdel&appId=' + appid+"&t="+new Date());
    },
    up: function(appid) {
        $.getScript(G_dirUrl + 'images/manager/Ajax_value.asp?callback=cbup&appId=' + appid+"&t="+new Date());
    },
    down: function(appid) {
        $.getScript(this.baseUrl + 'move_app.php?callback=cbdown&direction=down&appId=' + appid);
    },
    operate: function(el, op) {
        if (!isbusy) {
            isbusy = true;
            this[op](theappid = $(el).parents('li').attr('appid'));
        }
        return false;
    },
    init: function() {
        var T = this;
        $.getScript(T.baseUrl + 'get_app_count.php?callback=cbgetc');
        var i = 0,
        cur = ($('#tabs>li').click(function() {
            if (this == cur) return;
            cur.className = '';
            $('#ul' + cur.id.substr(3)).hide(); (cur = this).className = 'on';
            $('#ul' + cur.id.substr(3)).show();
            if (i = 1 - i) {
                if (modified) {
                    $.getScript(G_dirUrl + 'images/manager/Ajax_value.asp?callback=cbgetadded'+"&t="+new Date());
                    modified = false;
                } else updateHeight();
            } else updateHeight();
        }))[i];
        $('span.add a').live('click',
        function() {
            T.operate(this, 'add');
        });
        $('span.del a').live('click',
        function() {
            T.operate(this, 'del');
        });
        $('a:has(span.appicon-up)').live('click',
        function() {
            T.operate(this, 'up');
        });
        $('a:has(span.appicon-dn)').live('click',
        function() {
            T.operate(this, 'down');
        });
        $('#btn_setc').click(function() {
            if (!cgetted) return;
            var sel = $('#select2')[0];
            if (sel.selectedIndex == lastc) return;
            thec = sel.selectedIndex;
            $.getScript(T.baseUrl + 'set_app_count.php?callback=cbsetc&count=' + ( + sel.value || 'default'));
        });
        $('#bt_reset').click(function() {
            if (confirm('该操作将取消您的所有应用设置，恢复系统初始状态。确认继续吗？')) $.getScript(G_dirUrl + 'images/manager/Ajax_value.asp?callback=cbreset'+"&t="+new Date());
        });
    }
};
parent.J.add('manager', index, window);