﻿
//--------------------------------------------------------------------------------
function CliKontikiHost( authToken, authSignature )
{
    try
    {
        this._kdxApi = new ActiveXObject( "KDX.SecureApi" );
        this._kdxApi.authorize( authToken, authSignature );
    }
    catch( e )
    {
        this._kdxApi = null;
    }

    this._pageStyle = "class=browser,width=1024,height=768,position=1,resize=1,scrollbars=1,visible=1";
    this._rootFrameName = "root";
    this._onlineLogoutPage = CliBuilOnlineUrl( "/page/logout.do", false, true)
}

//--------------------------------------------------------------------------------
CliKontikiHost.prototype =
{
    //--------------------------------------------------------------------------------
    LaunchPage : function( url )
    {
        ///<summary>_launchPageReuseTemplateIfAvailable</summary>
        ///<param name="url"></param>
    
        var pageStyle = this._pageStyle;
        var frameName = this._rootFrameName;
        
        this.LaunchPageEx( url, frameName, pageStyle );
    },
    
    //--------------------------------------------------------------------------------
    LaunchPageEx : function( url, optionalFrameName, optionalPageStyle )
    {
        ///<summary>_launchPageReuseTemplateIfAvailable</summary>
        ///<param name="url"></param>
        ///<param name="frameName">logical container name to display page in</param>
        ///<param name="optionalPageStyle">style to set on frame</param>
    
        if( optionalPageStyle == null )
            optionalPageStyle = this._pageStyle;
        
        if( optionalFrameName == null )
            optionalFrameName = this._rootFrameName;

        var kdxUrn = "";
        
        //TODO: previous code had a try catch that did a _launchPageInNewTemplate on error
        
        var kdxFrame = null;
        try
        {
            kdxFrame = this._kdxApi.getFrame( kdxUrn, optionalFrameName );
        }
        catch(e){ }
        
        if( kdxFrame == null )
        {
            var newKdxFrame = this._kdxApi.launchTemplate( url, optionalPageStyle, kdxUrn, optionalFrameName );
        }
        else
        {
            kdxFrame.navigate( url );
        }
    },
    
    //--------------------------------------------------------------------------------
    GetKdxApi : function()
    {
        ///<summary>Get access to the kontiki instance</summary>
        return this._kdxApi;
    },
    
    //  accessor for the kdxapi  
    closeKHost : function() 
    {
        this._kdxApi.exitProfile();
    },
    
    onClose : function(kdxFrame) {
        // ensure this only happens for the root template (not the play template)
        var rootFrame;
        try {
            rootFrame = this._kdxApi.getFrame( '', this._rootFrameName );
            if (kdxFrame != rootFrame) return;
        }
        catch (e) {
            return;
        }
        
        var xmlHttp;
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (E) {
                xmlHttp = false;
             }
        }
        
        if (xmlHttp) {
            var pageLoadCallback = function() {
                if (xmlHttp.readyState==4 && xmlHttp.status==200) {
                    xmlHttp.abort();
                    kdxFrame.closeWindow();
                }
            }
        }     
        
        xmlHttp.onreadystatechange = pageLoadCallback;
        xmlHttp.open("HEAD", this._onlineLogoutPage, true)
        xmlHttp.send(null)
    }
}//

/*

//------------------------------------------------------------------------------
//  Encapsulates all kontiki UI related work
//------------------------------------------------------------------------------
function CliKontikiHosted(clientController, propositionInstance, profileName, authToken, authSignature ) {

   
    var PLAYER_FRAME_NAME = "player";
    var PLAYER_PAGE_STYLE = 'class=browser,width=610,height=585,position=1,resize=1,scrollbars=1,visible=1';
    
    // The following pages are from the cache but we do not use the cache: prefix as they are all relative to the RootFrameset page
    var CACHED_PCNAME_PAGE = 'cache:PCName.htm';
    var CACHED_PLAYER_PAGE = 'cache:MediaPlayer.htm';
    var CACHED_NO_LICENSE_PAGE = 'cache:LicenseAcquisition.htm';
    var CACHED_START_PAGE = 'cache:StartPage.htm';
    var RELATIVE_CACHED_START_PAGE = "StartPage.htm";
    
    var ONLINE_HYBRID_STARTUP_PAGE = CliBuilOnlineUrl( "/page/hybridLaunch.do?requestedUrl=" );
    var ONLINE_LOGOUT_PAGE = CliBuilOnlineUrl( "/content/Home/Application-Navigation/Logout/content/logout.do" );

    
    var _clientController;
    var _propositionInstance;
    var _profileName;
    var _kdxApi;
    
    //  accessor for the kdxapi  
    CliKontikiHosted.prototype.getKdxApi = function() { return _kdxApi;}
    
    

    
    
 
    
    
	
	
	
	// progress the flow
	CliKontikiHosted.prototype.agreeEulaFinalisationFlow = function()
	{
        _launchPageReuseTemplateIfAvailable(CACHED_PCNAME_PAGE, PAGE_STYLE, '', ROOT_FRAME_NAME);
	}
		
	// complete the install finalisation flow by displaying the Start page
	CliKontikiHosted.prototype.completeFinalisationFlow = function( pcName )
	{
	    try
	    {
	        _propositionInstance.OemInstallation.CompleteInstallation( pcName );
            document.location = RELATIVE_CACHED_START_PAGE;
        }
        catch( e )
        {
            CliDisplayError( CliErrorAction_SetPcName, e.number );
        }
	}
   //////////////////////////////////////////////////////////////////////////


    
        
    //  launches a page inside kontiki if the client has the app installed  
    CliKontikiHosted.prototype.launchInKontikiIfAvailable = function( destinationUrl ) {
        if (!_clientController.isInstalled())
            return false;
            
        if (!_ensureIsFinalised())
            return false;

        _launchPageReuseTemplateIfAvailable(destinationUrl, PAGE_STYLE, '', ROOT_FRAME_NAME);
        return true;
          
    }
    
    //  performs a check for finalisation then launches a url in the kontiki root template  
    function _launchPageInRootTemplateAndCheckFinalisation(url) {
        if (!_ensureIsFinalised()) 
            return false;
        _launchPageReuseTemplateIfAvailable(url, PAGE_STYLE, '', ROOT_FRAME_NAME);
    }

    //  launches the media player for a specific urn inside a kontiki template 
    CliKontikiHosted.prototype.launchOfflineMediaPlayer = function (kdxUrn) {
        CliDebug('launchOfflineMediaPlayer kdxUrn: ' + kdxUrn );
        if (!_ensureIsFinalised()) {
            return false;
        }
        _launchPageInNewTemplate(CACHED_PLAYER_PAGE, PLAYER_PAGE_STYLE, kdxUrn, PLAYER_FRAME_NAME);
    }

    //  launches the 'no license' message in a kontiki template 
    CliKontikiHosted.prototype.launchNoLicenseMessage = function () {
        _launchPageReuseTemplateIfAvailable(CACHED_NO_LICENSE_PAGE, PLAYER_PAGE_STYLE, '', PLAYER_FRAME_NAME);
    }

    //  launches the media player for a specific urn inside a kontiki template 
    CliKontikiHosted.prototype.launchOnlineMediaPlayer = function (kdxUrn, onlineUrl) {
        CliDebug('launchOnlineMediaPlayer kdxUrn: ' + kdxUrn + '  onlineUrl: ' + onlineUrl);
        if (!_ensureIsFinalised()) 
            return false;
        
        if (kdxUrn==null) kdxUrn='';
        var kdxFrame = _kdxApi.launchTemplate( onlineUrl, PLAYER_PAGE_STYLE, kdxUrn, PLAYER_FRAME_NAME );
        //_launchPageInNewTemplate(onlineUrl, PLAYER_PAGE_STYLE, kdxUrn, PLAYER_FRAME_NAME);
    }


    //  launches the hybrid startup page.  if a requesturl is not passed into the function, the online home page is loaded
    //    after the hybrid startup page has been rendered.
    
    CliKontikiHosted.prototype.launchOnlineHybridStartupPage = function (requestedUrl) {
        
        if (requestedUrl==null)
            requestedUrl = ONLINE_HOME_PAGE;
                  
        var pageToLaunch = ONLINE_HYBRID_STARTUP_PAGE + encodeURI(requestedUrl);
            
        _launchPageInRootTemplateAndCheckFinalisation(pageToLaunch);
    }
    
    CliKontikiHosted.prototype.closeApp = function() {
        try {
            window.external.closeWindow();
        }
        catch (e){}
    }
    
    CliKontikiHosted.prototype.navigate = function( url, frameName )
    {
        if( frameName == null )
            frameName = ROOT_FRAME_NAME;

        _launchPageReuseTemplateIfAvailable( url, PAGE_STYLE, '', frameName );
    }
    
  
    this.init(clientController, propositionInstance, profileName, authToken, authSignature);
    return true;
}
*/