// -----------------------------------------------------------------------------
// Globals
// Major version of Flash required
var requiredMajorVersion = 9;
// Minor version of Flash required
var requiredMinorVersion = 0;
// Minor version of Flash required
var requiredRevision = 28;
// -----------------------------------------------------------------------------

// Version check for the Flash Player that has the ability to start Player Product Install (6.0r65)
var hasProductInstall = DetectFlashVer(6, 0, 65);

// Version check based upon the values defined in globals
var hasRequestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);

var g_lastPath = null;
var g_lastId = null;
var g_isIE = (window.navigator.userAgent.indexOf("MSIE")>0);
function playWMV(path, x, y, volume, trackCG, trackTL)
{
	if(path.indexOf("?")>0)
		path += "&nopr=1";
	else
		path += "?nopr=1";
	g_lastPath = path;
	var wmp = document.getElementById("wmppane");
	if(g_isIE)
	{
		//wmp.style.left = x + "px";
		wmp.style.top = y + "px";
		wmp.style.display = "";
		wmp = document.getElementById("wmppane");
		wmp = wmp.contentWindow.document.getElementById("wmpMain");
		wmp.url = path;
		wmp.settings.volume = volume;
	}
	else
	{
		//wmp.style.left = x + "px";
		wmp.style.top = y + "px";
		wmp.style.display = "";
		wmp = document.getElementById("wmppane");
		wmp.contentWindow.writeWMP(path,424,false,640,null,null,null,true,false);
		wmp = wmp.contentWindow.document.getElementById("wmpMain")
	}
	document.getElementById("wmppane").contentWindow.davInitStream(wmp,_rsdDN,_rsdCI, trackTL, _rsdFQ, trackCG, _rsdSI, _rsdTC, _rsdPCG);
}
function wmpProxy(action, value)
{
	var wmp = document.getElementById("wmppane").contentWindow.document.getElementById("wmpMain");
	switch(action)
	{
	case "fullscreen":
		wmp.fullScreen = true;
		break;
	case "stop":
		wmp.controls.stop();
		break;
	case "pause":
		wmp.controls.pause();
		break;
	case "play":
		wmp.controls.play();
		break;
	case "setvolume":
		wmp.settings.volume = value;
		break;
	case "getvolume":
		return wmp.settings.volume;
		break;
	case "shareshow":
		document.getElementById("wmppane").style.display = "none";
		break;
	case "sharehide":
		document.getElementById("wmppane").style.display = "";
		break;
	case "position":
		var p = document.getElementById("wmppane");
		//p.style.left = value[0] + "px";
		p.style.top = value[1] + "px";
		break;
	case "ecm":
		wmp.enableContextMenu = true;
		break;
	case "hide":
		if(g_isIE)
			wmp.controls.stop();
		else
			document.getElementById("wmppane").contentWindow.writeWMP("");
		document.getElementById("wmppane").style.display = "none";
	}
}

function getPageUrl()
{
	var url = window.location.href;
	var i = url.indexOf("?");
	if(i>0)
		url = window.location.href.substring(0,i);
	return url.substring(0,url.lastIndexOf("/")+1);
}

function writeFlexPlayer(oemId, oemName, nielsenCI, isProduction, embed, inline)
{
	var serverStr = "server=" + location.href.substring(0, location.href.lastIndexOf("/")+1);
	var secureServerStr = serverStr;
	var consoleStr = "console";
	if(isProduction)
	{
		//serverStr = "server=http://sanford.jtvs.jumptv.com/XML/MediaPlayer/";
		//secureServerStr = "secureserver=https://sanford.jtvs.jumptv.com/XML/MediaPlayer/";
		
		//serverStr = "server=http://tor-dbr-qa.jumptv.com/XML/MediaPlayer/";		
		//secureServerStr = "secureserver=https://tor-dbr-qa.jumptv.com/XML/MediaPlayer/";
		
		serverStr = "server=http://www.olemisssports.com/XML/MediaPlayer/";
		secureServerStr = "secureserver=https://www.nmnathletics.com/XML/MediaPlayer/";
	}
	if ( hasProductInstall && !hasRequestedVersion ) {
		// MMdoctitle is the stored document.title value used by the installation process to close the window that started the process
		// This is necessary in order to close browser windows that are still utilizing the older version of the player after installation has completed
		// DO NOT MODIFY THE FOLLOWING FOUR LINES
		// Location visited after installation is complete if installation is required
		var MMPlayerType = (isIE == true) ? "ActiveX" : "PlugIn";
		var MMredirectURL = window.location;
	    document.title = document.title.slice(0, 47) + " - Flash Player Installation";
	    var MMdoctitle = document.title;
	
		AC_FL_RunContent(
			"src", "scripts/playerProductInstall",
			"FlashVars", "MMredirectURL="+MMredirectURL+'&MMplayerType='+MMPlayerType+'&MMdoctitle='+MMdoctitle+"",
			"width", "100%",
			"height", "100%",
			"align", "middle",
			"id", "console",
			"quality", "high",
			"bgcolor", "#000000",
			"name", "console",
			"allowScriptAccess","always",
			"type", "application/x-shockwave-flash",
			"pluginspage", "http://www.adobe.com/go/getflashplayer"
		);
	} else if (hasRequestedVersion) {
	
		var flashVars = new Array();
		if(g_isIE)
			flashVars[flashVars.length] = "isie=true";
		flashVars[flashVars.length] = serverStr;
		flashVars[flashVars.length] = secureServerStr;
		flashVars[flashVars.length] = "jtv=" + oemId;
		flashVars[flashVars.length] = "jtvname=" + oemName;
		flashVars[flashVars.length] = "nielsenci=" + nielsenCI;
		flashVars[flashVars.length] = "fids=141250,141445,141446,141545"; //prod
		//flashVars[flashVars.length] = "fids=40931,40932,40933";	// qa
		//flashVars[flashVars.length] = "fids=50247,50247,50247,50247"; //dev
		//flashVars[flashVars.length] = "qa=true";	// qa only
		if(window.location.search.length>1)
		{
			var nvs = window.location.search.substring(1).split("&");
			if(nvs.length>0)
			{
				for(var i=0;i<nvs.length;i++)
				{
					flashVars[flashVars.length] = nvs[i];
				}
			}
		}
		flashVars[flashVars.length] = "referrer="+encodeURIComponent(document.referrer);
	
		// if we've detected an acceptable version
		// embed the Flash Content SWF when all tests are passed
		AC_FL_RunContent(
				"flashVars", flashVars.join("&"),
				"src", consoleStr,
				"width", "100%",
				"height", "100%",
				"align", "middle",
				"id", "console",
				"quality", "high",
				"bgcolor", "#000000",
				"name", "console",
				"wmode", "transparent",
				"allowFullScreen", "true",
				"allowScriptAccess","always",
				"type", "application/x-shockwave-flash",
				"pluginspage", "http://www.adobe.com/go/getflashplayer"
		);
	  } else {  // flash is too old or we can't detect the plugin
	    var alternateContent = 'Alternate HTML content should be placed here. '
	  	+ 'This content requires the Adobe Flash Player. '
	   	+ '<a href=http://www.adobe.com/go/getflash/>Get Flash</a>';
	    document.write(alternateContent);  // insert non-flash content
	  }
}

function getSessionId()
{
	var sid = "";
	if(document.cookie!=null && document.cookie.length>0)
	{
		var crumbs = document.cookie.split(";");
		for(var i=0;i<crumbs.length;i++)
		{
			var curCrumb = crumbs[i].split("=");
			if(curCrumb[0].indexOf("JSESSIONID")==0)
			{
				if(unescape(curCrumb[1])!="undefined")			
				{
					sid = unescape(curCrumb[1]);
					break;
				}
			}
		}
	}
	return sid;
}

function writeWMP(url, h, bDisableContextMenu, w, mute, containerId, id, notIEShowControls, windowless)
{
	if(h==null)
		h = 360;
	
	if(w==null)
		w = 480;
		
	if(mute==null)
		mute = false;
	
	if(bDisableContextMenu==null)
		bDisableContextMenu=true;
		
	if(notIEShowControls==null)
		notIEShowControls = true;
		
	if(windowless==null)
		windowless = true;

	var isie = window.navigator.userAgent.indexOf("MSIE")>0;
	var ismac = window.navigator.userAgent.indexOf("Mac")>0;

	var strUIMode="none";   // Some Firefox clients may have the plugin therefore we change UIMode for consistency
	strUIMode="full";
	if (!isie && notIEShowControls)
		strUIMode="full";
	
	if(id==null)
		id = "wmpMain";
	
	var strHTML ='<object id="'+id+'" classid="clsid:6bf52a52-394a-11d3-b153-00c04f79faa6" type="application/x-oleobject" height="'+h+'" width="'+((isie)?"100%":w)+'">';
		if (bDisableContextMenu==true)
			strHTML+='<param name="enableContextMenu" value="false" />';
		strHTML+='<param name="autoStart" value="true" />';
		if(windowless)
			strHTML+='<param name="windowlessVideo" value="true" />';
		strHTML+='<param name="url" value='+url+' />';
		strHTML+='<param name="uiMode" value="'+strUIMode+'" />';
		strHTML+='<param name="mute" value="'+((mute)?"true":"false")+'" />';
		if(mute)
			strHTML+='<param name="volume" value="0" />';
		strHTML+='<embed type="';
		if(ismac)
			strHTML+='application/x-mplayer2';
		else
			strHTML+='application/x-ms-wmp';
		strHTML+='" src="'+url+'" autostart="true" showcontrols="1" height="'+h+'" width="'+w+'" mute="'+((mute)?"1":"0")+'"';
		if(mute)
			strHTML+=' volume="0"';
		strHTML += ' /></object>';

	if(containerId==null)
		containerId = "wmppane0";
	document.getElementById(containerId).innerHTML=strHTML;
}

var _rsdDN = "//secure-us.imrworldwide.com/";
var _rsdCI = "us-xostech";
var _rsdFQ = 10;
var _rsdSI = "null";
var _rsdTC = 0;
var _rsdPCG = 0;

function setJTVTokenCookie(key)
{
	try
	{
		if(key!=null)
			document.cookie = "jtvTokenCookie=" + escape(key);
		else
			document.cookie = "jtvTokenCookie=;expires=Fri, 31 Dec 1999 23:59:59 GMT;";
	}
	catch(e){}
}

function getJTVTokenCookie()
{
	if(document.cookie!=null && document.cookie.length>0)
	{
		var the_cookie = document.cookie;
		var crumbs = the_cookie.split(";");
		for(var i=0;i<crumbs.length;i++)
		{
			curCrumb = crumbs[i].split("=");
			if(curCrumb[0].indexOf("jtvTokenCookie")!=-1)   // Cookie was found
			{ 
				var val = unescape(curCrumb[1]);
				if(val!="undefined")			
				{
					return val;
				}
			}
		}
	}
	return null;
}
function iptvShowIFrame()
{
	if (document.getElementById("iptvIFrame")!=null)
		document.getElementById("iptvIFrame").style.display="";
}
function iptvHideIFrame()
{
	if (document.getElementById("iptvIFrame")!=null)
		document.getElementById("iptvIFrame").style.display="none";
}

function iptvShowCompanions(playerX, playerY, companions)
{
	for(var i=0;i<companions.length;i++)
	{
		var adsize = companions[i][0];
		switch(adsize)
		{
		case "300x250":
			iptvShowRightCompanion(playerX, playerY, companions[i]);
			break;
		case "728x90":
			iptvShowTopCompanion(playerX, playerY, companions[i]);
			break;
		}
	}
}
function iptvHideCompanions()
{
	iptvHideRightCompanions();
}

function iptvHideRightCompanions()
{
	var tbl = document.getElementById("iptvAdTbl");
	if (tbl != null)
	{
		if (!g_isIE && document.getElementById("iptvIFrame") != null)
			document.getElementById("iptvIFrame").style.display="none";
		tbl.setAttribute("anim_destX", (-1 * tbl.offsetWidth));

		if (g_isIE)
			iptvStartAnimation(tbl, false);
		else
			document.getElementById("iptvAdContainer").style.display="none";		
	}
}

function iptvShowRightCompanion(playerX, playerY, companion)
{
	var adContainer = document.getElementById("iptvAdContainer");
	if (adContainer == null)
	{
		adContainer=document.createElement("div");
		adContainer.id="iptvAdContainer"
		adContainer.style.width="320px";
		adContainer.style.height="290px";
		adContainer.style.position="absolute";
		adContainer.style.overflow="hidden";
		adContainer.style.left = (playerX + 645) +"px";
		adContainer.style.top = playerY + "px";
		document.body.appendChild(adContainer);

		var tbl = document.createElement("table");
		tbl.style.backgroundColor="#666666";
		tbl.id = "iptvAdTbl";
		tbl.cellPadding = "0";
		tbl.cellSpacing = "0";
		tbl.style.position = "absolute";
		tbl.style.top = "0px";
		tbl.style.left = "-319px";
		var oRow1=tbl.insertRow(-1);
		var oCell = oRow1.appendChild(document.createElement("td"));
		oCell.style.width="100%";
		oCell.style.textAlign = "right";
		oCell.style.paddingRight="3px";
		oCell.innerHTML = "<a style='color:#FFFFFF;font-size:8pt;font-family:arial' href='javascript:iptvHideRightCompanions()'>CLOSE</a>";

		var oRow2=tbl.insertRow(-1);
		var oCell2 = oRow2.appendChild(document.createElement("td"));
		oCell2.style.padding="5px";
		oCell2.vAlign = "top";

		var oDiv = oCell2.appendChild(document.createElement("div"));
		oDiv.id = "iptvAdDiv";
		oDiv.style.width = "300px";
		oDiv.style.height = "250px";

		adContainer.appendChild(tbl);
		
	}

	var obj = document.getElementById("wmppane");
	if(obj)
	{
		// Set HTML for ad
		if("iframe"==companion[1] && companion[2]!=null)
		{
			strAdHTML = "<iframe id='iptvIFrame' width='300' height='250' frameborder='0' scrolling='no' src='"+companion[2]+"'></iframe>";

			document.getElementById("iptvAdDiv").innerHTML = strAdHTML;

			// Animate ad table within ad box
			var tbl = document.getElementById("iptvAdTbl");
			tbl.style.left = (-1 * tbl.offsetWidth) + "px";
			tbl.setAttribute("anim_destX", 0);

			iptvStartAnimation(tbl, true);
		}		
		else
		{
			iptvHideRightCompanions();
		}
	}
}

function iptvShowTopCompanion(playerX, playerY, companion)
{
	var oDiv=document.getElementById("iptvTopAdDiv");
	if (oDiv==null)
	{
		oDiv = document.createElement("div");
		oDiv.id = "iptvTopAdDiv";
		oDiv.style.position = "absolute";
		oDiv.style.top = "0px";
		oDiv.style.left = "0px";
		oDiv.style.width = "728px";
		oDiv.style.height = "90px";
		document.body.appendChild(oDiv);
		oDiv.style.display = "none";
	}

	if("iframe"==companion[1] && companion[2]!=null)
	{
		var strAdHTML = "<iframe id='iptvTopIFrame' width='728' height='90' frameborder='0' scrolling='no' src='"+companion[2]+"'></iframe>";
		oDiv.innerHTML = strAdHTML;
		oDiv.style.display = "";
	}
	else
	{	
		oDiv.style.display="none";
	}
}

function iptvStartAnimation(obj, bShow)
{
	if (!g_isIE)
	{
		if (bShow)
		{
			iptvHideIFrame();
			setTimeout("iptvShowIFrame()", 1000);
		}
		else
			iptvHideIFrame();
	}

	if (!g_isIE)
		document.getElementById("iptvAdContainer").style.display="";

	if (obj.getAttribute("anim_destX") < obj.offsetLeft)
		obj.anim_moveleft = true;
	else
		obj.anim_moveleft = false;

	iptvAnimateItem();
}

function iptvAnimateItem()
{
	var iptvAniSpeed = 8;          // Animation speed - bigger==faster

	var obj=document.getElementById("iptvAdTbl");
	var bMoveLeft = obj.anim_moveleft;
	var currLeft = obj.offsetLeft;

	var iTargetPosX = obj.getAttribute("anim_destX");

	if (iTargetPosX != null)
	{
		if (!g_isIE && currLeft < -1200)
		{
			document.getElementById("iptvAdContainer").style.display="none";
			return;
		}
		
		if ((bMoveLeft == true && currLeft - iptvAniSpeed < iTargetPosX) || (!bMoveLeft && currLeft + iptvAniSpeed > iTargetPosX))
		{
			obj.style.left = iTargetPosX + "px";  // Stop animating
			if (!g_isIE && bMoveLeft)
				document.getElementById("iptvAdContainer").style.display="none";
		}
		else
		{
			var iMove = (bMoveLeft) ? -iptvAniSpeed : iptvAniSpeed;
			obj.style.left = currLeft + iMove + "px";
			setTimeout("iptvAnimateItem()", 1);
		}
	}
}
