// -----------------------------------------------------------------------------
// Globals
// Major version of Flash required
var requiredMajorVersion = 9;
// Minor version of Flash required
var requiredMinorVersion = 0;
// Minor version of Flash required
var requiredRevision = 128;
// -----------------------------------------------------------------------------

// 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_isIE = (window.navigator.userAgent.indexOf("MSIE")>0);
function playWMV(path, x, y, volume, trackCG, trackTL)
{
	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 = 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.contentWindow.writeWMP(path,360,false,640,null,null,null,true,false);
		wmp = wmp.contentWindow.document.getElementById("wmpMain")
		/*var win = window.open("wmppop.htm","wmvplayer","scrollbars=yes,menubar=no,height=334,width=480,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 "ff":
		wmp.controls.fastForward();
		break;
	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 "rewind":
		wmp.controls.fastReverse();
		break;
	case "shareshow":
		if(g_isIE)
			document.getElementById("wmppane").style.display = "none";
		break;
	case "sharehide":
		if(g_isIE)
			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(isProduction, embed, inline)
{
	var serverStr = "server=" + location.href.substring(0, location.href.lastIndexOf("/")+1);
	var consoleStr = "console";
	var h = "1070";
	if(embed)
	{
		consoleStr = "embed";
		h = "100%";
	}
	else if(!isNaN(inline))
	{
		consoleStr = "inline"+inline;
		h = "100%";
	}
	if(isProduction)
	{
		//serverStr = "server=http://localhost/cycling/";
		//consoleStr = "http://nhl.cdn.neulion.net/u/cycling/console";
		//if(embed)
		//	consoleStr = "http://nhl.cdn.neulion.net/u/cycling/embed";
	}
	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] = "usesecure=true";
		flashVars[flashVars.length] = "tremorprogid=49c19441c8d74";
		flashVars[flashVars.length] = "nlwa=" + g_nlwa;
		if(window.location.search.length>1)
		{
			flashVars[flashVars.length] = "qs=" + encodeURIComponent(window.location.search.substring(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", h,
				"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 = 640;
		
	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";
	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;
}

function iptvShowCompanions(playerX, playerY, companions)
{
	for(var i=0;i<companions.length;i++)
	{
		var adsize = companions[i][0];
		switch(adsize)
		{
		case "728x90":
			iptvShowTopCompanion(playerX, playerY, companions[i]);
			break;
		case "300x250":
			iptvShowFirstCompanion(playerX, playerY, companions[i]);
			break;
		}
	}
}

function iptvHideCompanions(hide)
{
	iptvMoveAd(false);
}

function iptvShowTopCompanion(playerX, playerY, companion)
{
	var oDiv=document.getElementById("iptvTopAdDiv");
	if (oDiv==null)
	{
		oDiv = document.createElement("div");
		oDiv.id = "iptvTopAdDiv";
		oDiv.style.width = "728px";
		oDiv.style.height = "90px";
		document.getElementById("topBannerDiv").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 iptvShowFirstCompanion(playerX, playerY, companion)
{
	var obj = document.getElementById("console");
	if(obj)
	{
		var adContainer = document.getElementById("iptvAdContainer");
		if (adContainer == null)
		{
			adContainer=document.createElement("div");
			adContainer.id="iptvAdContainer"
			adContainer.style.width="300px";
			adContainer.style.height="250px";
			adContainer.style.position="absolute";
			adContainer.style.overflow="hidden";
			document.body.appendChild(adContainer);
			
			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)
				
			iptvAdBottom = (y_pos + playerY + 450);
			iptvAdTop = iptvAdBottom - 430;
			
			adContainer.style.left = (x_pos + playerX + 649) + "px";
			adContainer.style.top = iptvAdBottom + "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>";
		}

		adContainer.innerHTML = strAdHTML;

		iptvMoveAd(true);
	}
}

var iptvAdTop=0;
var iptvAdBottom=0;
var iptvAniSpeed = 8;   // Animation speed - bigger==faster
 
function iptvMoveAd(bUp)
{
	var tbl=document.getElementById("iptvAdContainer");
	if (bUp)
	{
		tbl.style.top = iptvAdBottom + "px";
		tbl.setAttribute("anim_destY", iptvAdTop);
	}
	else
	{
		tbl.style.top = iptvAdTop + "px";
		tbl.setAttribute("anim_destY", iptvAdBottom);
	}
	iptvAnimateItem(bUp);
}
  
function iptvAnimateItem(bMoveUp)
{
	var obj=document.getElementById("iptvAdContainer");
	var currTop = obj.offsetTop;
 
	var iTargetPosY = obj.getAttribute("anim_destY");
	if (iTargetPosY != null)
	{
		if ((bMoveUp && (currTop - iptvAniSpeed <= iTargetPosY)) || (!bMoveUp && (currTop + iptvAniSpeed >= iTargetPosY)))
		{
			obj.style.top = iTargetPosY + "px";  // Stop animating
		}
		else
		{
			if (bMoveUp)
			{
				obj.style.top = currTop - iptvAniSpeed + "px";
				setTimeout("iptvAnimateItem(true)", 1);
			}
			else
			{
				obj.style.top = currTop + iptvAniSpeed + "px";
				setTimeout("iptvAnimateItem(false)", 1);
			}
		}
	}
}
function iptvShowReg(param)
{
	var url = "secure/registerform";
	location.href = url;
}