var g_server = "http://mls.neulion.com/mlsvp/";
var g_pageurl = "http://www.mlssoccer.com";
var g_locResource = "http://mls.neulion.com/mlsvp/";

//Configure Firefox userAgent
if (window.navigator.userAgent.indexOf("Firefox/3.6") > 0) {
    changeuserAgent();
}


function changeuserAgent() {
    var altuserAgentGetter = function() {
        return "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2) Gecko/20100115 NeuLion/3.6";
    };
    if (Object.defineProperty) {
        Object.defineProperty(navigator, "userAgent", {
            get: altuserAgentGetter
        });
    } else if (Object.prototype.__defineGetter__) {
        navigator.__defineGetter__("userAgent", altuserAgentGetter);
    }
}

// Configure logging parameters
SLS.appName = "MLSVP";
SLS.appVersion = "10.0";
SLS.showSlates = true;	// wire up the events to show slates automatically
SLS.logEntryflow = true;  // log the entryflow (installed / current / supported)
SLS.logInstall = true;  // log full install experience (also acts as though logEntryflow=true)
SLS.hqPlayerMsLogUri = "http://mls.logqueue.com";	// will be different for testing/production
// Required Silverlight version
SLS.minSlVersion = "3.0.40624.0";
SilverlightControlHost = "iptvSLPlayerDiv";

// Use this section to configure messages

PromptOuterHTML1 = '<table cellpadding="0" cellspacing="0" border="0" style="font-family:arial;font-size:14pt;color:#C6C6C6;background-repeat:no-repeat;background-position:center top;background-image:url('+g_locResource+'images/slinstallbg.jpg)" width="860" height="324"><tr><td height="65" align="center" style="font-size:18pt;font-weight:bold">';
PromptOuterHTMLH = 'You\'re one simple step from...';
PromptOuterHTML2 = '</td></tr><tr><td height="230" align="center" style="padding-left:26px;padding-right:27px">';
PromptOuterHTML3 = '</td></tr><tr><td height="29"></td></tr></table>';

PromptFinishInstall = PromptOuterHTML1+PromptOuterHTMLH+PromptOuterHTML2+'<p>You are now installing Silverlight, refresh your browser when done.</p>'+PromptOuterHTML3;

PromptInstall = PromptOuterHTML1+PromptOuterHTMLH+PromptOuterHTML2+'<p>Getting access to exclusive MLS video content in a player featuring<br/>stunning HD quality video, enhanced controls, and more!</p><p><input type="button" value="Click to Begin Watching Video" onclick="InstallClicked()" style="cursor:pointer;cursor:hand;height:50px;color:#000000;font-size:16pt;font-weight:bold;border:0px;background-image:url('+g_locResource+'images/slinstallbutton.gif)" />';
if(window.g_iptvVideoId==null)
	PromptInstall += '<br/><a href="javascript:iptvOnSilverlightClose()" style="font-size:10pt;color:#1A66A0">No Thanks</a>';
PromptInstall += '</p><p style="font-size:10pt">Clicking the button above will upgrade your browser with the latest version of Microsoft Silverlight.<br/>The update is free and will not require you to restart your computer.<!--warn--></p>'+PromptOuterHTML3;

PromptUpgrade = PromptOuterHTML1+PromptOuterHTMLH+PromptOuterHTML2+'<p>Getting access to exclusive MLS video content in a player featuring<br/>stunning HD quality video, enhanced controls, and more!</p><p><input type="button" value="Click to Begin Watching Video" onclick="UpgradeClicked()" style="cursor:pointer;cursor:hand;height:50px;color:#000000;font-size:16pt;font-weight:bold;border:0px;background-image:url('+g_locResource+'images/slinstallbutton.gif)" />';
if(window.g_iptvVideoId==null)
	PromptUpgrade += '<br/><a href="javascript:iptvOnSilverlightClose()" style="font-size:10pt;color:#1A66A0">No Thanks</a>';
PromptUpgrade += '</p><p style="font-size:10pt">Clicking the button above will upgrade your browser with the latest version of Microsoft Silverlight.<br/>The update is free and will not require you to restart your computer.<!--warn--></p>'+PromptOuterHTML3;

PromptFinishUpgrade = PromptOuterHTML1+PromptOuterHTMLH+PromptOuterHTML2+'<p>You are now upgrading Silverlight. When this is done, please restart your browser.</p>'+PromptOuterHTML3;

PromptRestart = PromptOuterHTML1+PromptOuterHTMLH+PromptOuterHTML2+'<p>Please restart your browser.</p>'+PromptOuterHTML3;

PromptNotSupported = PromptOuterHTML1+PromptOuterHTML2+'<p>Sorry, this browser doesn\'t support Silverlight.<br/>';
if(window.g_iptvVideoId==null)
	PromptNotSupported += '<br/><a href="javascript:iptvOnSilverlightClose()" style="font-size:10pt;color:#1A66A0">Close</a>';
PromptNotSupported += '</p>'+PromptOuterHTML3;

// Popup alert that this browser is not officially supported but might work (Chrome)
WarningNotTested = "Warning! This browser is not officially supported but you may proceed";
// is specified will replace the text <!--warn--> on the "install" and "upgrade" slates defined above
PromptNotTested = "<br/><b>Warning!</b> This browser is not officially supported but you may proceed.";

if(window.addEventListener)
{
    window.addEventListener("load", onLauncherPageLoad, false);
}
else
{
    window.attachEvent("onload", onLauncherPageLoad);
}
	
function iptvWriteSLPlayer()
{
	var locScripts = g_locResource + "scripts/";
	
	var initParams = new Array();
	initParams[initParams.length] = "server=" + g_server;
	initParams[initParams.length] = "pageurl=" + g_pageurl;
	
	var str = "";
	
	var w = 1040;
	var h = 499;
	if(window.g_iptvVideoId!=null)
	{
		initParams[initParams.length] = "playertype=enhanced"; //enhanced, embedded
		w = 960;
		h = 401;
		
		str += '<div id="iptvSLPlayerDiv" align="center" style="width:'+w+'px;height:'+h+'px"><object id="iptvembed" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="'+w+'" height="'+h+'">';
	}
	else
	{
		str += '<div id="iptvOverlayLowerDiv" style="position:absolute;top:0px;left:0px;background-color:#000000;filter:alpha(opacity=70);-moz-opacity:.70;opacity:.70;z-index:1000001;display:none"></div>';
		str += '<div id="iptvOverlayUpperDiv" style="position:absolute;top:0px;left:0px;width:0px;height:0px;overflow:hidden;z-index:1000002"><div id="iptvSLPlayerDiv" align="center" style="position:absolute;left:0px;top:0px;width:'+w+'px;height:'+h+'px"><object id="iptvembed" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="'+w+'" height="'+h+'">';
		initParams[initParams.length] = "playertype=embedded"; //enhanced, embedded
	}
	
	str += '<param name="source" value="'+locScripts+'mls.xap"/>';
	str += '<param name="initParams" value="' + initParams.join(",") + '"/>';
	str += '<param name="onLoad" value="onSilverlightLoad" /><param name="onerror" value="onSilverlightError" /><param name="background" value="Transparent" /><param name="minRuntimeVersion" value="3.0.40624.0" /><param name="autoUpgrade" value="false" /><param name="Windowless" value="true" /><param name="enableHtmlAccess" value="true"/>';
	str += '</object></div>';
	str += '<div id="iptvCompanionDiv" style="position:absolute;left:0px;top:0px;width:300px;height:250px;display:none"></div>';
	
	if(window.g_iptvVideoId==null)
		str += '</div>';
	
	document.write(str);
}

iptvWriteSLPlayer();

function iptvOnSilverlightLoad(sender)
{
	document.getElementById("iptvCompanionDiv").innerHTML = "";
	if(window.g_iptvVideoId!=null)
	{
		iptvPlayVideo(window.g_iptvVideoId, window.g_iptvVideoLinkName, window.g_iptvAdTag);
	}	
}

function iptvOnSilverlightClose()
{
	iptvShowLightbox(false);
	document.getElementById("iptvCompanionDiv").innerHTML = "";
}

function iptvPlayVideo(id, linkName, adtag)
{
	if(adtag==null)
		adtag = "";
	iptvShowLightbox(true);
	// we need wait a while for loading slp object
    setTimeout("iptvPlaySlVideo('"+id+"','"+linkName+"','"+adtag+"')", 500);
}

function iptvPlaySlVideo(id, linkName, adtag)
{
    if(id==null)
		return;
    var slp = document.getElementById("iptvembed");
    if(slp && slp.Content && slp.Content.MainSL)
    {
        slp.Content.MainSL.playVideo(id, linkName, adtag);
    }
    else
    {
        setTimeout("iptvPlaySlVideo('"+id+"','"+linkName+"','"+adtag+"')", 500);
    }
}

function iptvShowAd(adType, adURL, adLink)
{
	var strAdHTML = "";
    if ("iframe" == adType && adURL != null)
	{
        strAdHTML = "<iframe width='300' height='250' frameborder='0' scrolling='no' src='" + adURL + "'></iframe>";
    }
    else if ("html" == adType && adURL != null)
    {
    	strAdHTML = adURL;
    }
    else if ("swf" == adType && adURL != null)
	{
        strAdHTML = "<object width='300' height='250' classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000'><param name='movie' value='" + adURL + "' /><embed pluginspage='http://www.adobe.com/go/getflashplayer' src='" + adURL + "' type='application/x-shockwave-flash' width='300' height='250'></embed></object>";
    }
    else if ("img" == adType && adURL != null)
	{
        if (adLink != null && adLink.length > 0)
            strAdHTML = "<a href='" + adLink + "' target='_blank'>";
        strAdHTML += "<img src='" + adURL + "' border='0' />";
        if (adLink != null && adLink.length > 0)
            strAdHTML += "</a>";
    }
    var div = document.getElementById("iptvCompanionDiv");
    div.innerHTML = strAdHTML;
    
    if(window.g_iptvVideoId!=null)
	{
		var x = 0;
		var y = 0;
		var temp=document.getElementById("iptvSLPlayerDiv");
		do
		{
			x += temp.offsetLeft;
			y += temp.offsetTop;
			temp = temp.offsetParent;
		}
		while(temp!=null)
	
		div.style.left = (x + 653) + "px";
		div.style.top = (y + 20) + "px";
	}
    
    div.style.display = "";
}

function iptvHideAD()
{
	document.getElementById("iptvCompanionDiv").style.display = "none";
}

function iptvSLJSProxy(action, value)
{
	switch(action)
	{
		case "trackevent":
			if(window.pageTracker)
			{
				var v = value.value;
				if(v!=null && v.length==0)
					v = null;
				else
					v = Number(v);
				if(value.label!=null && value.label.length==0)
					value.label = null;
				pageTracker._trackEvent(value.category, value.action, value.label, v);
		    }
			break;
		case "trackpageview":
		    if(window.pageTracker)
		    {
		    	pageTracker._trackPageview(value);
		    }
			break;
		case "popout":
			break;
		case "dim":
			break;
	}
}

function iptvShowLightbox(bShow)
{
	if(window.g_iptvVideoId==null)
	{
		var overlayLowerDiv=document.getElementById("iptvOverlayLowerDiv");
		var overlayUpperDiv=document.getElementById("iptvOverlayUpperDiv");
		var slDiv=document.getElementById("iptvSLPlayerDiv");
		var adDiv=document.getElementById("iptvCompanionDiv");
			
		if (bShow)
		{
			var sels=document.getElementsByTagName("SELECT");
			for(var i=0;i<sels.length;i++)
				sels[i].style.visibility = "hidden";
	
			if(window.navigator.userAgent.indexOf("Firefox/")==-1)
				document.documentElement.style.overflow = "hidden";
			overlayLowerDiv.style.display = "";
			overlayLowerDiv.style.width = document.documentElement.scrollWidth+"px";
			overlayLowerDiv.style.height = document.documentElement.scrollHeight+"px";
			overlayUpperDiv.style.width = document.documentElement.scrollWidth+"px";
			overlayUpperDiv.style.height = document.documentElement.scrollHeight+"px";
			var st = document.documentElement.scrollTop;
			if(window.navigator.userAgent.indexOf("Safari")>0)
				st = document.body.scrollTop;
			var l = (document.documentElement.offsetWidth-slDiv.offsetWidth)/2;
			var t = st + ((document.documentElement.clientHeight-slDiv.offsetHeight)/2);
			slDiv.style.left = l+"px";
			slDiv.style.top = t+"px";
			adDiv.style.left = (l+730)+"px";
			adDiv.style.top = (t+48)+"px";
		}
		else
		{
			var sels=document.getElementsByTagName("SELECT");
			for(var i=0;i<sels.length;i++)
				sels[i].style.visibility = "visible";
	
			overlayLowerDiv.style.display = "none";
			overlayUpperDiv.style.width = "0px";
			overlayUpperDiv.style.height = "0px";
	
			// Needed for bug workaround in Safari -- scroll bars won't reappear when overflow is set back to ""
			if(window.navigator.userAgent.indexOf("Safari")>0)
			{
				document.documentElement.style.overflow = "scroll";
				setTimeout("iptvHideLightboxLater()",100);
			}
			else if(window.navigator.userAgent.indexOf("Firefox/")==-1)
				iptvHideLightboxLater();
		}
	}
}
function iptvHideLightboxLater()
{
	document.documentElement.style.overflow = "";
}