﻿//Jquery Extension used to center the login/logout and signup dialogs
jQuery.fn.centerLogin = function (xFixed, yFixed) {
    //Move the menu to the center of the screen
    this.show();
    this.css("position", "absolute");
    var top = (($(window).height() - this.children('div').eq(0).outerHeight()) / 2) + $(window).scrollTop();

    if (typeof yFixed != "undefined" && yFixed > 0)
        top = yFixed;
    this.css("top", top + "px");
    var left = (($(window).width() - this.children('div').eq(0).outerWidth()) / 2) + $(window).scrollLeft();

    if (typeof xFixed != "undefined" && xFixed > 0)
        left = xFixed;
    this.css("left", left + "px");
    this.hide();
    return this;
}

//Jquery Extension used to reset position of the login/logout and signup dialogs
jQuery.fn.resetLoginPos = function () {
    //Reset the login form to its position under the menu
    this.css("top", "35px");
    this.css("left", "160px");
    return this;
}
function ShowLoginArea(toShowObject, extraArguments) {
    hdnTargetUrl.val(extraArguments) //Set the navigation Url to forward to after login

    //Set the Top meny to static so that the z-index is never used (not shining through)
    $('.menu').parent().css('position', 'static');


    //Moves the dialogs to directly under the form. This is to solve z-index bug.
    $('#dvLogin').prependTo($('Form:eq(0)')).css('z-index', '1500').centerLogin(0, 100);
    $('#dvLogout').prependTo($('Form:eq(0)')).css('z-index', '1500').css('left', $('#dvLogin').css('left')).css('top', $('#dvLogin').css('top'));
    $('#dvSignup').prependTo($('Form:eq(0)')).css('z-index', '1500').css('left', $('#dvLogin').css('left')).css('top', $('#dvLogin').css('top'));
    $('#DialogBG').prependTo($('Form:eq(0)'));

    toShowObject.show("slow");
    return false;
}

function ShowLoginAreaWithOverlay(toShowObject, extraArguments) {
    showDialogBackground();
    ShowLoginArea(toShowObject, extraArguments);
}

function ShowHide(ToShow, ToHide) {
    $('#' + ToHide).hide('slow');
    $('#' + ToShow).show('slow');

    SetShowValues(ToShow)
}
function SetShowValues(ToShow) {
    if (ToShow == 'login-send-pass-div') {
        $('#login-title').html("HAR DU GLÖMT DITT LÖSENORD?");
        textBoxEmail.val('');
    }
    else if (ToShow == 'dvSignup') {
        $('.signUpDialogButton').removeAttr("disabled");
    }
    else {
        $('#login-title').html("LOGGA IN PÅ MIN BOSTAD");
        textBoxUsername.val('');
        textBoxPassword.val('');
    }
}

function ShowDiv(DivToShow) {
    var divToShowJQobject = $('#' + DivToShow);
    divToShowJQobject.show('slow');
    SetShowValues(DivToShow)
}

function CloseLoginDialog(objectToHide) {
    //Moves the dialogs back after the menu so that they work with the normal dropdown functionality if used without overlay
    objectToHide.slideToggle('slow', function () {
        $('#dvLogin').insertAfter($('.menu')).resetLoginPos();
        $('#dvLogout').insertAfter($('.menu')).resetLoginPos();
        $('#dvSignup').insertAfter($('.menu')).resetLoginPos();
    });

    //Set the top meny to relative so that the absolute position elements within is positioned correctly
    $('.menu').parent().css('position', 'relative');

    closeDialog(); //Close the dialog backgroundlayer
    return false;
}
function ResetPassword() {

    jQuery.ajaxSetup({
        xhr: function () {
            try {
                if (window.ActiveXObject)
                    return new window.ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) { }

            return new window.XMLHttpRequest();
        }
    });

    $.ajax({
        type: 'POST',
        url: '/WebServices/HelperService.asmx/ResetPassword',
        data: '{ "email" : "' + textBoxEmail.val() + '"}',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        async: false,
        success: function (msg) {
            if (msg.d == true) {
                messageDivForgotPass.show();
                messageDivForgotPass.html('Nytt lösenord skickat till ' + textBoxEmail.val().trim());
            }
            else {
                messageDivForgotPass.show();
                messageDivForgotPass.html('Det finns ingen användare med e-postadressen  ' + textBoxEmail.val().trim());
            }
        },
        error: function (msg, textStatus, errorThrown) {
            messageDivForgotPass.show();
            messageDivForgotPass.html('Kunde ej återställa lösenordet för ' + textBoxEmail.val().trim());
        }
    });
    textBoxEmail.val('');
}
