<!-- 
/************************************************************************

FILE: scripts.js
DESCRIPTION: Functions and variables used by KURLS to install and/or
initiate delivery into the KDM 4.02.

Copyright (c) 2004-2005 Kontiki Inc.

*************************************************************************/

//////////////////////////////////////////////////////////////////////////
//
// Include file.
//
//////////////////////////////////////////////////////////////////////////
var rootDir = "http://cdn.digitalcity.com/_media/hiqvideo/";

document.write('<script src="' + rootDir + 'kdx_init.js"><\/script>');
document.write('<script src="' + rootDir + 'config_xml.js"><\/script>');
document.write('<script src="' + rootDir + 'kdx_wrapper.js"><\/script>');

//////////////////////////////////////////////////////////////////////////
//
// Configurable variable: An absolute URL to the index.html file of the
// implementation.
//
// NOTE: Since this variable may be called from files on various servers,
// this *must* be an absolute URL.
//
//////////////////////////////////////////////////////////////////////////

var gIndexPageUrl = rootDir + 'index.html'; // index page url
var gEulaPopupUrl = rootDir + 'aol_eula_popup.html';	// EULA page url
var gMinSysReqUrl = rootDir + 'aol_requirements.html'; // minimum system requirements page
var gFallbackUrl =  rootDir + 'aol_fallback.html'; // fallback page

var alertsMode = false;
var minSysReqIsMeet = -1;

//////////////////////////////////////////////////////////////////////////
//
// Configurable variable: Array of known DMS domains. Associate the KID
// namespace to the domain of the DMS's End User Webapp.
//
//////////////////////////////////////////////////////////////////////////
var gDMSMap = new Array();
gDMSMap.kontiki = "web-stage.kontiki.com";
gDMSMap.dl = "web-stage.kontiki.com";
gDMSMap.aol_stage = "web-stage.kontiki.com";
gDMSMap.stage = "web-stage.kontiki.com"
gDMSMap.winqa = "web-stage.kontiki.com";
gDMSMap.ey = "web-stage.kontiki.com";

//////////////////////////////////////////////////////////////////////////
//
// FUNCTION: launchKDX(moid)
//
// DESCRIPTION: Pre-4.02 launcher for backward compatibility. Note: The
// "s" param is not used by the 4.02 client, but it is necessary for
// non-interception by KDM 3.x that may be already installed.
//
//////////////////////////////////////////////////////////////////////////
function launchKDX(moid)
{     getIt("moid="+moid+"&model=" + gDefaultModel + "&domain=" + gDefaultDMS + "&s=kdx&alerts=false");
}

//////////////////////////////////////////////////////////////////////////
//
// FUNCTION: launchPlugin(moid, profile)
//
// DESCRIPTION: Pre-4.02 launcher for backward compatibility. Note: The
// "s" param is not used by the 4.02 client, but it is necessary for
// non-interception by KDM 3.x that may be already installed.
//
//////////////////////////////////////////////////////////////////////////
function launchPlugin(moid, profile)
{
getIt("moid="+moid+"&model=" + gDefaultModel + "&domain=" + gDefaultDMS + "&s=kdx&alerts=false");
}

//////////////////////////////////////////////////////////////////////////
//
// FUNCTION: getIt(getitAttributes)
//
// DESCRIPTION: 4.02 launcher. Initiates installation and/or delivery
// into the KDM 4.02 client and passes along values via the "getitAttributes"
// string. Note: The "s" param is not used by the 4.02 client, but it is 
// necessary for non-interception by KDM 3.x that may be already
// installed.
//
//////////////////////////////////////////////////////////////////////////
var HIQ = null;
function getIt(getitAttributes)
{
	if(HIQ == null)
		HIQ = new KDX_Wrapper();
	if(!HIQ.checkInstalled())
	{
		window.location.href = gVideoPortalRedirectUrl + "?" + getitAttributes;
		return false;
	}

	var htmlArgs = parseArgs(getitAttributes);
	return HIQ.DownloadVideo(htmlArgs["kget"]);
		
	//
}

function checkForAOL(){
	var isAOL = false;
	if(navigator.appVersion.indexOf("AOL") != -1 || navigator.userAgent.indexOf("AOL") != -1){
		isAOL = true;
	}
	return isAOL;
}

////////////////////////////////////////////////////////////////////////////////
// FUNCTION: doFallback(attributes)
//
// DESCRIPTION: Redirects to fallback page. Uses values passed in via
// "attributes" to determine fallback server. Note: This function should be
// customized for implementations that have a different fallback flow.
////////////////////////////////////////////////////////////////////////////////
function doFallback(attributes)
{	
	if(isPlatformSupported() && isWMPOK())
		window.location.href = gFallbackUrl;
	else
		window.location.href = gMinSysReqUrl;
}

////////////////////////////////////////////////////////////////////////////////
// FUNCTION: doRedirect(newURL)
//
// DESCRIPTION: If user is in a popup window, calls location.replace(),
// otherwise calls location.href() for optimized user experience.
////////////////////////////////////////////////////////////////////////////////
function doRedirect(newURL)
{
	if(self.name != "popup")
		window.navigate(newURL);
	else{
        var popup = window.open(newURL,"fallback");		
        popup.focus();
		window.close();
	}		
}

//////////////////////////////////////////////////////////////////////////
// FUNCTION: isPlatformSupport()
//
// DESCRIPTION: Detects browser/platform and returns true
// if user is supported for KDM 4.02.
//////////////////////////////////////////////////////////////////////////
function isPlatformSupported()
{	
	if(minSysReqIsMeet == -1){
	
		var agt = navigator.userAgent.toLowerCase();
		var major = parseInt(navigator.appVersion); 
		
		var osIsOk = ((agt.indexOf("windows nt 5.1")!=-1) || (agt.indexOf("xp")!=-1) ); // Windows XP
		
		if (alertsMode) alert(navigator.userAgent);		
		if (alertsMode) alert(navigator.appVersion);
	
		var isXPSP2 = window.navigator.userAgent.indexOf("SV1") != -1;
		
		var isIE = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));

		if(agt.indexOf("msie ") != -1){
			var IEversion = parseInt(agt.substr((agt.indexOf("msie ")+5), 1));
		}else
			var IEversion = -1;
			
		var isIE6 = (isIE && (major == 4) && (IEversion >= 6));
	
		minSysReqIsMeet = isIE6 && osIsOk && isOSSupported();
	}
	return minSysReqIsMeet;
}

function isWMPOK(){
	var playerIsOk = false;
	try{
		var mediaPlayer = new ActiveXObject("WMPlayer.OCX");
		var version = parseInt(mediaPlayer.versionInfo);
		
		if(alertsMode)  alert("mediaPlayer.versionInfo "+version);
		if(version < 10)
			playerIsOk = false;
		else
			playerIsOk = true;
	}catch(e){};
	
	return playerIsOk;
}

function WMPcheck(){
	var playerIsOk = false;

	if(isPlatformSupported()){
		playerIsOk = isWMPOK();
		if(!playerIsOk){
			window.setTimeout("showWMPDialog()", 1000);		
			//showWMPDialog();
		}
	}
	
	return playerIsOk;
}

function isOSSupported()
{	
	return (navigator.platform == "Win32");
}

function showWMPDialog(){
	var features = "dialogWidth: 400px; dialogHeight: 175px; scroll: no; help: no; status: no;";
	window.showModalDialog(gWMPDialogUrl, window, features);
}

//////////////////////////////////////////////////////////////////////////
// FUNCTION: isXPSP2()
//
// DESCRIPTION: Returns true for XPSP2 platforms.
//////////////////////////////////////////////////////////////////////////
function isXPSP2()
{
    return window.navigator.userAgent.indexOf("SV1") != -1;
}
                
//////////////////////////////////////////////////////////////////////////
// FUNCTION: doXPSP2()
//
// DESCRIPTION: On Install and Upgrade Pages, display hidden XPSP2 UI
//////////////////////////////////////////////////////////////////////////
function doXPSP2()
{
	xpsp2.style.display = "block";
	standard.style.display = "none";
	
	if(window.resizeBy){
		var windowWidth = document.body.clientWidth;
		var windowHeight = document.body.clientHeight;		
		var w = screen.availWidth;
		var h = screen.availHeight;

		var newWidth = 	640;
		var newHeight =	710;
		
		var y = (h-newHeight)/2;
		
		if(newHeight > h){
			y = 0;
			
			newWidth = 662;
			newHeight = h*0.9;						
		}

		var x = (w-newWidth)/2;
		

		try {
			window.resizeBy(newWidth - windowWidth, newHeight - windowHeight);
			window.moveTo(x, y);
		} catch(e) {};
	}
}

////////////////////////////////////////////////////////////////////////////////
// FUNCTION: isUrn(i_urn)
//
// DESCRIPTION: Returns true if "i_urn" is of urn syntax
// (i.e., urn:kid:kontiki:somenetwork:somemoid)
////////////////////////////////////////////////////////////////////////////////
function isUrn(i_urn)
{
    if (i_urn.indexOf("urn") != -1)
        return true;
    else
        return false;
}

////////////////////////////////////////////////////////////////////////////////
// FUNCTION: normalizeMoid(i_urn)
//
// DESCRIPTION: Converts string "i_urn" of urn syntax to a moid.
////////////////////////////////////////////////////////////////////////////////
function normalizeMoid(i_urn)
{
    var index = 0;
    var tempString;
    if (i_urn.indexOf("urn") != -1)
    {
        while(true) 
        {
            index = i_urn.indexOf(":", index);
            if (index == -1 )
                break;
                
            index++;
            tempString = i_urn.substring(index, i_urn.length);
        }    
        
        return tempString;
    }
    else 
        return i_urn;
}

////////////////////////////////////////////////////////////////////////////////
// FUNCTION: getDMS(i_urn)
//
// DESCRIPTION: Parses out the DMS domain of the URN "i_urn". The map "m" needs
// to be customized for implementations used by externally deployed DMS's.
////////////////////////////////////////////////////////////////////////////////
function getDMS(i_urn)
{
    var index1;
    var index2;
    var index3;
    var tempString = "";
    var domain = null;
    var defaultDomain = gDefaultDMS;
    
    // parse out the kid from the urn
    if (i_urn.indexOf("urn") != -1)
    {
        index1 = (i_urn.indexOf(":") + 1);
        index2 = (i_urn.indexOf(":", index1) + 1);
        index3 = i_urn.indexOf(":", index2);
        tempString = i_urn.substring(index2, index3);
    }
        
    if(tempString != "") domain = gDMSMap[tempString];
    if(domain) return domain;
    else return defaultDomain;

}

////////////////////////////////////////////////////////////////////////////////
// FUNCTION: parseHTMLArgs()
//
// DESCRIPTION: Returns a string of param/value pairs from the 
// location.search query string
////////////////////////////////////////////////////////////////////////////////
function parseHTMLArgs()
{
    if( document.location.search.length > 0 )
    {
	    //skip the '?'
        args = parseArgs(document.location.search.substring(1));

        return args;
    }
    else return "";
}

////////////////////////////////////////////////////////////////////////////////
// FUNCTION: parseArgs(string)
//
// DESCRIPTION: Returns JS object of param/value pairs from "string" of syntax
// "foo=bar&fee=bat&fie=bas"
////////////////////////////////////////////////////////////////////////////////
function parseArgs(string)
{
    var args = new Object();
    var pairs = string.split("&");
        
    for(var i = 0; i < pairs.length; i++)
    {
        // Look for "name=value"
        var pos = pairs[i].indexOf('='); 
        // if not found, skip to next
        if (pos == -1) continue; 
        // Extract the name
        var argname = pairs[i].substring(0,pos); 
    
        // Extract the value
        var value = unescape( pairs[i].substring(pos+1) );
        // Store as a property
        args[argname] = value; 
    
        //alert(argname + " = " + value);
    }
    return args;
}

////////////////////////////////////////////////////////////////////////////////
// FUNCTION: getDomain(i_url)
//
// DESCRIPTION: Returns "server.domain.ext" of "i_url" of syntax
// "http://server.domain.ext/foobar/file.foo"
////////////////////////////////////////////////////////////////////////////////
    
function getDomain(i_url)
{
    var index = i_url.indexOf("://");
    var domain = i_url.substring(index + 3);
    domain = domain.substring(0, domain.indexOf("/"));
    return domain;
}

//Moviefone uninstall routine
function uninstallMoviefone(){
	try{
		if (alertsMode) alert("Checking is moviefone installed");
		
		var mgAuthToken		= "moviefone_classic | *.kontiki.com, *.aol.com | urn:kid:fone:";
		var mgAuthSignature	= "4l8vrwbXnkXiMYaP2HRVJSTQM0Uw2Ar309u5yvo5YlxEGlxD37FBhk36ldaKJ1qW9bYrA8Lb+3RT/qRA6B8Zm2EJjqR2zJ7um8IM0ip06WT1ytp8O+rBgkyx4gSsdwNWxnOTgGK709vnLLrSRPnebhJM93au5WWVgMBJP/BDAME=";	
		
		var minstall = new ActiveXObject("KDX.SecureInstall");		
		minstall.authorize(mgAuthToken, mgAuthSignature);
		
		if(minstall.pref("kdx.installed")){
			if (alertsMode) alert("Movifone found, trying to uninstall");
			minstall.uninstall();			
		}else{
			if (alertsMode) alert("Movifone not found");
		}		
			
	}catch(e){
		if (alertsMode) alert("Exception while Moviefone uninstall routine");
	}
}

//-->

