﻿/// <reference name="MicrosoftAjax.debug.js" />
/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />

/// <reference path="jquery.intellisense.js"/>

Depend.Application = function() {
    this.Initialize();
};
Depend.Application.prototype =
{
    Name: 'Depend.Application',
    __typeName: 'Depend.Application',
    __class: true,
    userControls: new Array(),
    _isUserAuthenticated: undefined,
    RootUrl: '/',
    /*
    IsUserAuthenticated: function() {
        if (this._isUserAuthenticated === undefined) {
            this._isUserAuthenticated = Sys.Services.AuthenticationService.get_isLoggedIn();
        }
        return this._isUserAuthenticated;
    },
    */
    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);
            }
        }
    }
};

// inherit from the core
Depend.Extend(Depend.Application, Depend.Core);

Depend.GetApplication = function() {
    /// <summary>Static getter for the single Depend.Application instance</summary>
    /// <returns type="Depend.Application">Depend.Application</returns>
    if (window["DependApplicationInstance"] == undefined) {
        window["DependApplicationInstance"] = new Depend.Application();
    }
    return DependApplicationInstance;
};

Depend.ChangeLoginStatus = function(isAuthenticated) {
    if ($('body.iframe').length === 0) {
        Depend.GetApplication().ChangeLoginStatus(isAuthenticated);
    }
    else {
        if (Depend.IsParentAccessible()) {
            window.parent.Depend.GetApplication().ChangeLoginStatus(isAuthenticated);
        }
    }
};

Depend.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;
    }
};
