﻿TwentyQuestions.Auth = {};

$(document).ready(function () {

    $(".login").fancybox({
        'hideOnContentClick': false,
        'titleShow': false,
        'scrolling': 'no',
        'onComplete': function () {
            $('#UserName').watermark("Username/Email Address");
            $('#Password').watermark("Password");
            $('#UserName').tipTip({ activation: "focus", defaultPosition: "left" });
            $('#Password').tipTip({ activation: "focus", defaultPosition: "left" });

            TwentyQuestions.Auth.AddSignupPopupBehavior();

            TwentyQuestions.Auth.AddRetrievePasswordPopupBehavior();
        }
    });

    TwentyQuestions.Auth.AddSignupPopupBehavior();
});

TwentyQuestions.Auth.AddSignupPopupBehavior = function () {
    $(".signup").fancybox({
        'hideOnContentClick': false,
        'titleShow': false,
        'scrolling': 'no',
        'onComplete': function () {
            $('#UserName').watermark("Username");
            $('#Email').watermark("Email Address");
            $('#Password').watermark("Password");
            $('#ConfirmPassword').watermark("Repeat Password");
            $('#UserName').tipTip({ activation: "focus", defaultPosition: "left" });
            $('#Email').tipTip({ activation: "focus", defaultPosition: "left" });
            $('#Password').tipTip({ activation: "focus", defaultPosition: "right" });
            $('#ConfirmPassword').tipTip({ activation: "focus", defaultPosition: "right" });
        }
    });
};

TwentyQuestions.Auth.AddRetrievePasswordPopupBehavior = function () {
    $("#retrievepassword").fancybox({
        'hideOnContentClick': false,
        'titleShow': false,
        'scrolling': 'no',
        'onComplete': function () {
            $('#Email').watermark("Email Address");
            $('#Email').tipTip({ activation: "focus", defaultPosition: "left" });
        }
    });
};


TwentyQuestions.Auth.Login = function () {    

    var $popup = $("#fancybox-outer");
    var form = $popup.find("form");

    var data = form.serialize();
    var url = form.attr('action');

    $.ajax({
        type: "POST",
        url: url,
        data: data,
        dataType: "json",
        success: function (response) {
            if (response.Success) {
                location.href = response.ReturnUrl;
            } else {
                TwentyQuestions.Common.ShowMessageBar(response.Message);
            }
        },
        error: function (xhr, status, error) {
            TwentyQuestions.Common.ShowMessageBar(error);
        }
    });

    return false;

};

TwentyQuestions.Auth.Register = function () {
    var $popup = $("#fancybox-outer");
    var form = $popup.find("form");

    var data = form.serialize();
    var url = form.attr('action');

    $.ajax({
        type: "POST",
        url: url,
        data: data,
        dataType: "json",
        success: function (response) {
            if (response.Success) {
                location.href = response.ReturnUrl;
            } else {
                TwentyQuestions.Common.ShowMessageBar(response.Message, 7000);
            }
        },
        error: function (xhr, status, error) {
            TwentyQuestions.Common.ShowMessageBar(error);
        }
    });

    return false;
};

TwentyQuestions.Auth.ChangePassword = function (sender) {
    var form = $(sender).parent().parent();
    var data = form.serialize();

    var url = form.attr('action');
    $.ajax({
        type: "POST",
        url: url,
        data: data,
        dataType: "json",
        success: function (response) {            
            TwentyQuestions.Common.ShowMessageBar(response.Message);
        },
        error: function (xhr, status, error) {
            TwentyQuestions.Common.ShowMessageBar(error);
        }
    });
    return false;
};

TwentyQuestions.Auth.RetrievePassword = function () {
    var $popup = $("#fancybox-outer");
    var form = $popup.find("form");
    var data = form.serialize();
    var url = form.attr('action');
    $.ajax({
        type: "POST",
        url: url,
        data: data,
        dataType: "json",
        success: function (response) {
            if (response.Success) {
                $.fancybox.close();
            }

            TwentyQuestions.Common.ShowMessageBar(response.Message);
        },
        error: function (xhr, status, error) {
            TwentyQuestions.Common.ShowMessageBar(error);
        }
    });

    return false;   
};


