﻿if (!window.SLS)
	window.SLS = {};

// deployment settings
SLS.hqPlayerSlUri = "http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0";
SLS.hqPlayerMsLogUri = "http://msmmod.orcsweb.com/"; 
SLS.hqPlayerMsLogDelay = 1000;
SLS.appName = "Wynn Conference";
SLS.appVersion = "3.0";
SLS.hqPlayerSlVersion = "3.0.40624.0";						// SL version
SLS.fallBackUri = installPage; 								// redirect in event of failure (object code)
SLS.silverplayerUri = "ClientBin/ispSSPlayer.xap"; 			// uri of XAP (abs || rel)
SLS.silverplayerPage = "silverlight.asp"; 						// 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 installRunning = '';
	//installRunning += '		<p>\n';
	//installRunning += '			<img src="ClientBin/transSL-largeH.png" border="0" alt="Microsoft Silverlight" />\n';
	//installRunning += '		</p>\n';
	installRunning += '		<p>Thank you for choosing to upgrade your browser with Microsoft Silverlight '+SLS.appVersion+'.</p>\n';
	installRunning += '		<p>Your ';
	if(state.isSlUpgradeRequired){
		installRunning += 'upgrade';
	}else{
		installRunning += 'installation';
	}
	installRunning += '	will begin shortly.  If it doesn\'t, click <a href="#" onclick="SLS.getSilverlight();" style="color:#fd0;">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>\n';
	installRunning += '			You do not need to restart your computer.\n';
	installRunning += '			<span style="float:right;margin:10px 0px 0px 10px;"><a href="http://www.microsoft.com/silverlight" alt="Microsoft Silverlight"><img src="ClientBin/transSL-smallH.png" border="0" /></a></span>\n';
	installRunning += '		</p>\n';

    document.getElementById("instCopy").innerHTML = installRunning;
    SLS.installState = 1;
    SLS.logInstallFlow(1);
	window.location=SLS.hqPlayerSlUri;
};
SLS.NotSupported = function(){
	var install1 = '<div id="noSupport" style="width:600px;padding:100px auto;margin:0px auto;">\n'; // text block
	install1 += '	<div id="instLogo" style="width:250px;height:400px;float:left;margin-right:40px;">\n';
	install1 += '		<img src="ClientBin/LG_09 LARAs_SPH_FINAL.png" style="border:none;" alt="10a Entrega Anual Latin Grammy"/>\n';
	install1 += '	</div>\n';
	install1 += '	<div id="instCopy" style="width:305px;float:left;padding-top:60px;">\n';
	install1 += '		<p style="color:#fff;font-size:150%;font-weight:bold;" class="bigtext">\n';
	install1 += '			You are using a browser or operating system that is not supported.<br/><br/>\n';
	install1 += '			Please click <a href="http://www.microsoft.com/silverlight/get-started/install/default.aspx" target="help">here</a> for a list of supported configurations.\n';
	install1 += '		</p>\n';
	install1 += '	</div>\n';
	install1 += '</div>\n'; //ends txt1
	//var divTease = document.createElement("DIV");
	//divTease.innerHTML = install1;
	//document.getElementById("playerBox").appendChild(divTease);
	document.getElementById("playerBox").innerHTML = install1;
}
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){
		if (state.isSlVersionInstalled) {
			// log successful install
			if (state.isLogInstallFlowCookieDefined) {
				SLS.logInstallFlow(4);
			}
			var sl = Silverlight.createObjectEx(
			{
				id: "sl",
				source: SLS.silverplayerUri,
				properties: { width: xapW, height: xapH, enableHtmlAccess: "true", allowHtmlPopupWindow: "true", version: SLS.hqPlayerSlVersion, minRuntimeVersion: SLS.hqPlayerSlVersion, autoUpgrade: "true", background: "transparent", initParams: pInitParams }
			}
			);

			// show the player
			document.getElementById('playerBox').innerHTML = sl
			//bw = setInterval("testSize()",150);
		}
		else {
			// show the install prompt
			if (!state.isFallbackCookieDefined) {
				SLS.installState = 0;
				SLS.logInstallFlow(0);
				var install1 = '<div id="install" style="width:600px;height:100%;padding:40px auto;margin:0px auto;">\n';
				install1 += '	<div id="instLogo" style="width:250px;height:400px;float:left;margin-right:40px;">\n';
				install1 += '		<img src="ClientBin/LG_09 LARAs_SPH_FINAL.png" style="border:none;" alt="10a Entrega Anual Latin Grammy"/>\n';
				install1 += '	</div>\n';
				install1 += '	<div id="instCopy" style="width:305px;float:left;padding-top:60px;">\n';
				install1 += '		<p>\n';
				install1 += '			Watch this presentation of the LATIN GRAMMY PRE-TELECAST AWARDS using Microsoft Silverlight and Smooth Streaming\n';
				install1 += '			<ul>\n';
				install1 += '			<li>Direct live video</li>\n';
				install1 += '			<li>Highest resolution quality video</li>\n';
				install1 += '			<li>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/glassStartWatchingSingle.png" width="170" height="53" alt="Start Watching" border="0" /></a></span>\n';
				install1 += '		</p>\n';
				install1 += '		<p>\n';
				install1 += '			Clicking the button above will upgrade your browser with Microsoft Silverlight '+SLS.appVersion+'.  The upgrade is compatible with both Windows and Macintosh operating systems.  The update is free and will <span style="text-decoration:underline;">not</span> require you to restart your computer.\n';
				install1 += '			<span style="float:right;margin:10px 0px 0px 10px;"><a href="http://www.microsoft.com/silverlight" alt="Microsoft Silverlight"><img src="ClientBin/transSL-smallH.png" border="0" /></a></span>\n';
				install1 += '		</p>\n';
				install1 += '	</div>\n';
				install1 += '</div>\n';
			   
				var divTease = document.createElement("DIV");
				divTease.innerHTML = install1;
				document.getElementById("playerBox").appendChild(divTease);
			}
			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);
    }
};