﻿/// <reference name="MicrosoftAjax.debug.js" />
/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />

/// <reference path="jquery-1.2.6-vsdoc.js"/>

// See: http://asp.net/ajax/documentation/live/ClientReference/Sys.Services/AuthenticationServiceClass/default.aspx

SCOTT.CSC.AutoLogin = function(clientId, successMessageText, failureMessageText, registerMessageText) 
{
    this.ClientID = clientId;
    this.Selector = '#' + clientId;
    this.SuccessMessageText = successMessageText;
    this.FailureMessageText = failureMessageText;
    this.RegisterMessageText = registerMessageText;
    this.RedirectUrl = '';
    this.OnSuccess = undefined;
    this.OnFailure = undefined;
            
    this.Initialize();
};

SCOTT.CSC.AutoLogin.prototype =
{
    Name: 'SCOTT.CSC.AutoLogin',
    __typeName: 'SCOTT.CSC.AutoLogin',
    __class: true,
    AttachEvents: function() {
        var __app = this;
        var context = $(this.Selector);

        var execClose = function() {
            __app.HideLogin();
        };

        $('.CloseButton', context).click(execClose);

        //this.AuthenticationService.set_defaultLoginCompletedCallback(onLoginCompleted);
        this.AuthenticationService.set_defaultUserContext(this);

        $('.LoginButton', context).click(function(e) {
            e.preventDefault();
            __app.Login();
        });

        // listen for the enter key and do login when entered
        $('.LoginForm', context).keydown(function(e) {
            if (e.keyCode === 13) {
                __app.Login();
                e.preventDefault();
                return false;
            }
            else {
                return true;
            }
        });
    },
    CompleteSuccessfulLogin: function() {
        // fire the callback and clear it
        if (this.OnSuccess) {
            this.OnSuccess();
            this.OnSuccess = undefined;
        }

        if (this.RedirectUrl && this.RedirectUrl !== '') {
            window.location.href = this.RedirectUrl;
        }
    },
    ShowLogin: function(onSuccess, onFailure) {
        var __app = this;

        if (onSuccess) {
            this.OnSuccess = onSuccess;
        }
        if (onFailure) {
            this.OnFailure = onFailure;
        }

        this.ShowMask();
        $(this.Selector + ' input').val('');

        this.CenterElement(this.Selector);

        $(this.Selector).
            css("z-index", "100").
            fadeIn(250, function() {
                $(__app.Selector + ' input.UserName').focus();
            });
    },
    ShowLoginWithRedirect: function(url) {
        var __app = this;
        __app.RedirectUrl = url;

        this.ShowLogin();
    },
    HideLogin: function() {
        var __app = this;

        $(this.Selector).fadeOut(250, function() {
            __app.HideMask();
        });
    },
    Login: function() {
        var __app = this;

        var context = $(this.Selector);
        var username = $('.UserName', context).val();
        var password = $('.Password', context).val();
        var isPersistent = $('span.Persistent input[type="checkbox"]', context).get(0).checked;

        var onLoginCompleted = function(validCredentials, userContext, methodName) {
            if (validCredentials) {
                SCOTT.CSC.GetApplication().ChangeLoginStatus(true);
                $('.LoginForm', context).fadeOut(250, function() {
                    $('.Message span', context).html(__app.SuccessMessageText);
                    $('.Message', context).fadeIn(250, function() {
                        setTimeout(function() {
                            $('.Message', context).fadeOut(250, function() {
                                __app.HideLogin();
                                __app.CompleteSuccessfulLogin();
                            });
                        }, 2000);
                    });
                });
            }
            else {
                $('.LoginForm', context).fadeOut(250, function() {
                    $('.Message span', context).html(__app.FailureMessageText);
                    $('.Message', context).fadeIn(250, function() {
                        setTimeout(function() {
                            if (__app.OnFailure) {
                                __app.OnFailure();
                                __app.OnFailure = undefined;
                            }
                            $('.Message', context).fadeOut(250, function() {
                                $('.LoginForm', context).fadeIn();
                            });
                        }, 3000);
                    });
                });
            }
        };

        var handleUserStatus = function(status) {
            if (status !== "CSC-User") {
                $('.LoginForm', context).fadeOut(250, function() {
                    $('.Message span', context).html(__app.RegisterMessageText);
                    $('.Message', context).fadeIn(250, function() {
                        setTimeout(function() {
                            $('.Message', context).fadeOut(250, function() {
                                $('.LoginForm', context).fadeIn();
                            });
                        }, 4500);
                    });
                });
            }
            else {
                __app.AuthenticationService.login(
                    username, password, isPersistent, __app.RedirectUrl,
                    null, onLoginCompleted, null, null);
            }
        };

        this.UsersService.GetUserStatus(username, handleUserStatus);
    }
};

SCOTT.CSC.Extend(SCOTT.CSC.AutoLogin, SCOTT.CSC.UserControl);
