var loadFlag = false;

(function($) {

    var setInfo = $(".sys_set_list");

    var mainPage = $(".sys_set_box");

    var lastWinWidth = $(window).width();
    var lastWinHeight = $(window).height();

    mainPage.height(lastWinHeight-7);

    var lastInfoWidth = setInfo.width();
    var lastInfoHeight = setInfo.height();




    $(window).bind("resize", function() {

        var curWinWidth = $(this).width();
        var curWinHeight = $(this).height();
        if (curWinWidth != lastWinWidth || curWinHeight != lastWinHeight) {

            mainPage.height(curWinHeight);

            lastWinWidth = curWinWidth;
            lastWinHeight = curWinHeight;
        }

    });

    setInfo.bind("resize", function() {

        var curInfoWidth = $(this).width();
        var curInfoHeight = $(this).height();

    });

    //  内容私用的帮助方法集合
    var fBrowserHelper = {

        //  构建html
        buildHtml: function(item) {
            var url = item.icon.endWith("dat") ? ("fetionres:///" + item.icon) : item.icon;
            var strHtml = '<li><dl><dt><img src="' + url + '" alt="" width="44px" height="44px"></dt>';
            strHtml += '<dd class="tit"><h1>' + item.name + '</h1></dd><dd class="inf"><div class="limit_inf">' + item.desc + '</div></dd>';
            strHtml += '<dd class="btn"><input id="statusBtn_' + item.serviceId + '" type="button" serviceId="' + item.serviceId + '" status="' + item.status + '" value="' + (item.status == 1 ? '启用' : '停用') + '" class="' + (item.status == 1 ? 'sys_btn_0' : 'sys_btn') + '" />';
            if (item.status == 0) {
                strHtml += '<input id="openMsgBtn_' + item.serviceId + '" type="button" serviceId="' + item.serviceId + '" status="' + item.status + '" value="打开会话" class="sys_btn" />';
            } else {
                strHtml += '<input id="openMsgBtn_' + item.serviceId + '" type="button" serviceId="' + item.serviceId + '" status="' + item.status + '" value="打开会话" class="sys_btn" style="color:#EEEEE0;"/>';
            }
            strHtml += '</dd></dl></li>';
            return strHtml;

        }
    };

    //  飞信客户端提供的接口
    var fetionInter = {
        SetUp: function(id, status, callback) {
            //buildXml
            var input = '<?xml version="1.0" encoding="UTF-8"?><root><id>' + id + '</id><status>' + status + '</status></root>';
            //SM.ajax(1, "SetUp", input, "", function(status, outTxt, outJson) {   });
            SM.ajax(1, "SetUp", input, "", callback);
            //return window.external.SetUp(id,status,callback);
        },
        InitLoad: function() {
            SM.ajax(1, "OpenSysServiceWin", "", "", function(status, outTxt, listData) {
                var jsonData;
                //if (listData instanceof string) {
                if (listData instanceof String) {
                    jsonData = eval("(" + listData + ")");
                } else {
                    jsonData = listData;
                }
                if (jsonData && jsonData.length > 0) {
                    var strHtml;
                    for (var i = jsonData.length - 1; i > -1; i--) {
                        strHtml = fBrowserHelper.buildHtml(jsonData[i]);
                        var logItem = $(strHtml);
                        var btn = logItem.find("input");
                        btn.data("serviceId", jsonData[i].serviceId);
                        btn.data("status", jsonData[i].status);
                        btn.each(function(k, v) {
                            fetionInter.bindEvent(v)
                        })
                        setInfo.find("ul").prepend(logItem);

                    }
                }


            });
        },
        bindEvent: function(btn) {
            $(btn).click(function() {
                if (this.id == "statusBtn_" + this.getAttribute("serviceId")) {
                    if ($(this)[0].getAttribute("status") == 0) {
                        $(this)[0].setAttribute("status", 1);
                        $(this).val("启用");
                        $(this).removeClass().addClass('sys_btn_0');
                        $("#openMsgBtn_" + this.getAttribute("serviceId"))[0].style.color = "#EEEEE0";

                        $("#openMsgBtn_" + this.getAttribute("serviceId"))[0].setAttribute("status", 1)
                    } else {
                        $(this)[0].setAttribute("status", 0);
                        $(this).val("停用");
                        $(this).removeClass().addClass('sys_btn');
                        //$("#openMsgBtn_"+this.getAttribute("serviceId")).removeClass().addClass('sys_btn');
                        $("#openMsgBtn_" + this.getAttribute("serviceId"))[0].style.color = "#727272";
                        $("#openMsgBtn_" + this.getAttribute("serviceId"))[0].setAttribute("status", 0)
                    }
                    console.log("serviceId " + $(this)[0].getAttribute("serviceId"));
                    console.log("status " + $(this)[0].getAttribute("status"));
                    fetionInter.SetUp($(this)[0].getAttribute("serviceId"), ($(this)[0].getAttribute("status")) == 0 ? 0 : 1,
									function(status, outTxt, outJson) {
									    /*if(status==200){
									    }*/
									});
                } else if (this.id == "openMsgBtn_" + this.getAttribute("serviceId") && this.getAttribute("status") == 0) {
                    fetionInter.openMsg(this.getAttribute("serviceId"));
                }

            });
        },
        openMsg: function(id) {
            SM.ajax(1, "OpenChatDlg", id, "", function(status, outTxt, outJson) { });
        }
    };
    window.fxInter = fetionInter;
    $(window).load(function() {//alert('init');
        //	fetionInter.InitLoad();
    });

})(jQuery);

function AfterSMCompleted() {
    if (!loadFlag) {
        loadFlag = true;
        // console.log("sys pubblico service AfterSMCompleted");
        window.fxInter.InitLoad();
    }
}

$(document).ready(function() {
    //fxInter.InitLoad();
    // 禁止右键
    //    $(document).bind("contextmenu", function () {
    //        return false;
    //    });
    var ctrlKey = false;  //  是否按下ctrl键
    //禁止F5刷新
    document.onkeydown = function(e) {
        e = e || event;
        if (e.keyCode == 116) {

            e.keyCode = 0;
            e.cancelBubble = true;

            return false;
        }

        if (ctrlKey && e.keyCode == 67) {
            var range = document.selection.createRange();
            //alert(range.htmlText);
            //                alert(range.text);
            //                window.clipboardData.setData('Text',range.text+"123");
        }

        if (e.ctrlKey) {
            ctrlKey = true;
        }
    };
    document.onkeyup = function(e) {
        e = e || event;
        if (e.ctrlKey) {
            ctrlKey = false;
        }
    };

    String.prototype.replaceAll = function(s1, s2) {
        return this.replace(new RegExp(s1, "gm"), s2);
    };
    String.prototype.endWith = function(str) {
        if (str == null || str == "" || this.length == 0 || str.length > this.length)
            return false;
        if (this.substring(this.length - str.length) == str)
            return true;
        else
            return false;
        return true;
    };
});

