; (function (w, $) {
    w.LOGIN = {
        url: "/account/_ssologin/",
        ssoValidaUrl: "/sso/validatesso/",
        ssoAvataUrl: "/sso/ssologinjs/",
        containerid: 'login_modal',
        ssoHost:'',
        dowork: null,
        quitAction: null,
        funcArgs: null,
        QueryMsg: _locModel.T.T_QueryToLogin || "Your must sign to continue, sign in now?"
    };

    LOGIN.show = function (dowork, funcArgs, opt) {
        LOGIN.onquit();
        this.dowork = dowork;
        this.funcArgs = funcArgs;

        $.get(this.url, function (data) {
            var me = LOGIN;
            if (!me.$container) {
                $container = $("<div id='" + me.containerid + "'></div>");

                $container.append("<div class='login_model_mask' {0}></div>".format(opt && opt.disableShadeClose ? "" :"onclick='LOGIN.hide()'"));

                $("body").append($container);
            }
            $container.append(data);
            if (!me.$container) me.$container = $container;
        }.bind(opt));
    };


    LOGIN.hide = function () {
        this.onquit();
    };

    LOGIN.submit = function (formId) {
        $form = $('#' + formId);

        this.ssoHost = $form[0].getAttribute("data-host");

        if (!$form.find("#Email")[0].value.trim()) {
            LOGIN.callback({ Code: -1, Message: _locModel.T.T_LOGIN_ERR_AccountRequired || "Account is required." });
            return;
        }

        if (!$form.find("#Password")[0].value.trim()) {
            LOGIN.callback({ Code: -1, Message: _locModel.T.T_LOGIN_ERR_PasswordRequired || "Password is required." });
            return;
        }

        var options = {
            url: $form[0].action,
            datatype: "application/json",
            headers: {

            },
            success: function (data) {
                if (data.Status == 0) {
                    LOGIN.callback(data);//登陆成功不回调【反馈102】
                    LOGIN.hide();
                    //location.reload();
                }
                else {
                    LOGIN.callback({ Code: -1, Message: data.Message});
                }
            }
        };
        $form.ajaxSubmit(options);
    };

    //{Code,Status,Key}
    LOGIN.callback = function (data) {
        console.log(data);
        if (data.Code === 0) {
            addScript(document.head, this.ssoHost + this.ssoValidaUrl + "?key=" + data.Key);
        }
        else if (data.Code == 6) {
            //email not bind
            addScript(document.head, this.ssoHost + this.ssoValidaUrl + "?key={0}&opr=2".format(data.Key));
        }if (data.Code == -1) {
            var msg = "<ul class='alert--error'><li>"+_locModel.T.T_LOGIN_ERR_AccountRequire+"</li></ul>";
            $("#" + LOGIN.containerid + " .mgs_login").html(msg);
        }
        else {
            var msg = "<ul class='alert--error'><li>"+data.Message+"</li></ul>";
            $("#" + LOGIN.containerid + " .mgs_login").html(msg);
        }
    };

    LOGIN.refreshIcon = function () {
        if (window.ssoContent) {
            $("#login_icon").html(ssoContent);
        }

        return this;
    };

    LOGIN.failed = function () {
        this.hide();
    };

    LOGIN.success = function () {
        if (this.dowork) {
            if (this.funcArgs) {
                if (this.funcArgs instanceof Array)
                    this.dowork.apply(null, this.funcArgs);
                else
                    this.dowork(this.funcArgs);
            }
            else
                this.dowork();
        }

        this.refreshIcon().hide();
    };

    LOGIN.showIcon = function () {
        this.refreshIcon().hide();
    }

    LOGIN.opReqEmail = function (data) {
        if (data && data.emailRequired) {
            LOGIN.emailrequires();
            return true;
        }
        return false;
    }

    //jump to email binding page
    LOGIN.emailrequires = function () {
        InfoUI.show(3);
    }

    LOGIN.cancel = function () {
        this.hide();
    }

    LOGIN.onquit = function () {
        var c = _$(LOGIN.containerid);
        if (c) c.parentNode.removeChild(c);
        LOGIN.$container = null;

        if (this.quitAction) {
            if (this.funcArgs) {
                if (this.funcArgs instanceof Array)
                    this.quitAction.apply(null, this.funcArgs);
                else
                    this.quitAction(this.funcArgs);
            }
            else
                this.quitAction();
        }
    }

    //登录限制
    LOGIN.check = function (dowork, funcArgs, opt) {
        var ret = this.isLogin(opt);
        if (ret == -1) {
            this.show(function () { location.reload(); }, funcArgs, opt);
            return false;
        } else if (ret == 2) {
            return false;
        }

        return true;
    }

    LOGIN.checkAndQuary = function (dowork, funcArgs, quitAction) {
        var ret = this.isLogin();
        if (ret == -1) {
            this.dowork = dowork;
            this.quitAction = quitAction;
            if (typeof (popUtil) != "undefined") {
                LOGIN.show(function () { location.reload(); }, funcArgs);
            }
            else {
                this.show();
            }

            return false;
        } else if (ret == 2) {
            return false;
        }

        return true;
    }

    //获取登陆状态
    LOGIN.isLogin = function (opt) {
        var ret = -1;
        $.ajax({
            url: "/account/checklogin?" + Date.parse(new Date()),
            type: "post",
            async: false,
            dataType: "json",
            success: function (data) {
                if (data && ((data.Code == 1) || (this.opt && this.opt.ignoreMail && data.emailRequired))) {
                    ret = 1;
                }
                else if (LOGIN.opReqEmail(data)) { ret = 2; }
            }.bind({opt:opt})
        });
        return ret;
    };

    LOGIN.getAvatar = function (ssoHost) {
        this.ssoHost = ssoHost;
        addScript(document.head, ssoHost + this.ssoAvataUrl);
    };

    LOGIN.onSignOut = function () {
        if (popUtil) {
            popUtil.open(popUtil.option(0, "<h2>" + _locModel.T.T_MSG_SureToSignOut + "</h2>", 5, function () { location.href = "/logoutt/"; }, 0));
        }
    }
})(window, jQuery);
