/*!
* jQuery core plugin
* Version 1.0.0   
* @requires jQuery v1.7.1 or later
* 
*Copyright (c) 2012 @cyril(王海龙)
*/


$.ajaxSetup({
    global: false,
    type: "POST",
    crossDomain: false
});


(function($) {
    $.extend({
        ui: {
            initBar: function(barStatus) {
                var recAppUl = $('#recApps');
                var recBack = $('.recommendlistback');

                if (barStatus == "1") {
                    recAppUl.css('display', 'block');
                    recBack.css('display', 'block');
                    $('.tag').removeClass('tag-hack').find('em').removeClass();
                } else {
                    recAppUl.css('display', 'none');
                    recBack.css('display', 'none');
                    $('.tag').addClass('tag-hack').find('em').addClass('down');
                }

                if (recAppUl.css('display') == 'block') {
                    $('#recApp').toggle(
                        function() {
                            recAppUl.hide();
                            recBack.hide();
                            $('.tag').addClass('tag-hack').find('em').addClass('down');
                            $.http.saveRecBar(0);
                            $.common.autoSize();
                        },
                        function() {
                            recAppUl.show();
                            recBack.show();
                            $('.tag').removeClass('tag-hack').find('em').removeClass();
                            $.http.saveRecBar(1);
                            $.common.autoSize();
                        }
                    )
                } else {
                    $('#recApp').toggle(
                        function() {
                            recAppUl.show();
                            recBack.show();
                            $('.tag').removeClass('tag-hack').find('em').removeClass();
                            $.http.saveRecBar(1);
                            $.common.autoSize();
                        },
                        function() {
                            recAppUl.hide();
                            recBack.hide();
                            $('.tag').addClass('tag-hack').find('em').addClass('down');
                            $.http.saveRecBar(0);
                            $.common.autoSize();
                        }
                    )
                }
            },

            initNav: function() {

                $('#recApps').click(function() {
                    $(this).siblings().removeClass('on');
                    $(this).addClass('on');

                    $('#apps').hide();
                    $('#detail').hide();
                    $('#recApp').show();

                    $.http.setCounter('501400003');
                });

                $('#allApps').click(function() {

                    $(this).siblings().removeClass('on');
                    $(this).addClass('on');

                    $('#apps').show();
                    $('#detail').hide();
                    $('#recApp').hide();

                    $.http.setCounter('501400004');
                });

                $('#specialPage').click(function() {

                    $(this).siblings().removeClass('on');
                    $(this).addClass('on');

                    $('#special').show();

                    $('#apps').hide();
                    $('#detail').hide();
                    $('#recApp').hide();

                    $.http.setCounter('501400005');
                });

                var tabType = $.common.GetQueryString("tabType");
                FXC.Data._tabType = tabType;
                if (tabType == 1) {
                    $('#recApps').click();
                } else if (tabType == 2) {
                    $('#allApps').click();
                    // 还原默认值
                    FXC.Data._tabType = 1;
                }
            },

            initRecApp: function(recApps) {
                var strContent = '';

                for (var i = 0; i < recApps.length; i++) {
                    strContent += '<li app="' + recApps[i].AppId + '" RegId="' + recApps[i].RegId + '" OpenRegId="' + recApps[i].OpenRegId + '"><img src="' + FXC.Url.icon + recApps[i].Icon + '"><span>' + recApps[i].Name + '</span></li>';
                }

                $('#recApps').html(strContent);

                $('#recApps li').bind('click', function() {
                    var appItem = FXC.Data._recommAppMap.get($(this).attr("app"));
                    $.API.openApp($.common.obj2str(appItem));

                    $.http.setCounter($(this).attr("OpenRegId"));
                    $.http.setCounter($(this).attr("RegId"));
                }).hover(function() {
                    $(this).siblings().removeClass('on');
                    $(this).addClass('on');
                }, function() {
                    $(this).removeClass();
                });
            },

            initUserApp: function(userApps) {
                FXC.Data.__userAppTree = $.fn.AppTree({ target: "apps_list" });
                FXC.Data.__userAppTree.createAppList(userApps);
                FXC.Data.__userAppTree.bindEventToItem();
            },

            showPanel: function(showDivId, appId) {

                if (showDivId == "apps") {
                    $('#apps').show();
                    $('#detail').hide();
                    $('#recApp').hide();
                } else if (showDivId == "detail") {
                    $('#apps').hide();
                    $('#detail').show();
                    $.http.getAppDetail(appId);
                    $('#recApp').hide();
                } else {
                    $('#apps').hide();
                    $('#detail').hide();
                    $('#recApp').show();
                };
            },

            updateMoreApps: function(newAppCount) {

                if (newAppCount > 0 && newAppCount < 9) {
                    $('#moreApps em').remove();
                    $('#moreApps div').append($('<em>' + newAppCount + '</em>'));
                } else if (newAppCount == 0) {
                    $('#moreApps em').remove();
                } else if (newAppCount > 9) {
                    $('#moreApps em').remove();
                    $('#moreApps div').append($('<em>9+</em>'));
                }

                if (newAppCount > 0) {

                    // autoScroll
                    var userList = $('#divAllAppList');
                    var h3 = userList[0].scrollHeight;
                    var h2 = userList[0].clientHeight;

                    userList.scrollTop(h3 - h2);
                }


                if (newAppCount > 0 && newAppCount < 9) {
                    $('#firstGet em').remove();
                    $('#firstGet').append($('<em>' + newAppCount + '</em>'));
                } else if (newAppCount == 0) {
                    $('#firstGet em').remove();
                } else if (newAppCount > 9) {
                    $('#firstGet em').remove();
                    $('#firstGet').append($('<em>9+</em>'));
                }

            },

            initSortBar: function() {
                var liItems = $('.app-sort').find('li');

                liItems.click(function() {
                    $(this).siblings().removeClass('on');
                    $(this).addClass('on');

                    FXC.Data.__appInfoTree.removeAppList();
                    FXC.Data.pageIndex = 0;

                    var sort = $(this).attr('sort');
                    $.http.getAppList(FXC.Data.pageIndex, sort);

                    if (sort == 'UserCount') {
                        $.http.setCounter('501600001');
                    } else if (sort == 'JoinTime') {
                        $.http.setCounter('501600003');
                    } else if (sort == 'Level') {
                        $.http.setCounter('501600002');
                    }

                });

            },

            loadAppList: function(apps) {

                FXC.Data.__appInfoTree.createAppList(apps);
            },

            initEditorApp: function(recApps) {
                var appUl = $('#editorApp');
                var contentStr = '';
                for (var i = 0; i < recApps.length; i++) {
                    FXC.Data._editorAppMap.put(recApps[i].AppId, recApps[i]);

                    var appStr = $.common.obj2str(recApps[i]);

                    contentStr += '<li app="' + recApps[i].AppId + '"><a onclick= \'OpenAppPc1(' + appStr + ')\' href="javascript:void(0)"><img src="' + FXC.Url.icon + recApps[i].Icon + '"></a>';
                    contentStr += '<dl><dt><a onclick= \'OpenAppPc1(' + appStr + ')\' href="javascript:void(0)">' + recApps[i].Name + '</a></dt><dd><p class="score">';

                    var starCount = 0;
                    var starLevel = (recApps[i].Level / 2).toString().replace('.5', '.0');
                    for (var j = 0; j < starLevel; j++) {
                        contentStr += '<em class="pick"></em>';
                        starCount++;
                    }

                    if (recApps[i].Level % 2 == 1) {
                        contentStr += '<em class="pick1"></em>';
                        starCount++;
                    }

                    for (var ii = 0; ii < 5 - starCount; ii++) {
                        contentStr += '<em class=""></em>';
                    }

                    if (recApps[i].Added == "1") {
                        var appStr = $.common.obj2str(recApps[i]);
                        contentStr += '</p></dd><dd><a href="javascript:void(0)" class="btn_lk2" onclick= \'OpenAppPc(' + appStr + ')\'></a></dd></dl></li>';
                    } else {
                        contentStr += '</p></dd><dd><a href="javascript:void(0)" class="btn_lk1" RegId=' + recApps[i].AddRegId + '></a></dd></dl></li>';
                    }
                }
                appUl.html(contentStr);

                appUl.find('.btn_lk1').click(function() {

                    var appId = $(this).parent().parent().parent().attr('app');
                    $.http.addApp(appId, "editor");
                    $.http.setCounter($(this).attr('RegId'));
                });

                var liItems = appUl.find('li');

                liItems.hover(function() {
                    $(this).siblings().removeClass('on');
                    $(this).addClass('on');
                }, function() {
                    $(this).removeClass();
                });

            },

            initSpeApps: function(apps) {

                var contentStr = '';
                for (var i = 0; i < apps.length; i++) {

                    FXC.Data._speAppMap.put(apps[i].AppId, apps[i]);

                    var appStr = $.common.obj2str(apps[i]);

                    if (i == 0) {
                        contentStr += '<div class="fl">';
                    } else if (i == 2) {
                        contentStr += '<div class="fr">';
                    }

                    contentStr += '<dl app="' + apps[i].AppId + '"><dt><a onclick= \'OpenAppPc(' + appStr + ')\' href="javascript:void(0)"><img src="' + FXC.Url.icon + apps[i].Icon + '" /></a></dt>';
                    contentStr += '  <dd><div class="tit"><h2><a onclick= \'OpenAppPc(' + appStr + ')\' href="javascript:void(0)">' + apps[i].Name + '</a></h2><p class="score">';

                    var starCount = 0;
                    var starLevel = (apps[i].Level / 2).toString().replace('.5', '.0');
                    for (var j = 0; j < starLevel; j++) {
                        contentStr += '<em class="pick"></em>';
                        starCount++;
                    }

                    if (apps[i].Level % 2 == 1) {
                        contentStr += '<em class="pick1"></em>';
                        starCount++;
                    }

                    for (var ii = 0; ii < 5 - starCount; ii++) {
                        contentStr += '<em class=""></em>';
                    }

                    if (apps[i].Added == "1") {
                        contentStr += '</p><a onclick= \'OpenAppPc(' + appStr + ')\'  href="javascript:void(0)" class="btn_lk2 f"></a></div><p class="text">' + apps[i].Description + '</p></dd></dl>';
                    } else {
                        contentStr += '</p><a RegId=' + apps[i].AddRegId + '  href="javascript:void(0)" class="btn_lk1 f"></a></div><p class="text">' + apps[i].Description + '</p></dd></dl>';
                    }

                    if (i == 1 || i == 3) {
                        contentStr += '</div>';
                    }
                }

                $('#speApps').html(contentStr);

                $('#speApps').find('.btn_lk1').click(function() {

                    var appId = $(this).parent().parent().parent().attr('app');
                    $.http.addApp(appId, "special");
                    $.http.setCounter($(this).attr('RegId'));
                });

            },

            initSpePic: function(data) {

                $('#speImg').attr('src', FXC.Url.icon + data.Pic)
                $('#speDesc').html(data.Desc)
            },

            initPic: function(pics) {
                var contentStr = '';
                for (var i = 0; i < pics.length; i++) {
                    var appStr = $.common.obj2str(pics[i]);
                    contentStr += '<div app=' + pics[i].AppId + '><a onclick= \'OpenAppPc1(' + appStr + ')\' href="javascript:void(0)"><img src="' + FXC.Url.icon + pics[i].Pic + '" alt="" width="387px"></a></div>';
                }

                $('.items').html(contentStr);
            },

            initAppDetail: function(appItem) {

                // 数据上报
                appItem.OpenRegId = "501700003";
                var appStr = $.common.obj2str(appItem);

                var contentStr = '<dt><img onclick= \'OpenAppPc(' + appStr + ')\' src=' + FXC.Url.icon + appItem.Icon + '></dt>';
                contentStr += '<dd class="dd1"><a onclick= \'OpenAppPc(' + appStr + ')\' href="javascript:void(0)">' + appItem.Name + '</a>';

                if (appItem.Added == "1") {
                    // 数据上报
                    appItem.OpenRegId = "501700004";
                    var appStr = $.common.obj2str(appItem);
                    contentStr += '<a class="btn_lk4" onclick= \'OpenAppPc(' + appStr + ')\' href="javascript:void(0)"></a>';
                } else {
                    contentStr += '<a class="btn_lk3" href="javascript:void(0)"></a>';
                }

                contentStr += '</dd><dd class="dd2"><p class="score">';

                var starCount = 0;
                var starLevel = (appItem.Level / 2).toString().replace('.5', '.0');
                for (var j = 0; j < starLevel; j++) {
                    contentStr += '<em class="pick"></em>';
                    starCount++;
                }

                if (appItem.Level % 2 == 1) {
                    contentStr += '<em class="pick1"></em>';
                    starCount++;
                }

                for (var ii = 0; ii < 5 - starCount; ii++) {
                    contentStr += '<em class=""></em>';
                }

                contentStr += '</p><span class="right"><em>' + appItem.UserCount + '</em>人已使用</span></dd>';

                $('.app-show').html(contentStr);
                $('#appDesc').html($.common.cnSubstr(appItem.Description, 200));
                $('.backright').click(function() {

                    $('#apps').show();
                    $('#allApps').click();
                    $('#detail').hide();
                    $('#recApp').hide();

                    $.http.setCounter('501700002');
                });

                var imgStr = '<ul>';
                for (var ii = 0; ii < appItem.AppPic.length; ii++) {
                    imgStr += '<li><img style="height:164px" src="' + FXC.Url.icon + appItem.AppPic[ii].Pic + '"/></li>';
                }

                imgStr += "</ul>";

                $('#items').html(imgStr);

                if (appItem.AppPic.length > 0) {
                    //                    $("#scrollable").scrollable({circular: true});
                    $('#scrollable #items').jCarouselLite({
                        btnNext: ".app-focus .next",
                        btnPrev: ".app-focus .prev"
                    });
                }

                $('.dd1 .btn_lk3').click(function() {

                    $.http.addApp(appItem.AppId, "detail");
                    $.http.setCounter('501700001');
                });
            },

            tip: function(msg) {
                $.XYTipsWindow({
                    ___title: "无边框无标题",
                    ___showTitle: false,
                    ___showBoxbg: false,
                    ___time: 2000,
                    ___content: 'text:<div class="layer"><div class="layer-bg"></div><div class="layer-ct"><em class="red"></em><p>' + msg + '</p></div></div>'
                });
            }

        },

        common: {

            getViewport: function() {
                var ua = window.navigator.userAgent.toLowerCase(),
                isStrict = document.compatMode == "CSS1Compat",
                isOpera = ua.indexOf("opera") > -1,
                isIE = !isOpera && ua.indexOf("msie") > -1;
                var w = isIE ? (isStrict ? document.documentElement.clientWidth : document.body.clientWidth) : self.innerWidth;
                var h = isIE ? (isStrict ? document.documentElement.clientHeight : document.body.clientHeight) : self.innerHeight;
                return { width: w, height: h };
            },

            autoSize: function() {

                //自适应窗口
                try {
                    //var h = $('.recommend').css('height').replace("px","");
                    var viewport = $.common.getViewport();
                    var height = viewport.height; // - h - 20 ; //document.body.clientHeight - h;
                    //	                var width = viewport.width - v; //document.body.clientWidth - v;

                    //                    $('#divAllAppList ul').css('height', height - 15);     
                    $('#divAllAppList').css('height', height - 10);
                }
                catch (e) {
                    top.status = e.message;
                }
            },

            //获取url中的参数
            GetQueryString: function(name) {
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
                var r = window.location.search.substr(1).match(reg);
                if (r != null) return unescape(r[2]); return null;
            },

            // 计算字节长度（中文和全角等字符算2个长度）
            cnLength: function(str) {
                if (str == null) {
                    return null;
                }
                var l = 0;
                for (var k = 0; k < str.length; k++) {
                    if (str.charCodeAt(k) > 255)
                        l += 2;
                    else
                        l++;
                }
                return l;
            },

            // 按字节长度的字符串
            cnSubstr: function(str, len, nopoint) {
                if (str == null) {
                    return null;
                }
                if ($.common.cnLength(str) <= len + 2 && !nopoint) {
                    return str;
                }
                var strlen = 0;
                var s = "";
                if (str != null) {
                    for (var k = 0; k < str.length; k++) {
                        if (str.charCodeAt(k) > 255)
                            strlen += 2;
                        else
                            strlen++;

                        if (strlen <= len) {
                            s += str.charAt(k);
                        } else {
                            if (nopoint)
                                return s;
                            else
                                return s + "...";
                        }
                    }
                }
                return s;
            },
            forbidden: function() {
                // 禁止右键
                $(document).bind("contextmenu", function(e) {
                    e.target = e.srcElement || document;
                    if (e.target.nodeName == 'INPUT') {//只有输入框里有右键菜单
                        return true;
                    } else {
                        return false;
                    }
                });
                // 禁止选择文字
                $(document).bind("selectstart", function() {
                    return false;
                });
                //禁止F5刷新
                document.onkeydown = function(e) {
                    e = e || event;
                    if (e.keyCode == 116) {// || e.ctrlKey
                        e.keyCode = 0;
                        e.cancelBubble = true;
                        return false;
                    }
                }
            },
            // 停止事件冒泡
            stopPropagation: function(e) {
                e = window.event || e;
                if (window.event) {
                    e.cancelBubble = true;
                } else {
                    e.stopPropagation();
                }
            },

            // JS模拟的HashMap
            jHashMap: function() {

                /** Map 大小 **/
                var size = 0;

                /** 对象 **/
                var entry = new Object();

                /** 存 **/
                this.put = function(key, value) {
                    if (!this.containsKey(key)) {
                        size++;
                    }
                    entry[key] = value;
                }

                /** 取 **/
                this.get = function(key) {
                    return this.containsKey(key) ? entry[key] : null;
                }

                /** 删除 **/
                this.remove = function(key) {
                    if (this.containsKey(key) && (delete entry[key])) {
                        size--;
                    }
                }

                /** 是否包含 Key **/
                this.containsKey = function(key) {
                    return (key in entry);
                }

                /** 是否包含 Value **/
                this.containsValue = function(value) {
                    for (var prop in entry) {
                        if (entry[prop] == value) {
                            return true;
                        }
                    }
                    return false;
                }

                /** 所有 Value **/
                this.values = function() {
                    var values = new Array();
                    for (var prop in entry) {
                        values.push(entry[prop]);
                    }
                    return values;
                }

                /** 所有 Key **/
                this.keys = function() {
                    var keys = new Array();
                    for (var prop in entry) {
                        keys.push(prop);
                    }
                    return keys;
                }

                /** Map Size **/
                this.size = function() {
                    return size;
                }

                /* 清空 */
                this.clear = function() {
                    size = 0;
                    entry = new Object();
                }
            },
            str2obj: function(jsonString) {
                var jsonObj = eval('(' + jsonString + ')');

                return jsonObj;
            },

            obj2str: function(o) {
                var r = [];
                if (typeof o == "string") return "\"" + o.replace(/([\'\"\\])/g, "\\$1").replace(/(\n)/g, "\\n").replace(/(\r)/g, "\\r").replace(/(\t)/g, "\\t") + "\"";
                if (typeof o == "object") {
                    if (!o.sort) {
                        for (var i in o)
                            r.push('"' + i + '"' + ":" + $.common.obj2str(o[i]));
                        if (!!document.all && !/^\n?function\s*toString\(\)\s*\{\n?\s*\[native code\]\n?\s*\}\n?\s*$/.test(o.toString)) {
                            r.push("toString:" + o.toString.toString());
                        }
                        r = "{" + r.join() + "}"
                    } else {
                        for (var ii = 0; ii < o.length; ii++)
                            r.push($.common.obj2str(o[ii]))
                        r = "[" + r.join() + "]"
                    }
                    return r;
                }
                return o.toString();
            }
        },


        http: {
            getRecApps: function(isFrist) {

                var o = new Object();
                o.url = FXC.Url.recAppList;
                o.data = { "c": $.API.getCred() };
                o.dataType = "json";
                o.success = function(data) {
                    $.http.getRecAppscallback(data, isFrist);
                };
                o.error = function(XMLHttpRequest, textStatus, errorThrown) {
                    $.API.RequsetError(404);
                }

                $.ajax(o);

            },

            getRecAppscallback: function(data, isFrist) {
                if (data.StatusCode == "200") {
                    if (isFrist) {
                        $.ui.initPic(data.AppRoll);
                        var root = $("#chained").scrollable({ circular: true }).autoscroll({ autoplay: true });
                    }

                    $.ui.initEditorApp(data.RecommApp);
                }
            },

            saveApp: function(orderStr) {
				//
                var o = new Object();
                o.url = FXC.Url.saveApp;
                o.data = { "AppSortOrder": orderStr, "c": $.API.getCred() };
                o.dataType = "json";
                o.success = function(data, textStatus) {
                    $.http.saveAppcallback(data);
                    $.http.setCounter('501100002');
                };
                o.error = function(XMLHttpRequest, textStatus, errorThrown) {

                }

                $.ajax(o);
            },

            saveAppcallback: function(data) {

            },

            saveRecBar: function(barStatus) {
				//
                var o = new Object();
                o.url = FXC.Url.saveRecBar;
                o.data = { "BarStatus": barStatus, "c": $.API.getCred() };
                o.dataType = "json";
                o.success = function(data, textStatus) {
                    $.http.saveRecBarcallback(data);
                    if (barStatus == 0) {
                        $.http.setCounter('501100003');
                    } else {
                        $.http.setCounter('501100004');
                    }
                };
                o.error = function(XMLHttpRequest, textStatus, errorThrown) {

                }

                $.ajax(o);
            },

            saveRecBarcallback: function(data) {

            },

            removeApp: function(appId, appItem) {
                var o = new Object();
                o.url = FXC.Url.removeApp;
                o.data = { "AppId": appId, "c": $.API.getCred() };
                o.dataType = "json";
                o.success = function(data, textStatus) {
                    $.http.removeAppcallback(data, appItem);
                    $.http.setCounter('501100001');
                };
                o.error = function(XMLHttpRequest, textStatus, errorThrown) {

                    $.ui.tip('删除失败，请重试');
                }

                $.ajax(o);
            },

            removeAppcallback: function(data, appItem) {
                if (data.StatusCode == "200") {
                    var appItemObj = FXC.Data._userAppMap.get(appItem.attr("app"));
                    $.API.DeleteAppToPC($.common.obj2str(appItemObj));

                    appItem.remove();
                } else if (data.StatusCode == "500") {

                    $.ui.tip('删除失败，请重试');
                }
            },

            addApp: function(appId, panel) {//收藏一个应用
				$.API.getCred();
                var o = new Object();
                o.url = FXC.Url.addApp;
                o.data = { "AppId": appId, "c": cred };
                o.dataType = "json";
                o.success = function(data, textStatus) {
                    $.http.addAppcallback(data, appId, panel);
                };
                o.error = function(XMLHttpRequest, textStatus, errorThrown) {
                    $.ui.tip('添加失败，请重试');
                }
                $.ajax(o);
            },

            addAppcallback: function(data, appId, panel) {
                if (data.StatusCode == "200") {
					var tempApp = FXC.Data._allAppMap.get(appId);
					if(tempApp){
						tempApp.Added="1";
						FXC.Data._allAppMap.put(appId,tempApp);
						FXC.Data.__userAppTree.deleteAppItem(appId);
						FXC.Data.__userAppTree.insertAppItem(tempApp);
					}
                } else if (data.StatusCode == "500") {
                    $.ui.tip('添加失败，请重试');
                }
            },

            getAppList: function(pageIndex, sort, isFirst) {

                var o = new Object();
                o.url = FXC.Url.appList;
                o.data = { "sort": sort, "c": $.API.getCred(), "pageIndex": pageIndex };
                o.dataType = "json";
                o.async = false;
                o.success = function(data) {
                    $.http.getAppListcallback(data, isFirst);
                };
                o.error = function(XMLHttpRequest, textStatus, errorThrown) {
                    $.API.RequsetError(404);
                }

                $.ajax(o);

            },

            getAppListcallback: function(data, isFirst) {

                if (data.StatusCode == "200") {
                    //                    if(data.Sort=="JoinTime"&&isFirst==true){       
                    //                        $('#allApps').click();
                    //                    }else if(isFirst==true){
                    //                        $('#recApps').click();
                    //                    }

                    $.ui.loadAppList(data.App);
                    $('.app-sort li').each(function() {
                        if ($(this).attr('sort') == data.Sort) {
                            $(this).siblings().removeClass('on');
                            $(this).addClass('on');
                        }
                    })
                } else if (data.StatusCode == "204") {
                    $('#loading').remove();
                }

            },

            getAppDetail: function(appId) {

                var o = new Object();
                o.url = FXC.Url.appDetail;
                o.data = { "AppId": appId, "c": $.API.getCred() };
                o.dataType = "json";
                o.success = function(data) {
                    $.http.getAppDetailcallback(data);
                };
                o.error = function(XMLHttpRequest, textStatus, errorThrown) {
                    $.API.RequsetError(404);
                }

                $.ajax(o);
            },

            getAppDetailcallback: function(data) {
                if (data.StatusCode == "200") {
                    FXC.Data._detailAppMap.clear();
                    FXC.Data._detailAppMap.put(data.AppId, data);
                    $.ui.initAppDetail(data);
                }
            },

            getQueryString: function(name) {
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
                var r = window.location.search.substr(1).match(reg);
                if (r != null) return unescape(r[2]);
                return null;
            },

            getUserData: function(userApps) {//获取应用魔方应用方法
                var ver = $.http.getQueryString("v");
                var o = new Object();
                o.url = FXC.Url.userData;
                o.data = { "c": cred, "v": ver };
                o.dataType = "json";
                o.success = function(data, status, xhr) {
                    if (data.StatusCode == "200") {
                        $.http.getUserDatacallback(data, userApps);
                    } else {
                        $.API.RequsetError(404);
                    }
                };
                o.error = function(XMLHttpRequest, textStatus, errorThrown) {
					//alert(textStatus);
                    $.API.RequsetError(404);
                }
                $.ajax(o);
            },
            getUserDatacallback: function(data, userApps) {
                if (data.StatusCode == "200") {
                    $.merge(userApps, data.UserApp);
					//
					for(var k=0;k<userApps.length;k++){
						FXC.Data._allAppMap.put(userApps[k].AppId,userApps[k]);
					}
					
                    $.ui.initUserApp(userApps);
                    $.common.autoSize();
                }
            },

            getRecommAppList: function() {

                var o = new Object();
                o.url = FXC.Url.recommAppList;
                o.data = { "c": $.API.getCred() };
                o.dataType = "json";
                o.success = function(data) {
                    if (data.StatusCode == "200") {
                        $.http.getRecommAppListcallback(data);
                    } else {
                        $.API.RequsetError(404);
                    }
                };
                o.error = function(XMLHttpRequest, textStatus, errorThrown) {
                    $.API.RequsetError(404);
                }

                $.ajax(o);
            },

            getRecommAppListcallback: function(data) {
                if (data.StatusCode == "200") {

                    if (data.Special.HasData == 'true') {
                        $('#specialPage').show();
                        $.ui.initSpeApps(data.Special.Apps);
                        $.ui.initSpePic(data.Special);
                    } else {
                        $('#specialPage').remove();
                    }
                }
            },

            getAppCategory: function() {

                var o = new Object();
                o.url = FXC.Url.appCategory;
                o.data = { "c": $.API.getCred() };
                o.dataType = "json";
                o.success = function(data) {

                    if (data.StatusCode == "200") {
                        $.http.getAppCategorycallback(data);
                    }
                };
                o.error = function(XMLHttpRequest, textStatus, errorThrown) {

                }

                $.ajax(o);
            },

            getAppCategorycallback: function(data) {
                if (data.StatusCode == "200") {

                    $.ui.initAppCat(data.AppCategory);
                }
            },

            setCounter: function(counterId) {

                var o = new Object();
                o.url = FXC.Url.setCounter;
                o.data = { "c": $.API.getCred(), "countertype": counterId };
                o.dataType = "json";
                o.success = function(data) {
                    $.http.setCountercallback(data);
                };
                o.error = function(XMLHttpRequest, textStatus, errorThrown) {
                    //                    alert('error');                    
                }

                $.ajax(o);
            },
            setCountercallback: function(data) {

            }

        },

        // 客户端接口 
        API: {
            openApp: function(appItemStr) {
                window.external.AppBoxClicked(appItemStr);
            },
            clickMoreApp: function(tabType) {
                window.external.ClickMoreApp('AppStore.htm?tabType=' + tabType);
            },
            getCred: function() {
                window.external.GetM161Cridential();
            },
            getDomain: function() {
                return "http://webim.feixin.10086.cn";
            },
            IsInstalled: function(a) {
                return window.external.IsInstalledThirdApp(a);
            },
            AddAppToPC: function(appItemStr) {
                window.external.AddAppToPC(appItemStr);
            },
            DeleteAppToPC: function(appItemStr) {
                window.external.DeleteApp(appItemStr);
            },
            RefreshAppStatus: function(appId) {
                window.external.RefreshAppItem(appId);
            },
            RequsetError: function(strErrorCode) {
                window.external.RequsetErrorCode(strErrorCode);
            },
            GetImageUrl: function() {
                return "http://download.fetion-portal.com/AppBox/";
                //return window.external.GetServerImageUrl();
            },
            GetLocalDir: function() {
                //return window.external.GetLocalJavaScriptDir();                
            }

        }
    });
})(jQuery);


//V11
var FXC = new Object();
var cred = '';
FXC.domain = $.API.getDomain();
FXC.IconDomain = $.API.GetImageUrl();
FXC.localDir = $.API.GetLocalDir();

FXC = {
    Data: {
        _userAppMap: new $.common.jHashMap(),
        _recommAppMap: new $.common.jHashMap(),
        _allAppMap: new $.common.jHashMap(),
        _editorAppMap: new $.common.jHashMap(),
        _detailAppMap: new $.common.jHashMap(),
        _speAppMap: new $.common.jHashMap(),
        _tabType: 1,
        __appInfoTree: null,
        __userAppTree: null,       
        bar: 1,
        pageIndex: 0
    },
    Url: {
        userData: FXC.domain + "/appboxweb/getuserdata",
        recAppList: FXC.domain + "/AppBoxWeb/GetRecommAppList",
        appDetail: FXC.domain + "/AppBoxWeb/GetAppDetail",
        appList: FXC.domain + "/AppBoxWeb/GetAppList",
        addApp: FXC.domain + "/AppBoxWeb/AddApp",
        removeApp: FXC.domain + "/AppBoxWeb/RemoveApp",
        saveApp: FXC.domain + "/AppBoxWeb/SaveAppOrder",
        saveRecBar: FXC.domain + "/AppBoxWeb/SaveRecommBarStatus",
        recommAppList: FXC.domain + "/AppBoxWeb/GetRecommAppList", 
        setCounter: FXC.domain + "/AppBoxWeb/SetCounter",        
        icon: FXC.IconDomain,
        localDir: FXC.localDir
    }
};

function GetDetail(appId) {
    $.http.setCounter('501600007');
    $.ui.showPanel('detail', appId);
}

function OpenAppPc(appItem) {
    
    $.http.setCounter(appItem.OpenRegId);
    $.API.openApp($.common.obj2str(appItem));
}

function OpenAppPc1(appItem) {

    $.http.setCounter(appItem.OpenRegId);
    $.http.setCounter(appItem.RegId);
    $.API.openApp($.common.obj2str(appItem));
}

function OpenAppPc2(appItem) {
    
    var item = FXC.Data.__appInfoTree.getAppItem(appItem.AppId);
    var btnClass = item.find('div a').attr('class');

    if (btnClass == "btn_lk3") {
        appItem.Added = "0";        
    } else if (btnClass == "btn_lk4") {
        appItem.Added = "1";
    }

    $.http.setCounter(appItem.OpenRegId);
    $.API.openApp($.common.obj2str(appItem));
}

/*由客户端调用的接口--Start*/

/*
*获取应用魔方应用方法
*/
function SetAppBoxData(userApps) {
	$.http.getUserData(userApps);
}

//获取用户凭证
function SetM161Cridential(cString) {
    cred = cString;
}

/*保存用户收藏应用标志
*参数说明:
*str:{"AppId":"1","RegId":"2","Icon":"3","Name":"4"}
*/
function StoreupRecommendApp(str){
	if(!str){
		return false;
	}
	var app = str;
	if(app.Added && app.Added==="1")
		return false;
	app.Added = "1";
	//save app to user
	$.http.addApp(app.AppId, "apps");
	
	
	return true;
}

/*由客户端调用的接口--End*/