﻿if (!window.SLS)
	window.SLS = {};

// deployment settings
SLS.hqPlayerSlUri = 'http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0';//'http://go.microsoft.com/fwlink/?linkid=124807';
SLS.hqPlayerMsLogUri = 'http://msmmod.orcsweb.com/'; 
SLS.hqPlayerMsLogDelay = 1000;
SLS.appName = 'HOME';
SLS.appVersion = '3.0';
SLS.hqPlayerSlVersion = '3.0.40624.0';
SLS.fallBackUri = 'http://www.istreamplanet.com/index.aspx';
SLS.silverplayerUri = 'ClientBin/'+riaTemplate;
SLS.silverplayerPage = 'http://www.istreamplanet.com/index.aspx';

SLS.fallBackCookieName = function() {
    return SLS.appName + "fallbackNoPrompt";
}

SLS.entryFlowCookieName = function() {
    return SLS.appName + "entryFlow";
}

SLS.appIdCookieName = function() {
    return SLS.appName + "appId";
}

SLS.installFlowCookieName = function() {
    return SLS.appName + "installFlow";
}

// common functions
SLS.getClientState = function() {
    if (!SLS.clientState) {
        var o = {};
        o.uid = SLS.getUid();
        o.isFallbackCookieDefined = SLS.isCookieDefined(SLS.fallBackCookieName(), "1");
        o.isLogEntryFlowCookieDefined = SLS.isCookieDefined(SLS.entryFlowCookieName(), "1");
        o.isLogInstallFlowCookieDefined = SLS.isCookieDefined(SLS.installFlowCookieName(), "1");
        o.isSlVersionInstalled = Silverlight.isInstalled(SLS.hqPlayerSlVersion);
        o.isSlVersionSupported = Silverlight.supportedUserAgent(SLS.hqPlayerSlVersion.substring(0, 1).concat(".0"));
        o.isSlUpgradeRequired = !o.isSlVersionInstalled && Silverlight.isInstalled(null);
        SLS.clientState = o;
		
    }
	if(SLS.clientState){
		var state = SLS.clientState;
		if (state.isSlVersionSupported==false){
            o = 0;
			SLS.NotSupported();
		}else{
			return SLS.clientState;
		}
	}
};

SLS.onHqPlayerPageBeforeUnload = function() {
    // log abandoned install on badge
    if (SLS.installState == 0) {
        SLS.logInstallFlow(3);
    }

    // log app event
    if (SLS.hqAppSessionId)
        SLS.logAppEvent();

    // force delay to allow logging to complete
    var date = new Date();
    while (new Date() - date < SLS.hqPlayerMsLogDelay);
};


SLS.isCookieDefined = function(name, value)
{
	var cookieValue = SLS.getCookieValue(name);
	return SLS.equals(cookieValue, value);
};

SLS.getCookieValue = function(name, caseSensitive)
{
	var cookie = document.cookie;

	if (cookie && cookie.length > 0)
	{
		var items = cookie.split(";");
		return SLS.getParamValue(items, name, caseSensitive);
	}
};


SLS.equals = function(s1, s2, caseSensitive)
{
	if (s1 == s2)
		return true;
	else if (s1 == null || s2 == null || caseSensitive)
		return false;
	else
		return s1.toLowerCase() == s2.toLowerCase();
};

SLS.trim = function(s)
{
	return s.replace(/^\s+|\s+$/g, "");
};

SLS.setCookie = function(name, value, hours)
{
	if (hours)
	{
		var expDate = new Date(new Date().getTime() + hours * 60 * 60 * 1000);
		document.cookie = name + "=" + value + ";expires=" + expDate.toGMTString();
	}
	else
	{
		document.cookie = name + "=" + value;
	}
};

SLS.clearCookie = function(name)
{
	document.cookie = name + "=;expires=Thu, 01-Jan-1970 00:00:01 GMT";
};

SLS.getUid = function()
{
    var uid = SLS.getCookieValue(SLS.appIdCookieName());

	if (!uid)
	{
		uid = Math.uuid();
		SLS.setCookie(SLS.appIdCookieName(),uid);
	}

	return uid;
};

SLS.getSilverlight = function() {
	var state = SLS.getClientState();
	var installRunning = '';
	installRunning += '		<p style="text-align:left;width:960px;">\n';
	installRunning += '			<img src="images/home_installLeft.png" border="0" align="left"/>\n';
	installRunning += '			<br/>\n';
	installRunning += '			<p style="margin-top:20px;">Thank you for choosing to upgrade your browser with Microsoft Silverlight 3.</p>\n';
	installRunning += '			<p>Your upgrade will begin shortly.  If it doesn\'t, click <a href="#" onclick="SLS.getSilverlight();" style="color:#0072bc;">here</a> to retry.</p>\n';
	if(state.isSlUpgradeRequired){
		installRunning += '			<p>When the upgrade completes, close all browser windows and relaunch to continue your experience.</p>\n';
	}else{
		installRunning += '			<p>When the upgrade completes your browser should automatically refresh.  If this does not occur please refresh your browser manually.</p>\n';
	}
	installRunning += '			<p>You do <u>not</u> need to restart your computer.</p>\n';
	installRunning += '		</p>\n';

    document.getElementById("txt1").innerHTML = installRunning;
    SLS.installState = 1;
    SLS.logInstallFlow(1);
	window.location=SLS.hqPlayerSlUri;
};
SLS.NotSupported = function(){
	var install1 = '<div id="install1" style="padding-top:10px;margin:0px auto;width:'+xapW+'px;height'+xapH+'px;position:relative;">\n';
	install1 += '	<div id="txt0" style="text-align:center;">\n';
	if(bannerImage!=''){
		install1 += '		<img src="'+bannerImage+'" border="0"/>\n';
	}
	install1 += '	</div>\n';
	install1 += '	<div style="clear:both;"></div>\n';
	install1 += '	<div id="txt1" style="position:relative;width:600px;margin:0px auto;">\n'; // text block
	install1 += '		<p style="font-size:24px;text-align:left;width:960px;" class="bigtext">\n';
	install1 += '			<img src="images/home_installLeft.png" border="0" align="left"/>\n';
	install1 += '			<br/>You are using a browser or operating system<br/>that is not supported.<br/><br/>\n';
	install1 += '			Please click <a href="http://ims.istreamplanet.com/events/help.html#sysreq" target="help">here</a> for a list of supported configurations.\n';
	install1 += '		</p>\n';
	install1 += '		\n';
	install1 += '	</div>\n'; //ends txt1
	install1 += '	<div id="txt4"></div>\n';
	install1 += '</div>';
	var divTease = document.createElement("DIV");
	divTease.innerHTML = install1;
	document.getElementById(playerTargetNode).appendChild(divTease);
}
SLS.NoSilverlight = function(doNoPrompt) {
    SLS.installState = 2;
    SLS.logInstallFlow(2);
    if (doNoPrompt) {
        SLS.setCookie(SLS.fallBackCookieName(), 1, 48);
    }
    window.location.href = SLS.fallBackUri;
};


SLS.getParamValue = function(items, name, caseSensitive) {
    for (var i = 0; i < items.length; i++) {
        var item = items[i].split("=");

        if (SLS.equals(SLS.trim(unescape(item[0])), name, caseSensitive))
            return item[1] ? unescape(item[1]) : item[1];
    }
};

SLS.showSilverlightApplication = function() {
    var state = SLS.getClientState();
	var dynInitParams = '';
	if (dynProps) {
		for(var d=0; d<dynProps.riaProperties.length; d++){
			if(d>0){dynInitParams += ',';}
			dynInitParams += dynProps.riaProperties[d].ckey+'='+dynProps.riaProperties[d].cval;
		}
	}

    if (state.isSlVersionInstalled) {
        // log successful install
        if (state.isLogInstallFlowCookieDefined) {
            SLS.logInstallFlow(4);
        }
        var sl = Silverlight.createObjectEx(
		{
		    id: "sl",
		    source: SLS.silverplayerUri,
		    properties: { width: xapW+"px", height: xapH+"px", windowless: "true", enableHtmlAccess: "true", allowHtmlPopupWindow: "true", version: SLS.hqPlayerSlVersion, background: "transparent", initParams: dynInitParams+",url=http://www.istreamplanet.com/ClientBin/silverlight-commercial.wmv" }
		});

        // show the player
        var divPlayer = document.createElement("DIV");
        divPlayer.innerHTML = '<div id="hqPlayer" style="margin:0px auto;width:'+xapW+'px;height:'+xapH+'px;position:relative;overflow:hidden;"><div id="slContainer" style="width:'+xapW+'px;height'+xapH+'px;position:absolute;left:0px;top:0px;">' + sl + '</div></div><div style="margin:-15px auto;background:transparent;height:auto !important;min-height:20px;margin-bottom:15px;"></div>';
        document.getElementById(playerTargetNode).appendChild(divPlayer);
    }
    else {
        // show the install prompt
        if (!state.isFallbackCookieDefined) {
            var install1 = '<div id="install1" style="padding-top: 10px;margin:0px auto;width:'+xapW+'px;height:'+xapH+'px;position:relative;font-family:arial;font-size:12px;font-weight:bold;">\n';
			install1 += '	<div id="txt0" style="text-align:center;">\n'; // logo image
			install1 += '	</div>\n';
			install1 += '	<div style="clear:both;height:10px;"></div>\n';
			install1 += '	<div id="txt1" style="position:relative;width:960px;margin:0px auto;">\n'; // text block
			install1 += '		<p style="text-align:left;width:960px;">\n';
			install1 += '			<img src="images/home_installLeft.png" border="0" align="left"/>\n';
			install1 += '			<br/>iStreamPlanet recommends Microsoft Silverlight 3 for the richest browsing experience.\n';
			install1 += '			With Silverlight 3, you get:\n';
			install1 += '			<br/><br/>&nbsp;&nbsp;&raquo;&nbsp;HD quality video using Smooth Streaming\n';
			install1 += '			<br/>&nbsp;&nbsp;&raquo;&nbsp;Full screen experiences\n';
			install1 += '			<br/>&nbsp;&nbsp;&raquo;&nbsp;Rich, dynamic environments\n';
			install1 += '			<br/>&nbsp;&nbsp;&raquo;&nbsp;The ability to pause and replay live action\n';
			install1 += '			<br/>\n';
			install1 += '			<br/>\n';
			install1 += '			<a id="btn1" href="javascript:SLS.getSilverlight();" title="Start Watching"><img src="images/glassStartWatchingSingle.png" width="170" height="53" alt="Start Watching" style="margin-left:150px;" border="0"/></a>\n';
			install1 += '			<br/>\n';
			install1 += '			<br/>\n';
			install1 += '			<span style="float:right;margin-left:10px;"><a href="http://www.microsoft.com/silverlight" alt="Microsoft Silverlight"><img src="images/transSL-smallV-blkT.png" border="0" /></a></span>\n';
			install1 += '			Clicking the button above will upgrade your browser with Microsoft Silverlight 3.  The update is free and will not require you to restart your computer.\n';
			install1 += '		</p>\n';
			install1 += '		\n';
			install1 += '	 	<br>\n';
			install1 += '	</div>\n'; //ends txt1
			install1 += '	<div id="txt4"></div>\n';
			install1 += '</div>';
            var divTease = document.createElement("DIV");

            divTease.innerHTML = install1;

            document.getElementById(playerTargetNode).appendChild(divTease);

            // log the install prompt
            SLS.installState = 0;
            SLS.logInstallFlow(0);
        }
        else {
            window.location.href = SLS.fallBackUri;
        }
    }
};

SLS.appendScript = function(index, src)
{
	try
	{
		var script = document.createElement("script");
		script.id = "script" + index;
		script.src = src;
		script.type = "text/javascript";

		var head = document.getElementsByTagName("head")[0];
		head.appendChild(script);
	}
	catch (e)
	{
		SLS.hqPlayerErrorHandler("SLS.appendScript", e);
	}
};

SLS.removeScript = function(index)
{
	try
	{
		var script = document.getElementById("script" + index);
		var head = document.getElementsByTagName("head")[0];
		head.removeChild(script);
	}
	catch (e)
	{
		SLS.hqPlayerErrorHandler("SLS.removeScript", e);
	}
};

SLS.logCount = 0;

SLS.setAppSessionId = function(id)
{
	SLS.hqAppSessionId = id;
};

SLS.logAppEvent = function()
{
	try
	{
		// svc parameters
		var u = SLS.hqAppSessionId;
		var i = SLS.logCount++;
		var t = new Date().getTime();

		// append script tag
		var src = SLS.hqPlayerMsLogUri + "/appevent.svc/parms?u=" + u + "&i=" + i + '&t=' + t + '&an=' + SLS.appName + '&av=' + SLS.appVersion;
		SLS.appendScript(i, src);
	}
	catch (e)
	{
		SLS.hqPlayerErrorHandler("SLS.logAppEvent", e);
	}
};

SLS.logEntryFlow = function() {
    try {
        var state = SLS.getClientState();


			if (!state.isLogEntryFlowCookieDefined) {
				// set the session cookie to avoid multiple calls
				SLS.setCookie(SLS.entryFlowCookieName(), "1");

				// svc parameters
				var s, o, p;
				var u = state.uid;
				var r = document.referrer;
				var i = SLS.logCount++;
				var t = new Date().getTime();

				//alert(state.isSlVersionSupported);
				// Silverlight install state
				if (state.isSlVersionInstalled && state.isSlVersionSupported)
					s = 2;
				else if (state.isSlUpgradeRequired && state.isSlVersionSupported)
					s = 1;
				else
					s = 0;

				// Silverlight support state
				if (state.isSlVersionSupported)
					o = 1;
				else
					o = 0;

				// player preference
				p = "";
				if (state.isFallbackCookieDefined)
					p = 5;


				// append script tag
				var src = SLS.hqPlayerMsLogUri + "/entryflow.svc/parms?u=" + u + "&r=" + r + "&s=" + s + "&o=" + o + "&p=" + p + "&i=" + i + '&t=' + t + '&an=' + SLS.appName + '&av=' + SLS.appVersion;
				SLS.appendScript(i, src);

				// force delay to allow logging to complete
				var date = new Date();
				while (new Date() - date < SLS.hqPlayerMsLogDelay);
			}
    }
    catch (e) {
		doHomeAnimation();
        SLS.hqPlayerErrorHandler("SLS.logEntryFlow", e);
		//script out animation flow
    }
};

SLS.logInstallFlow = function(action) {
try {
        // attempting to run silverlight install
        if (action == 1)
            SLS.setCookie(SLS.installFlowCookieName(), "1", 1);
        // install success
        else if (action == 4)
            SLS.clearCookie(SLS.installFlowCookieName());

        var state = SLS.getClientState();
        // svc parameters
        var u = state.uid;
        var a = action;
        var i = SLS.logCount++;
        var t = new Date().getTime();

        // append script tag
        var src = SLS.hqPlayerMsLogUri + "/install.svc/parms?u=" + u + "&a=" + a + "&i=" + i + '&t=' + t + '&an=' + SLS.appName + '&av=' + SLS.appVersion;
        SLS.appendScript(i, src);
    }
    catch (e) {
        SLS.hqPlayerErrorHandler("SLS.logInstallFlow", e);
    }
};