// -----------------------------------------------------------------------------
// 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_lastChannelId = null;
var g_isIE = (window.navigator.userAgent.indexOf("MSIE")>0);
var jtvshl_aniSpeed = 8;           // Animation speed - bigger==faster
var jtvshl_direction = "right";   // Can be "right" | "bottom"

var jtvshl_debug=false;
var jtvshl_dbgDiv=null;

function playWMV(path, x, y, volume)
{
	g_lastPath = path;
	if(g_isIE)
	{
		var wmp = document.getElementById("wmppane");
		wmp.style.left = x + "px";
		wmp.style.top = y + "px";
		wmp.style.display = "";
		wmp = wmp.contentWindow.document.getElementById("wmpMain");
		wmp.url = path;
		wmp.settings.volume = volume;
	}
	else
	{
		var wmp = document.getElementById("wmppane");
		wmp.style.left = x + "px";
		wmp.style.top = y + "px";
		wmp.style.display = "";
		wmp.contentWindow.writeWMP(path,270,null,null,null,null,null,null,false)
		return;
		var win = window.open("popupplayer.jsp","wmvplayer","scrollbars=yes,menubar=no,height=270,width=480,top=0,left=0,resizable=no,toolbar=no,location=no,status=yes");
		if(win!=null)
			win.focus();
	}
}
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 popupReady(win)
{
	win.playWMV(g_lastPath, g_lastId, g_lastChannelId);
}
function slShowHide(sl, show, move)
{
	if(show)
	{
		sl.style.width = "100%";
		sl.style.height = "100%";
	}
	else
	{
		sl.style.width = "1px";
		sl.style.height = "1px";
	}
}
function playSL(path, x, y, volume)
{
	var sl = document.getElementById("slpane");
	sl.style.left = x + "px";
	sl.style.top = y + "px";
	slShowHide(sl, true);
	sl = sl.contentWindow.document.getElementById("iptvslembed");
	sl.content.Page.playSL(path, volume);
}
function slProxy(action, value)
{
	var sl = document.getElementById("slpane").contentWindow.document.getElementById("iptvslembed");
	switch(action)
	{
	case "stop":
		sl.content.Page.stop();
		break;
	case "pause":
		sl.content.Page.pause();
		break;
	case "play":
		sl.content.Page.play();
		break;
	case "setvolume":
		sl.content.Page.setVolume(value);
		break;
	case "getvolume":
		return sl.content.Page.getVolume();
		break;
	case "share":
		//sl.content.Page.updateShare(value);
		break;
	case "shareshow":
		slShowHide(document.getElementById("slpane"), false, false);
		break;
	case "sharehide":
		slShowHide(document.getElementById("slpane"), true);
		break;
	case "position":
		var p = document.getElementById("slpane");
		p.style.left = value[0] + "px";
		p.style.top = value[1] + "px";
		break;
	case "hide":
		try
		{
			sl.content.Page.stop();
		}
		catch(e){}
		slShowHide(document.getElementById("slpane"), false, true);
		break;
	case "updateprevnext":
		sl.content.Page.updatePrevNext(value[0], value[1]);
		break;
	}
}
function slCallback(action, value)
{
	var obj = document.getElementById("jtvshlembed");
	if(obj!=null && obj.slCallback!=null)
	{
		if(action=="shareshow")
			slShowHide(document.getElementById("slpane"), false, false);
		obj.slCallback(action, value);
	}
}
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);
}
var iptvCompanionDuration = 0;
function writeFlexPlayer(id, oemId, isProduction, tremorProgId, adSportId, companionDuration, googleAccount, locServer, locImage)
{
	// companionDuration of -1 means companion stays out until user closes it
	// companionDuration of 0 means companion closes with pre-roll completion
	// companionDuration of >0 specifies the number of seconds the companion will auto-close
	if(companionDuration!=null && !isNaN(companionDuration))
		iptvCompanionDuration = companionDuration;
	
	var serverStr = "server=" + location.href.substring(0, location.href.lastIndexOf("/")+1);
	var consoleStr = "embed";
	
	if(isProduction)
	{
		if(oemId!=null)
			serverStr = "server=" + locServer + "XML/titanv3/";
		else
			serverStr = "server=" + locServer;
		consoleStr = locImage + consoleStr;
	}
	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", "http://smb.cdn.neulion.com/u/jtvshl/player/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;
		if(oemId!=null)
			flashVars[flashVars.length] = "jtv=" + oemId;
		if(id!=null)
			flashVars[flashVars.length] = "id="+id;
		flashVars[flashVars.length] = "tremorprogid="+tremorProgId;
		if(adSportId!=null)
			flashVars[flashVars.length] = "adsportid="+adSportId;
		if(googleAccount!=null)
			flashVars[flashVars.length] = "gaa="+googleAccount;
		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", "jtvshlembed",
				"quality", "high",
				"bgcolor", "#000000",
				"name", "jtvshlembed",
				"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 jtvshlDbgMessage(strMessage)
{
	if (jtvshl_debug)
	{
		if (jtvshl_dbgDiv==null)
		{
			jtvshl_dbgDiv=document.createElement("div");
			jtvshl_dbgDiv.id="jtvshl_dbgDiv";
			jtvshl_dbgDiv.style.fontFamily="arial";
			jtvshl_dbgDiv.style.fontSize="8pt";
			jtvshl_dbgDiv.style.backgroundColor="#D0D0D0";
			jtvshl_dbgDiv.style.border="1px solid gray";
			jtvshl_dbgDiv.innerHTML="DEBUG";
			jtvshl_dbgDiv.style.position="absolute";
			jtvshl_dbgDiv.style.left="5px";
			jtvshl_dbgDiv.style.top="5px";
			jtvshl_dbgDiv.style.padding="5px";
			document.body.appendChild(jtvshl_dbgDiv);

			var adContainer=document.getElementById("jtvshlAdContainer");
			if (adContainer!=null)
				adContainer.style.border="2px solid red";
		}

		jtvshl_dbgDiv.innerHTML += " | " + strMessage;
	}
}

function iptvShowCompanions(playerX, playerY, companions)
{
	for(var i=0;i<companions.length;i++)
	{
		var adsize = companions[i][0];
		switch(adsize)
		{
		case "300x250":
			jtvshlShowFirstCompanion(playerX, playerY, companions[i]);
			if(iptvCompanionDuration>0)
				setTimeout("iptvHideCompanions(true)", iptvCompanionDuration*1000);
			break;
		}
	}
}

function iptvHideCompanions(hide)
{
	if(iptvCompanionDuration==0 || hide==true)
	{
		var tbl = document.getElementById("jtvshlAdTbl");
		if (tbl != null)
		{
			if (!g_isIE && document.getElementById("jtvshlIFrame") != null)
				document.getElementById("jtvshlIFrame").style.display="none";
	
			document.getElementById("jtvshlAdContainer").style.display="";
			if (jtvshl_direction=="right")
				tbl.setAttribute("anim_destX", (-1 * tbl.offsetWidth));
			else
				tbl.setAttribute("anim_destY", (-1 * tbl.offsetHeight));

			jtvshlStartAnimation(true);
		}
	}
}

/*function iptvPrerollComplete()
{
	alert("PREROLL COMPLETE");
}*/

function jtvshlShowFirstCompanion(playerX, playerY, companion)
{
	var adContainer = document.getElementById("jtvshlAdContainer");
	if (adContainer == null)
	{
		adContainer=document.createElement("div");
		adContainer.id="jtvshlAdContainer"
		adContainer.style.width="320px";
		adContainer.style.height="290px";
		adContainer.style.position="absolute";
		adContainer.style.overflow="hidden";
		document.body.appendChild(adContainer);

		var tbl = document.createElement("table");
		tbl.style.backgroundColor="#666666";
		tbl.id = "jtvshlAdTbl";
		tbl.cellPadding = "0";
		tbl.cellSpacing = "0";
		tbl.style.position = "absolute";
		tbl.style.top = "0px";
		tbl.style.width="320px";
		tbl.style.height="290px";
		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:iptvHideCompanions(true)'>CLOSE</a>";

		var oRow2=tbl.insertRow(-1);
		oRow2.style.height="100%";
		var oCell2 = oRow2.appendChild(document.createElement("td"));
		oCell2.style.padding="5px";
		oCell2.vAlign = "top";

		var oDiv = oCell2.appendChild(document.createElement("div"));
		oDiv.id = "jtvshlAdDiv";
		oDiv.style.width = "300px";
		oDiv.style.height = "250px";

		adContainer.appendChild(tbl);
	}

	var obj = document.getElementById("jtvshlembed");
	if(obj)
	{
		obj = obj.parentNode;
		// Position ad container
		var x_pos=0;
		var y_pos=0;

		var temp = obj;
		do
		{
			x_pos += temp.offsetLeft;
			y_pos += temp.offsetTop;
			temp = temp.offsetParent;
		}
		while(temp!=null)

		if (jtvshl_direction=="right")
		{
			adContainer.style.left = (x_pos + obj.offsetWidth) + "px";
			adContainer.style.top = y_pos + "px";
		}
		else
		{
			adContainer.style.left = x_pos + "px";
			adContainer.style.top = (y_pos + obj.offsetHeight) + "px";
		}


		// Set HTML for ad
		var strAdHTML = "";
		
		if("iframe"==companion[1] && companion[2]!=null)
		{
			strAdHTML = "<iframe id='jtvshlIFrame' width='300' height='250' frameborder='0' scrolling='no' src='"+companion[2]+"'></iframe>";
		}
		else if("swf"==companion[1] && companion[2]!=null)
		{
			strAdHTML = "<object width='300' height='250' id='jtvshlSWF' classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000'><param name='movie' value='"+companion[2]+"' /><embed name='jtvshlSWF' pluginspage='http://www.adobe.com/go/getflashplayer' src='"+companion[2]+"' type='application/x-shockwave-flash' width='300' height='250'></embed></object>";
		}
		else if("img"==companion[1] && companion[2]!=null)
		{
		
			if (companion[3] !=null && companion[3].length > 0)
				strAdHTML = "<a href='"+companion[3]+"' target='_blank'>";
			
			strAdHTML += "<img src='"+companion[2]+"' border='0' />";

			if (companion[3] !=null && companion[3].length > 0)
				strAdHTML += "</a>";
		}

		if (!g_isIE)
			document.getElementById("jtvshlAdDiv").style.display="none";
			
		document.getElementById("jtvshlAdDiv").innerHTML = strAdHTML;

		// Animate ad table within ad box
		var tbl = document.getElementById("jtvshlAdTbl");
		
		if (jtvshl_direction=="right")
		{
			document.getElementById("jtvshlAdContainer").style.display="";
			tbl.style.left = (-1 * tbl.offsetWidth) + "px";
			jtvshlDbgMessage("Setting Left:" + tbl.style.left);
			tbl.setAttribute("anim_destX", 0);
		}
		else
		{
			document.getElementById("jtvshlAdContainer").style.display="";
			tbl.style.top = (-1 * tbl.offsetHeight) + "px";
			jtvshlDbgMessage("Setting Top:" + tbl.style.top);
			tbl.setAttribute("anim_destY", 0);
		}

		jtvshlStartAnimation(false);
	}

}
function jtvshlStartAnimation(bMoveIn)
{
	jtvshlDbgMessage("startAnimation: "+bMoveIn);
	document.getElementById("jtvshlAdContainer").style.display="";
	jtvshlAnimateItem(bMoveIn);
}

function jtvshlAnimateItem(bMoveIn)
{
	var obj=document.getElementById("jtvshlAdTbl");

	var currLeft = obj.offsetLeft;
	var currTop = obj.offsetTop;

	if (jtvshl_direction=="right")
	{
		var iTargetPosX = obj.getAttribute("anim_destX");
		if (iTargetPosX != null)
		{
			jtvshlDbgMessage(currLeft);
			if ((bMoveIn && (currLeft - jtvshl_aniSpeed <= iTargetPosX)) || (!bMoveIn && (currLeft + jtvshl_aniSpeed >= iTargetPosX)))
			{
				jtvshlDbgMessage("MoveIn: "+bMoveIn);
				jtvshlDbgMessage("CurrLeft: "+currLeft);
				jtvshlDbgMessage("Speed: "+jtvshl_aniSpeed);
				jtvshlDbgMessage("TargetXPos: "+iTargetPosX);

				obj.style.left = iTargetPosX + "px";  // Stop animating

				document.getElementById("jtvshlAdDiv").style.display="";
				if (bMoveIn)
					document.getElementById("jtvshlAdContainer").style.display="none";
			}
			else
			{
				if (bMoveIn)
				{
					if (document.getElementById("jtvshlAdContainer").style.display=="none")
						return;
					obj.style.left = currLeft - jtvshl_aniSpeed + "px";
					setTimeout("jtvshlAnimateItem(true)", 1);
				}
				else
				{
					obj.style.left = currLeft + jtvshl_aniSpeed + "px";
					setTimeout("jtvshlAnimateItem(false)", 1);
				}
			}
		}
	}
	else
	{
		var iTargetPosY = obj.getAttribute("anim_destY");
		if (iTargetPosY != null)
		{
			jtvshlDbgMessage(currTop);
			if ((bMoveIn && (currTop - jtvshl_aniSpeed <= iTargetPosY)) || (!bMoveIn && (currTop + jtvshl_aniSpeed >= iTargetPosY)))
			{
				jtvshlDbgMessage("MoveIn: "+bMoveIn);
				jtvshlDbgMessage("CurrTop: "+currTop);
				jtvshlDbgMessage("Speed: "+jtvshl_aniSpeed);
				jtvshlDbgMessage("TargetYPos: "+iTargetPosY);

				obj.style.top = iTargetPosY + "px";  // Stop animating
				
				document.getElementById("jtvshlAdDiv").style.display="";
				if (bMoveIn)
					document.getElementById("jtvshlAdContainer").style.display="none";
			}
			else
			{
				if (bMoveIn)
				{
					obj.style.top = currTop - jtvshl_aniSpeed + "px";
					setTimeout("jtvshlAnimateItem(true)", 1);
				}
				else
				{
					obj.style.top = currTop + jtvshl_aniSpeed + "px";
					setTimeout("jtvshlAnimateItem(false)", 1);
				}
			}
		}
	}
}
