﻿/// <reference path="~/Scripts/jquery-1.5.1-vsdoc.js"/>
/// <reference path="~/Scripts/jquery-ui-1.8.11.min.js"/>

//
//
//
//
//

var popupStatus = 0;
var submitStatus = 0;
var popUp;
var forgot = 0;

//Load the Popup
function loadPopup() {
    //Load the popup only if it is hidden
    if (popupStatus == 0) {
        $("#loginPopup").show({
            effect: "blind",
            duration: 200,
            complete: function () { $('#LoginEmail').focus() }

        });
        $("#loginBackground").show();
        popupStatus = 1;
    }
}

//Disable the Popup
function disablePopup() {
    //disables popup only if it is shown and not submitting
    if (popupStatus == 1 && submitStatus == 0) {
        $("#loginPopup").hide({
            effect: "blind",
            duration: 200
        });
        $("#loginBackground").hide();
        $("#loginError").css("display", "none");
        popupStatus = 0;
    }
}

function fixBackground() {
    var windowHeight = document.documentElement.clientHeight;
    $("#loginBackground").css({
        "height": windowHeight
    });
}

function NemIDClosed() {
    window.location.reload();
}

function forgottenPassword() {
    if (forgot == 0) {
        $('#forgotPassword').show();
        $('#contactArea').hide();
        $('#forgotEmail').val($("#LoginEmail").val());
        forgot = 1;
    }
    else {
        $('#forgotPassword').hide();
        $('#contactArea').show();
        forgot = 0;
    }
}

function resetPassword() {

    $('#forgotSubmit').hide();
    $('#forgotStatus').hide();
    $.post("/glemt-adgangskode", $('#forgotEmail').serialize(), function (data) {
        $('#forgotStatus').text("Vi har sendt dig en email.");
        $('#forgotStatus').show();
    });
    return false;
}

function popUpMessage(message) {
    $("#popUpMessage").text(message);
    $("#message").animate({ opacity: 1.0 }, 100).fadeIn();
    $("#message").animate({ opacity: 1.0 }, 900).fadeOut();
}

function messageActiveSession(url, serverUrl, alias) {
    $("#popUpMessage").html("<a href=\"javascript:void()\" onclick=\"javascript:popUpBrokenChat('" + url + "','" + serverUrl + "','" + alias + "')\">Du har en ikke-afsluttet chat<br /> Klik her for at gå til denne</a>");
    $("#message").animate({ opacity: 1.0 }, 1000).fadeIn();

}

function popUpBrokenChat(url, serverUrl, alias) {
    window.location = serverUrl + "/" + alias;
    page = window.open(url, '', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=880,height=514');
    var winX = (document.all) ? window.screenLeft : window.screenX;
    var winY = (document.all) ? window.screenTop : window.screenY;
    var winW = 630, winH = 460;
    if (document.body && document.body.offsetWidth) {
        winW = document.body.offsetWidth;
        winH = document.body.offsetHeight;
    }
    if (document.compatMode == 'CSS1Compat' &&
    document.documentElement &&
    document.documentElement.offsetWidth) {
        winW = document.documentElement.offsetWidth;
        winH = document.documentElement.offsetHeight;
    }
    if (window.innerWidth && window.innerHeight) {
        winW = window.innerWidth;
        winH = window.innerHeight;
    }
    page.moveTo((winW / 2) - 440 + winX, ((winH / 2) - 257 + winY));
}

//Triggers
$(document).ready(function () {

    $("#loadingImg").hide();
    $("#loadingBox").hide();
    //LOADING POPUP
    //Click the button event!
    $("#OpenLoginButton").click(function () {
        loadPopup();
    });
    //CLOSING POPUP
    //Click the x event!
    $("#loginPopupClose").click(function () {
        disablePopup();
    });
    $("#loginBackground").click(function () {
        disablePopup();
    });
    //Press Escape event!
    $(document).keyup(function (e) {
        if (e.keyCode == 27) {
            if (popupStatus == 1) {
                disablePopup();
            }
        }
    });

    $("#loginForm").submit(function () {
        if (submitStatus == 1) {
            return false;
        }
        if (forgot == 0) {
            $("#loginSubmit").hide();
            $('#loadingImg').show();
            $("#LoginEmail").attr('readOnly', 'readonly');
            $("#LoginPassword").attr('readOnly', 'readonly');



            submitStatus = 1;
            $.post("/Account/LogIn", { Email: $("#LoginEmail").val(), Password: Sha1.hash($("#LoginPassword").val()), rememberMe: $("#RememberMe").attr('checked') }, function (data) {


                if (data.isValid) {
                    $("#popUpMessage").text("Du er nu logget ind");
                    $("#message").animate({ opacity: 1.0 }, 100).fadeIn();
                    $("#message").animate({ opacity: 1.0 }, 900).fadeOut();
                    try {
                        if (data.isFriend == "true") {



                            _gaq.push(['_setCustomVar',
                                      1,                   // This custom var is set to slot #1.  Required parameter.
                                     'logintype',     // The name acts as a kind of category for the user activity.  Required parameter.
                                     'friend',               // This value of the custom variable.  Required parameter.
                                     1                    // Sets the scope to visitor-level.  Optional parameter.
                                   ]);


//                            _gaq.push(['_setCustomVar',
//                                      2,                   // This custom var is set to slot #2.  Required parameter.
//                                     'firstlogin',     // The name acts as a kind of category for the user activity.  Required parameter.
//                                     'false',               // This value of the custom variable.  Required parameter.
//                                     2                    // Sets the scope to session-level.  Optional parameter.
//                                   ]);
                        }
                        else if (data.isFriend == "false") {

                            _gaq.push(['_setCustomVar',
                                      1,
                                     'logintype',
                                     'user',
                                     1
                                   ]);

//                            _gaq.push(['_setCustomVar',
//                                      2,
//                                     'firstlogin',
//                                     'false',
//                                     2
//                                   ]);
                        }
                        else if (data.isFriend == "admin") {

                            _gaq.push(['_setCustomVar',
                                      1,
                                     'logintype',
                                     'admin',
                                     1
                                   ]);

//                            _gaq.push(['_setCustomVar',
//                                      2,
//                                     'firstlogin',
//                                     'false',
//                                     2
//                                   ]);
                        }
                    }
                    catch (err) {

                    }
                    setTimeout(function () {
                        window.location.reload();
                    }, 1700);

                }
                else if (data.requireNemid) {
                    try {
                        _gaq.push(['_setCustomVar',
                                      1,
                                     'logintype',
                                     'friend',
                                     1
                                   ]);
//                        _gaq.push(['_setCustomVar',
//                                      2,
//                                     'firstlogin',
//                                     'false',
//                                     2
//                                   ]);
                    }
                    catch (err) {

                    }
                    window.location = ("/account/nemid?returnurl=" + window.location.href);

                }
                else {
                    if (data.isDisabled) {
                        $("#loadingImg").hide();
                        $("#loginSubmit").show();
                        $("#loginError").text('Brugeren er deaktiveret.');
                        $("#loginError").css("display", "block");
                        $("#loginPopup").effect("shake", { times: 3 }, 50);
                        $("#LoginEmail").attr('readOnly', "");
                        $("#LoginPassword").attr('readOnly', "");

                        setTimeout(
                    function () {
                        submitStatus = 0;
                    }, 1000);
                    }
                    else {
                        $("#loadingImg").hide();
                        $("#loginSubmit").show();
                        $("#loginError").text('Forkert bruger eller kodeord.');
                        $("#loginError").css("display", "block");
                        $("#loginPopup").effect("shake", { times: 3 }, 50);
                        $("#LoginEmail").attr('readOnly', "");
                        $("#LoginPassword").attr('readOnly', "");

                        setTimeout(
                    function () {
                        submitStatus = 0;
                    }, 1000);
                    }

                }
            }, "json").error(function () {
                $("#loadingImg").hide();
                $("#loginSubmit").show();
                $("#LoginEmail").attr('readOnly', "");
                $("#LoginPassword").attr('readOnly', "");
                submitStatus = 0;
                $("#loginError").text('Der er sket en fejl. Prøv venligst igen.');
                $("#loginError").css("display", "block");
            });

            return false;
        }
        else if (forgot == 1) {

            $.post("/Account/CheckEmailExists", $('#forgotEmail').serialize(), function (data) {
                $('#forgotSubmit').hide();
                if (data.isValid != false) {
                    $('#forgotStatus').text("Mailen findes ikke i vores database.");
                    $('#forgotStatus').show();
                    $('#forgotSubmit').show();

                    return false;
                }
                else {
                    resetPassword();
                    return false;
                }
            });
            return false;

        }
    });


});
