﻿if (!window.SLS)
	window.SLS = {};

// deployment settings
SLS.hqPlayerSlUri = "http://go.microsoft.com/fwlink/?linkid=124807";
SLS.hqPlayerMsLogUri = "http://msmmod.orcsweb.com/";
SLS.hqPlayerMsLogDelay = 1000;
SLS.appName = "Our World Live";
SLS.appVersion = "3.0";
SLS.hqPlayerSlVersion = "3.0.40624.0"; 					// SL version
SLS.fallBackUri = "player.html"; 							// redirect in event of failure (object code)
SLS.silverplayerUri = "/ClientBin/OWLPlayer.xap"; 			// uri of XAP (abs || rel)
SLS.silverplayerPage = "player.html"; 						// page name that hosts the player

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 install2 = '<div style="background-image:url(/ClientBin/Images/installBackground.jpg);background-repeat:no-repeat;width:1000px;height:469px;">\n';
	install2 += '  <p style="text-align:center;font-family:Arial, Helvetica, sans-serif;color:#FFF"><br />\n';
	if (window.ArtistName)
	{
		install2 += '	<span style="font-size:28px;font-weight:bold;">Welcome to Billboardlive.com, ';
		install2 += window.ArtistName;
		install2 += ' LIVE!</span><br />\n';
	}
	else
	{
		install2 += '	<span style="font-size:28px;font-weight:bold;">Welcome to Billboardlive.com!</span><br />\n';
	}
	install2 += '	<span style="font-size:24px;font-weight:bold;">Thank you for choosing to upgrade your browser with</span><br />\n';
	install2 += '    <span style="font-size:24px;font-weight: bold;">The Newest Microsoft Silverlight</span>\n';
	install2 += '    <br />\n';
	install2 += '    <br />\n';
	install2 += '    <br />\n';
	install2 += '    <br />\n';
	install2 += '    <br />\n';
	install2 += '    <span style="font-size:18px">Your upgrade will begin shortly. &nbsp;If it does not, <a href="#" onclick="SLS.getSilverlight();" style="color:#fd0;">click here</a> to retry.</span>\n';
	install2 += '    <br />\n';
	install2 += '    <br />\n';
	install2 += '    <br />\n';
	install2 += '    <br />\n';
	if (state.isSlUpgradeRequired) {
	    install2 += '		<span style="font-size:18px">When the upgrade completes, close all browser windows and relaunch to continue your experience.</span>\n';
	} else {
	install2 += '  <span style="font-size:18px">When the upgrade completes your browser should automatically refresh.<br />If this does not occur please refresh your browser manually.</span>\n';
	}
	install2 += '    <br />\n';
	install2 += '    <br />\n';
	install2 += '    <br />\n';
	install2 += '    <br />\n';
	install2 += '<span style="font-size:24px;">You do not need to restart your computer.</span>\n';
	install2 += ' </p>\n';
	install2 += '</div>\n';
	document.getElementById("install").innerHTML = install2;
	SLS.installState = 1;
	SLS.logInstallFlow(1);
	window.location = SLS.hqPlayerSlUri;
};
SLS.NotSupported = function() {
	var install1 = '\n';
	install0 += '	<div style="background-image:url(ClientBin/Images/installBackground.jpg);background-repeat:no-repeat;width:1000px;height:469px;">\n';
	install0 += '	  <p style="text-align:center;font-family:Arial, Helvetica, sans-serif;color:#FFF"><br />\n';
	install0 += '	    <br />\n';
	install0 += '	    <br />\n';
	install0 += '	    <br />\n';
	install0 += '	    <br />\n';
	install0 += '	    <br />\n';
	install0 += '	    <br />\n';
	install0 += '		<span style="font-size:28px;font-weight:bold;">Sorry you are using a browser or an opperating system</span><br />\n';
	install0 += '	    <span style="font-size:24px;font-weight: bold;">that is not supported.</span>\n';
	install0 += '	</p>\n';
	install0 += '	</div>\n';
	var divTease = document.createElement("DIV");
	divTease.innerHTML = install0;
	document.getElementById("install").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 (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();

	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", enableHtmlAccess: "true", allowHtmlPopupWindow: "true", onError: "onSilverlightError", onLoad: "OnLoad", enableGPUAcceleration: "true", windowless: "true", version: SLS.hqPlayerSlVersion, minRuntimeVersion: SLS.hqPlayerSlVersion, background: "transparent", initParams: pInitParams }
		});

		// 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;"><div id="slContainer" style="width:' + xapW + 'px;height:' + xapH + 'px;position:absolute;left:0px;top:0px;">' + sl + '</div></div>';
		//document.body.appendChild(divPlayer);

		var install = document.getElementById("install");
		install.appendChild(divPlayer);
	}
	else
	{
		// show the install prompt
		if (!state.isFallbackCookieDefined)
		{
			var install1 = '\n';
			install1 += '<div style="background-image:url(/ClientBin/Images/installBackground.jpg);background-repeat:no-repeat;width:1000px;height:469px;">\n';
			install1 += '  <p style="text-align:center;font-family:Arial, Helvetica, sans-serif;color:#FFF"><br />\n';
			if (window.ArtistName)
			{
				install1 += '	<span style="font-size:28px;font-weight:bold;">Welcome to Billboardlive.com, ';
				install1 += window.ArtistName;
				install1 += ' LIVE!</span><br />\n';
			}
			else
			{
				install1 += '	<span style="font-size:28px;font-weight:bold;">Welcome to Billboardlive.com!</span><br />\n';
			}
			install1 += '	<span style="font-size:18px;font-weight:bold;">Watch this presentation using</span><br />\n';
			install1 += '    <span style="font-size:18px;font-weight: bold;">Our World Live Player and Smooth Streaming</span>\n';
			install1 += '  <ul style="padding-left:325px;font-family:Arial, Helvetica, sans-serif;color:#FFF;font-weight:bold;">\n';
			install1 += '	<li>Direct live video with multiple camera angle viewing</li>\n';
			install1 += '	<li>\'HD\' quality video</li>\n';
			install1 += '	<li>The ability to pause and replay live action</li>\n';
			install1 += '	</ul>\n';
			install1 += '</p>\n';
			install1 += '<p style="text-align:center">\n';
			install1 += '	<a id="btn1" href="#" onclick="SLS.getSilverlight();" title="Start Watching"><img src="/ClientBin/images/silverlightInstallBtn.png" width="170" height="53" alt="Start Watching" border="0" /></a>\n';
			install1 += '</p>\n';
			install1 += '<p style="text-align:center;font-family:Arial, Helvetica, sans-serif;color:#FFF">\n';
			install1 += '	Clicking the button above will upgrade your browser with Microsoft Silverlight ' + SLS.appVersion + '.<br />The update is free and will not require you to restart your computer.\n';
			install1 += '</p>\n';
			install1 += '</div>\n';

			var divTease = document.createElement("DIV");
			divTease.innerHTML = install1;
			document.getElementById("install").appendChild(divTease);

			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) {
		SLS.hqPlayerErrorHandler("SLS.logEntryFlow", e);
	}
};

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);
	}
};
