﻿function Auth() {
    var _this = this;

    this.authLoginAjax = "/login";
    this.authSendPassword = "/sendpassword";

    var mailRuProviderUrl = "/auth/openId?userOpenId=http://{0}.id.mail.ru";
    this.ActiveProvider = "";
    this.vkontakteCreated = false;

    this.init = function () {
        $(".authorizeID").click(function () {
            $(".provider-list").toggle();
        });

        $(".provider-list").hide();
        $(".userID_wrapper").hide();

        $("#MailRuProvider").click(function () {
            _this.userIdClear();
            _this.ActiveProvider = "mailru";
            $("#UserIdLabel").text("Введите свой mail логин");
            $(".userID_wrapper").toggle();
        });

        $("#OpenIdSubmit").click(function () {
            var login = $("#userID").val();
            if (login.length > 0) {
                if (_this.ActiveProvider == "mailru") {
                    var request = String.Format(mailRuProviderUrl, login);
                    window.location = request;
                }
            }
        });

        $("#LoginSubmit").live("click", function () {
            _this.login();
        });

        $("#SendPassword").live("click", function () {
            _this.sendPassword();
        });
    }

    this.userIdClear = function () {
        $("#userID").attr("value", "");
        _this.ActiveProvider = "";
    }

    this.login = function () {

        var ajaxData = {
            Email: $("#Email").val(),
            Password: $("#Password").val()
        };

        $.ajax({
            type: "POST",
            url: _this.authLoginAjax,
            data: ajaxData,
            success: function (data) {
                if (data.result == "ok") {
                    window.location.reload()
                }
                if (data.result == "error") {

                    var list = "";
                    for (var i = 0; i < data.errors.length; i++) {

                        var item = data.errors[i];
                        list += item.Message + "<br/>";
                    }
                    $("#osx-container #osx-modal-data .errors").html(list);
                }
            },
            error: function () {
                alert("Внутренняя ошибка");
            }
        });
    }

    this.sendPassword = function () {
        var ajaxData = {
            Email: $("#RegisterEmail").val()
        };

        $.ajax({
            type: "POST",
            url: _this.authSendPassword,
            data: ajaxData,
            beforeSend: function () {
                $("#osx-container #osx-modal-data .errors").empty();
            },
            success: function (data) {
                if (data.result == "ok") {
                    $('#SendPassword').qtip(
                   {
                       id: 'modal', // Since we're only creating one modal, give it an ID so we can style it
                       content: {
                           text: "На ваш email ("+ data.data + ") ушло письмо с паролем",
                           title: {
                               text: 'Сообщение',
                               button: true
                           }
                       },
                       position: {
                           my: 'center', // ...at the center of the viewport
                           at: 'center',
                           target: $(window)
                       },
                       show: {
                           event: false,
                           ready: true,
                           solo: true, // ...and hide all other tooltips...
                           modal: true // ...and make it modal
                       },
                       hide: false,
                       style: 'ui-tooltip-light ui-tooltip-rounded'
                   });

                }
                if (data.result == "error") {

                    var list = "";
                    for (var i = 0; i < data.errors.length; i++) {

                        var item = data.errors[i];
                        list += item.Message + "<br/>";
                    }
                    $("#osx-container #osx-modal-data .errors").html(list);
                }
            },
            error: function () {
                alert("Внутренняя ошибка");
            }
        });
    }
}

var auth;
$().ready(function () {
    auth = new Auth();
    auth.init();
});

