﻿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 = "Desktop Virtualization Hour";
SLS.appVersion = "3.0";
SLS.hqPlayerSlVersion = "3.0.40624.0";						// SL version
SLS.fallBackUri = "http://www.desktopvirtualization.com/"; 							
SLS.silverplayerUri = "/ClientBin/MSFT.DV.UI2.xap";
//if(initXap){
//	SLS.silverplayerUri = initXap;
//}
SLS.silverplayerPage = installPage;


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() {
	var state;
    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){
		state = SLS.clientState;
		//alert(state.isSlVersionInstalled+', '+state.isSlVersionSupported);
		if (state.isSlVersionSupported==false){
            o = 0;
			SLS.NotSupported();
			return SLS.clientState;
		}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 id="install">';
	install2 += '<p class="bigtext">Installing Microsoft Silverlight</p>\n';
	//install2 += '<p>\n';
	//install2 += '	<img src="ClientBin/transSL-largeH.png" border="0" alt="Microsoft Silverlight" />\n';
	//install2 += '</p>\n';
	install2 += '<p>\n';
	install2 += '	Thank you for choosing to upgrade your browser with Microsoft Silverlight '+SLS.appVersion+'.\n';
	install2 += '	Your upgrade will begin shortly.  If it doesn\'t, click <a href="#" onclick="SLS.getSilverlight();" style="font-weight:bold;">here</a> to retry.\n';
	install2 += '</p>\n';
	install2 += '<p>\n';
	if(state.isSlUpgradeRequired){
		install2 += '	When the upgrade completes, close all browser windows and relaunch to continue your experience.\n';
	}else{
		install2 += '	When the upgrade completes your browser should automatically refresh.  If this does not occur please refresh your browser manually.\n';
	}
	install2 += '</p>\n';
	install2 += '<p style="margin-bottom:50px;">\n';
	install2 += '	You will not need to restart your computer during this process.\n';
	install2 += '</p>\n';
	install2 += '<div style="position:absolute;bottom:10px;right:10px;">\n';
	install2 += '	<a href="http://www.microsoft.com/silverlight" alt="Microsoft Silverlight"><img src="ClientBin/transSL-smallH.png" border="0" /></a>\n';
	install2 += '</div>\n';
	install2 += '</div>\n';
	if(!document.getElementById("installdiv")){
		var divTease = document.createElement("DIV");
		divTease.setAttribute('id','installdiv');
		divTease.setAttribute('style','width:'+xapW+'px;height:'+xapH+'px;position:relative;');
		document.getElementById("silverlightControlHost").appendChild(divTease);
	}
    document.getElementById("installdiv").innerHTML = install2;
    SLS.installState = 1;
    SLS.logInstallFlow(1);
	window.location=SLS.hqPlayerSlUri;
};
SLS.NotSupported = function(){
	var install0 = '<div id="install">';
	install0 += '<p class="bigtext">Unsupported Platform</p>\n';
	install0 += '<p>\n';
	install0 += '	You are using a browser or operating system that is not supported for Silverlight playback.\n';
	install0 += '</p>\n';
	install0 += '<p style="margin-bottom:50px;">\n';
	install0 += '	Please click <a href="http://www.microsoft.com/silverlight/get-started/install/default.aspx?reason=unsupportedplatform&v=2.0">here</a> for a list of supported configurations or more assistance with the installation process.\n';
	install0 += '</p>\n';
	install0 += '<div style="position:absolute;bottom:10px;right:10px;">\n';
	install0 += '	<a href="http://www.microsoft.com/silverlight" alt="Microsoft Silverlight"><img src="ClientBin/transSL-smallH.png" border="0" /></a>\n';
	install0 += '</div>\n';
	install0 += '</div>\n';
	if(!document.getElementById("installdiv")){
		var divTease = document.createElement("DIV");
		divTease.setAttribute('id','installdiv');
		divTease.setAttribute('style','width:'+xapW+'px;height:'+xapH+'px;position:relative;');
		document.getElementById("silverlightControlHost").appendChild(divTease);
	}
	document.getElementById("installdiv").innerHTML = install0;
}
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(!document.getElementById('silverlightPlayer')){
			if (state.isLogInstallFlowCookieDefined) {
				SLS.logInstallFlow(4);
			}
			var sl = Silverlight.createObjectEx(
			{
				id: "dvhPlayer",
				source: SLS.silverplayerUri,
				properties: { width: xapW, height: xapH, enableHtmlAccess: "true", allowHtmlPopupWindow: "true", onError: "onSilverlightError", autoUpgrade: "true", windowless: "true", version: SLS.hqPlayerSlVersion, splashscreensource: splashScreen, minRuntimeVersion: SLS.hqPlayerSlVersion, background: "transparent", initParams: pInitParams }
			});

			// show the player
			if(document.getElementById("install")){
				var pa = document.getElementById("install");
				pa.parentNode.removeChild(pa);
			}
			var divPlayer = document.createElement("DIV");
			divPlayer.setAttribute('id','silverlightPlayer');
			divPlayer.setAttribute('style','width:'+xapW+'px;height:'+xapH+'px;position:relative;');
			divPlayer.innerHTML = sl;
			document.getElementById("silverlightControlHost").appendChild(divPlayer);
		}
    }
    else {
        // show the install prompt
        if (!state.isFallbackCookieDefined) {
            var install1 = '<div id="install">\n';
			install1 += '<p class="bigtext">Microsoft Silverlight Required</p>\n';
			install1 += '<p>\n';
			install1 += '	Microsoft Silverlight is required in order to watch the following video presentation,\n';
			install1 += '	but it appears that your computer does not currently have the Silverlight plugin.\n';
			install1 += '	Please click <strong>Start Watching</strong> to install it.\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';
			if(wmpLink!=''){
				install1 += '	<br/>\n';
				install1 += '	<a id="btn2" href="'+wmpLink+'" class="smaller" title="Watch in Windows Media Player">or click here to view in Windows Media Player</a></span>\n';
			}
			install1 += '</p>\n';
			install1 += '<p class="smaller" style="margin-bottom:50px;">\n';
			install1 += '	Clicking the button above will upgrade your browser with Microsoft Silverlight '+SLS.appVersion+'.  The update is free and will not require you to restart your computer.\n';
			install1 += '	Silverlight may not be supported on all Safari or Chrome platforms.  You may be redirected to verify before continuing the upgrade.';
			install1 += '</p>\n';
			install1 += '<div style="position:absolute;bottom:10px;right:10px;">\n';
			install1 += '	<a href="http://www.microsoft.com/silverlight" alt="Microsoft Silverlight"><img src="ClientBin/transSL-smallH.png" border="0" /></a>\n';
			install1 += '</div>\n';
			install1 += '</div>\n';
			if(!document.getElementById("installdiv")){
				var divTease = document.createElement("DIV");
				divTease.setAttribute('id','installdiv');
				var addStyle = 'width:'+(xapW)+'px;position:relative;';
				if(xapW<=600){ 
					var d = 1 - Math.floor((700-xapW)/10)/100;
					if(d < 0.6) { d = 0.6; }
					addStyle += 'font-size:'+d+'em;'; 
				}
				divTease.setAttribute('style',addStyle);
				document.getElementById("silverlightControlHost").appendChild(divTease);
			}
            document.getElementById("installdiv").innerHTML = install1;

            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);
    }
};