﻿/// <reference name="MicrosoftAjax.debug.js" />
/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />

/// <reference path="jquery.intellisense.js"/>

Kleenex.Application = function() 
{
    this.Initialize();
};
Kleenex.Application.prototype =
{
    Name : 'Kleenex.Application',
    __typeName : 'Kleenex.Application',
    __class : true,
    userControls : new Array(),
    _isUserAuthenticated : undefined,
    RootUrl : '/',
    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;
    },
    AttachEvents : function()
    {
        var __app = this;
        setTimeout(function()
        {
            for (var i=0;i<__app.userControls.length;i++)
            {
                var userControl = __app.userControls[i];
                //alert(userControl.Name + " : " + userControl.ClientID);
            }
        }, 3000);
    },
    RegisterUserControl : function(userControl)
    {
        this.userControls[this.userControls.length] = userControl;
    },
    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);
            }
        }
    },
    ShowAutoLogin : function(returnUrl)
    {
        Kleenex.ShowModal(Kleenex.Variables.RootUrl + 'Login.aspx?ReturnUrl=' + escape(returnUrl), true);
    }
};

// inherit from the core
Kleenex.Extend(Kleenex.Application, Kleenex.Core);

Kleenex.GetApplication = function()
{
    /// <summary>Static getter for the single Kleenex.Application instance</summary>
    /// <returns type="Kleenex.Application">Kleenex.Application</returns>
    if (window["KleenexApplicationInstance"] == undefined)
    {
        window["KleenexApplicationInstance"] = new Kleenex.Application();
    }
    return KleenexApplicationInstance;
};

// <a href="javascript:Kleenex.RequireAuthentication('/Home.aspx')">Do something</a>
Kleenex.RequireAuthentication = function(url)
{
    var app = Kleenex.GetApplication();
    if (!app.IsUserAuthenticated())
    {
        app.ShowAutoLogin(url);
    }
    else
    {
        window.location.href = url;
    }
};

Kleenex.ShowModal = function(url, requiresAuthentication, mode)
{
    if ($.browser.msie && $.browser.version < 7)
    {
        // work-around for IE6
        $('.CardPagerContainer select').hide();
    }
    
    var app = Kleenex.GetApplication();
    if (requiresAuthentication && !app.IsUserAuthenticated())
    {
        var retUrl = Kleenex.Variables.RootUrl + 'Login.aspx?ReturnUrl=' + url;
        if (mode)
        {
            retUrl = retUrl + '&mode=' + mode;
        }
        app.ShowOverlayFrame(retUrl);
    }
    else
    {
        app.ShowOverlayFrame(url);
    }
};

Kleenex.HideModal = function()
{
    if ($.browser.msie && $.browser.version < 7)
    {
        // work-around for IE6 (does not seem to show the hidden fields)
        $('.CardPagerContainer select').show();
    }

    // gaurantee the real window object is returned (via jeresig)
    var w = (function(){return this;})();

    if (w.frames.length !== 0)
    { // if this is the parent frame
        Kleenex.GetApplication().HideOverlayFrame();
    }
    else if (Kleenex.IsParentAccessible())
    {
        w.parent.Kleenex.GetApplication().HideOverlayFrame();
    }
    else
    {
        try
        {
            if (w.location.protocol == 'https:')
            { // move to non-SSL if the parent is not accessible from SSL
                w.location.href = Kleenex.Variables.CloseModalUrl;
            }
            else if (w.location.protocol == 'http:')
            { // move to SSL if the parent is not accessible from non-SSL
                w.location.href = Kleenex.Variables.SecureCloseModalUrl;
            }
        }
        catch (ex)
        {
            console.log(ex);
        }
    }

};

Kleenex.ChangeLoginStatus = function(isAuthenticated)
{
    if ($('body.iframe').length === 0)
    {
        Kleenex.GetApplication().ChangeLoginStatus(isAuthenticated);
    }
    else
    {
        if (Kleenex.IsParentAccessible())
        {
            window.parent.Kleenex.GetApplication().ChangeLoginStatus(isAuthenticated);
        }
    }
};


Kleenex.IsParentAccessible = function()
{
    try
    {
        // gaurantee the real window object is returned (via jeresig)
        var w = (function(){return this;})();
        var location = w.parent.location.href;
        return true;
    }
    catch (ex)
    {
        return false;
    }
};
