﻿/// <reference name="MicrosoftAjax.debug.js" />
/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />

/// <reference path="jquery-1.2.6-vsdoc.js"/>

SCOTT.CSC.Application = function() 
{
};

SCOTT.CSC.Application.prototype =
{
    Name : 'SCOTT.CSC.Application',
    __typeName : 'SCOTT.CSC.Core',
    __class : true,
    userControls : new Array(),
    _isUserAuthenticated : undefined,
    RegisterUserControl : function(userControl)
    {
        this.userControls[this.userControls.length] = userControl;
    },
    IsUserAuthenticated : function()
    {
        if (this._isUserAuthenticated === undefined)
        {
            this._isUserAuthenticated = Sys.Services.AuthenticationService.get_isLoggedIn();
        }
        return this._isUserAuthenticated;
    },
    ChangeLoginStatus : function(isAuthenticated)
    {
        this._isUserAuthenticated = isAuthenticated;
        for (var i=0;i<this.userControls.length;i++)
        {
            var userControl = this.userControls[i];
            if (userControl.ChangeLoginStatus !== undefined)
            {
                userControl.ChangeLoginStatus(isAuthenticated);
            }
        }
    },
    GetAutoLoginID : function()
    {
        var matches = $('.AutoLogin:first');
        if (matches.length > 0)
        {
            return matches.get(0).id;
        }
        else
        {
            return null;
        }
    },
    ShowAutoLogin : function(onSuccess, onFailure)
    {
        var id = this.GetAutoLoginID();
        window[id].ShowLogin(onSuccess, onFailure);
    },
    ShowAutoLoginWithRedirect : function(url)
    {
        var id = this.GetAutoLoginID();
        window[id].ShowLoginWithRedirect(url);
    },
    HideAutoLogin : function()
    {
        var id = this.GetAutoLoginID();
        window[id].HideLogin();
    }
};

SCOTT.CSC.Extend(SCOTT.CSC.Application, SCOTT.CSC.Core);

SCOTT.CSC.UserControl = function() {};
SCOTT.CSC.UserControl.prototype =
{
    Name : 'SCOTT.CSC.UserControl',
    __typeName : 'SCOTT.CSC.UserControl',
    __class : true,
    ClientID : undefined,
    Initialize : function()
    {
        this.InitializeServices();
        var csc = SCOTT.CSC.GetApplication();
        csc.RegisterUserControl(this);
        this.AttachEvents();
        this.Display();
    },
    ChangeLoginStatus : function(isAuthenticated)
    {
    }
};

// inherit from the core
SCOTT.CSC.Extend(SCOTT.CSC.UserControl, SCOTT.CSC.Core);

SCOTT.CSC.GetApplication = function()
{
    /// <summary>Static getter for the single SCOTT.CSC.Application instance</summary>
    /// <returns type="SCOTT.CSC.Application">SCOTT.CSC.Application</returns>
    if (window["SCOTTCSCApplicationInstance"] == undefined)
    {
        window["SCOTTCSCApplicationInstance"] = new SCOTT.CSC.Application();
    }
    return SCOTTCSCApplicationInstance;
};

SCOTT.CSC.RequireAuthentication = function(url)
{
    var app = SCOTT.CSC.GetApplication();
    if (!app.IsUserAuthenticated())
    {
        app.ShowAutoLogin();
    }
    else
    {
        window.location.href = url;
    }
}
// <a href="javascript:SCOTT.CSC.RequireAuthentication('/Home.aspx')">Do something</a>

SCOTT.CSC.RequireAuthenticationWithRedirect = function(url)
{
    var app = SCOTT.CSC.GetApplication();
    if (!app.IsUserAuthenticated())
    {
        app.ShowAutoLoginWithRedirect(url);
    }
    else
    {
        window.location.href = url;
    }
}
