﻿///<reference path="CliPropConfig6.js" />
///<reference path="CliBasePages6.js" />
///<reference path="CliKontikiHost6.js" />

//--------------------------------------------------------------------------------
function CliApplication( propositionInstance, iBasePages )
{
    this._propositionInstance = propositionInstance;
    this._iBasePages = iBasePages;
    
    this._propositionName;
    this._authToken;
    this._authSignature;
    
    this._kontikiHost;
}

//--------------------------------------------------------------------------------
CliApplication.prototype =
{
    //--------------------------------------------------------------------------------
    Init : function( propositionName, authToken, authSignature, configXml ) // : bool
    {
        ///<summary>Initialise the application</summary>
        ///<param name="propositionName"></param>
        ///<param name="authToken"></param>
        ///<param name="authSignature"></param>
        ///<param name="configXml"></param>
        ///<return>true if the application is installed</return>
        
        // check if host supports our object
        if( this._propositionInstance == null )
            return false;
        
        // do not initialise if the correct proposition is not installed
        if( !CliIsInstalled( propositionName ) )
            return false;
        
        this._propositionName = propositionName;
        this._authToken       = authToken;
        this._authSignature   = authSignature;
        
        this._kontikiHost = new CliKontikiHost( authToken, authSignature );
        
        this._InitialiseProposition( configXml );
        return true;
    },
    
    //--------------------------------------------------------------------------------
    _InitialiseProposition : function( configXml )
    {
        if( configXml == null )
            configXml = "<settings />";
       
        try
        {
            this._propositionInstance.Initialise( this._propositionName, configXml, this._authToken, this._authSignature );
        }
        catch( e )
        {
            if( e.number == "-2146827850" )
            {
                // this error will occur if the user does not have the COM object installed, so we just ignore (assume running in browser)
                return;
            }
            CliDebug( "CliApplication init failed: " + e.message );
        }
    },
    
    //--------------------------------------------------------------------------------
    Start : function()
    {
		var hasSettings = this._CheckIfSettingsPresent();  
        
        if( !hasSettings )
            this._ForceInitialSettings();
            
        if( this.CheckIfOnline() )
        {
            this._iBasePages.OpenOnlineHomePage();
        }
        else
        {
            if( hasSettings )
            {
                this._iBasePages.OpenOfflineHomePage();
            }
            else
            {
                this._iBasePages.OpenOfflineNoSettingsPage();
            }
        }
    },

    //--------------------------------------------------------------------------------
    GetKontikiHost : function()
    {
        return this._kontikiHost;
    },
    
    //--------------------------------------------------------------------------------
    IsInstallationComplete : function()
    {
        return this._propositionInstance.OemInstallation.OemInstallationComplete;
    },

    //--------------------------------------------------------------------------------
    CheckIfOnline : function()
    {
        ///<summary>Check if the application appears to be online<//summary>
        
        return this._propositionInstance.Offline.CheckIsOnline( '' );
    },
    
    //--------------------------------------------------------------------------------
    _CheckIfSettingsPresent : function()
    {
        ///<summary>Checks if there are any settings present within the offline application
        ///</summary>
        
        try
        {
            //attempt to get the propositions settings
            if( this._propositionInstance.Settings == null )
            {
               //settings are not present return false
                return false;
            }
        }
        catch( excep )
        {
            //Error occured getting the settings therefore they have not been set so return false
            return false;
        }
        
        var chanIterator;
        try
        {
            //attempt to get the channels from the client controller
            chanIterator = this._propositionInstance.Settings.Channels;
        }
        catch( excep )
        {
            //failed to get channels therefore there is an issue with the settings return false
            return false;
        }
            
        if( !chanIterator.More )
        {
           //there are no channels set. Therefore settings are not in place
           return false;
        }
        
        //Still here so channels are present and set, therefore as per discussions settings are present 
        return true;
    },
    
    //--------------------------------------------------------------------------------
    SwitchDomainIfNecessary : function( url )
    {
        ///<summary>if the client is installed for this app, and were not in the client
        ///we should open in the client, unless we are configured not to.</summary>
        ///<param name="url"></param>
        
        //if the browser domain online launch page is calling this method, dont
        //switch as that page should be launching another page in the
        //client.  Thus we dont want to switch from this page
        if( url.indexOf( this._iBasePages.OnlineBrowserDomainHybridLaunchPage ) >= 0 )
            return;
            
        // if we are not in either the client or browser domain, the exit from this function.  this will happen if
        // we are in the preview domian
        var inClientDomain  = this._iBasePages.IsUrlInClientDomain( url );
        var inBrowserDomain = this._iBasePages.IsUrlInBrowserDomain( url );
        if( !inClientDomain && !inBrowserDomain )
            return;

        var clientDomain    = CliGetClientDomain();
        var browserDomain   = CliGetBrowserDomain();

        if( this._DetectIfLoadedInsideKontiki() )
        {
            if( !inClientDomain )
            {
                // in the client but not the client domain, so switch to the client domain
                document.location.href = url.replace( browserDomain, clientDomain );
            }    
        }
        else
        {
            var isInstalled = CliIsInstalled( CliGetProfileName() );

            if( CliDisableForceIntoClient() )
                return;
            
            if( inBrowserDomain )
            {
                if( isInstalled )
                {
                    //if it's a forced browser page such as error FAQs then leave it in the browser
                    if(document.location.href.indexOf("forceBrowser=true") >0 )
                        return;
                        
                    // in the browser, in the browser domain, but we have the client installed, so launch in the client
                    var urlToLaunch = this._iBasePages.OnlineBrowserDomainHybridLaunchPage + encodeURIComponent( document.location.href );
                    document.location.href = urlToLaunch;
                }
            }
            else
            {
                // in the browser, but in the client domain, so switch to the browser domain
                document.location.href = url.replace( clientDomain, browserDomain );
            }
        }
    },
    
    //--------------------------------------------------------------------------------
    GetItem : function( mediaContentId )
    {
        ///<summary>get a library item</summary>
        
        return this._propositionInstance.VideoLibraryActions.GetItem( mediaContentId );
    },
        
    //--------------------------------------------------------------------------------
    _DetectIfLoadedInsideKontiki : function()
    {
        ///<summary>Tests if the window is loaded inside kontiki</summary>
        
        var foundKontiki = false;
        var windowToCheck = window;
        
        while( 1 == 1 )
        {
            try
            {
                var check = windowToCheck.external.param("authtoken");
                // if code reaches here we are in kontiki
                foundKontiki = true;
            } 
            catch( e )
            {}
            
            if (windowToCheck == window.top)
                break;
            else
                windowToCheck = window.parent;
        }
        return foundKontiki;
    }, 
    
    //--------------------------------------------------------------------------------
    _ForceInitialSettings : function()
    {
        CliDebug("Forcing initial settings");
        var defaultSettings = CliGetDefaultSettings();
        this._InitialiseProposition( defaultSettings );
    }
}//
