var playerType = "real";
var baseUrl = "http://dynamic.rte.ie/av/";
var playerHeight = 288;
var playerWidth = 512;

var videoHeight = 180;
var videoWidth = 320;


var hlElement = null;

var clipId = null;
var profileR = null;
var profileW = null;
var clipUrl = null;

var adPath = null;
var isPlayingAd = false;
var isLive = false;
var playCtrl = 0;
var playCtrlTimes = 1;
var enableAds = false;

var explicit = false;

function buildPlayer()
{
	//check cookie
	if(!playerType)
	{
		playerType = getCookie("player");
	}

	if(playerType == "wmp")
	{
		if(BrowserDetect.browser == "Explorer")
			document.write(getWMPForWinString(playerWidth, playerHeight, ""));
		else if(BrowserDetect.browser == "Firefox")
			document.write(getWMPForFFString(playerWidth, playerHeight,  ""));
	}
	else if (playerType == "real")
	{
		if(BrowserDetect.browser == "Explorer")	
			document.write(getRealPlayerForIEString(playerWidth, playerHeight,  ""));
		else
			document.write(getRealPlayerString(playerWidth, playerHeight,  ""));
	}
	else
	{
		document.write(getSelectPlayerString());
	}
}

function setPlayer(value)
{
	playerType = value;
	setCookie("player", playerType);
	
	playClip(clipId, profileW, profileR, clipUrl); 	
}

function initHl(id, profile1, profile2, url)
{
	var str = unescape(document.location.search);
	var a = str.split(",");

	if(a.length >= 3)
	{
		clipId = a[0].substr(1);
		profileW = (a[1] == "" || a[1] == "null") ? null: a[1];		
		profileR = (a[2] == "" || a[2] == "null") ? null: a[2];	;		
	}

	if(a.length == 4 && a[3] != '')
		url = a[3];
	
	if(clipId == null || clipId == "") 
	{
		clipId = id;
		profileW = profile1;		
		profileR = profile2;
	}
	
	if(clipId == null && url == null)
		return;

	clipUrl = url;

	highlightItem("clip" + id);

	//now lets try to play clip
	playClip(clipId, profileW, profileR, clipUrl); 	
}

function playClip(id, pWin, pReal, altUrl)
{
	if(!document.getElementById("player"))
	{
		return;
	}

	
	if(id == null && altUrl == null)
		return;
	
	highlightItem("clip" + id);

	if(explicit)
	{
		playExplicitClip(id, pWin, pReal, altUrl);
		return;
	}

	clipId = id;
	clipUrl = altUrl;
	profileW = pWin; 
	profileR = pReal;
	
	if(enableAds && adPath && playCtrl <= 0 && profileR != "209"  )
	{
		isLive = false;
		showAd();
		return;
	}
	else
	{
		playCtrl--;	
	}

	if(pReal && PluginDetect.test("Real"))
	       	playerType = "real";
	else if(pWin && PluginDetect.test("Windows Media"))
		playerType = "wmp";
	else
	{
		if(pReal)
		{
			document.getElementById("player").innerHTML = getRealNotAvailableString();
			return;
		}		
		else if(pWin)
		{
			document.getElementById("player").innerHTML = getWMPNotAvailableString();
			return;
		}
		else
		{
		    alert("Can't play requested clip!");
		    return;
		}
	}
	
	//set player
	if(playerType == "wmp" && isScriptingSupported(playerType) && document.getElementById("playerObj") && document.getElementById("playerObj").url)
	{
		document.getElementById("playerObj").url = buildClipUrl(altUrl != null ? altUrl : id, pWin, "asx", baseUrl);

		if(document.getElementById("clipExtraInfo"))
			document.getElementById("clipExtraInfo").innerHTML = "<a href=\"" + buildClipUrl(altUrl != null ? altUrl : id, pWin, "asx", baseUrl) + "\">Play clip in Windows Media Player</a>";
			
	}
	else if(playerType == "real" && isScriptingSupported(playerType) && document.getElementById("playerObj") && !document.getElementById("playerObj").url)
	{
		document.getElementById("playerObj").SetSource(buildClipUrl(altUrl != null ? altUrl : id, pReal, "smil", baseUrl));

		if(document.getElementById("clipExtraInfo"))
			document.getElementById("clipExtraInfo").innerHTML = "<a href=\"" + buildClipUrl(altUrl != null ? altUrl : id, pReal, "smil", baseUrl) + "\">Play clip in Real Player</a>";

	}
	else
	{
		document.getElementById("player").style.visibility = "hidden";
		
		
		if(playerType == "real")
		{
			if(BrowserDetect.browser == "Explorer")
			{
				document.getElementById("player").innerHTML = getRealPlayerForIEString(playerWidth, playerHeight, "");
				document.getElementById("playerObj").SetSource(buildClipUrl(altUrl != null ? altUrl : id, pReal, "smil", baseUrl));
			}
			else
				document.getElementById("player").innerHTML = getRealPlayerString(playerWidth, playerHeight, buildClipUrl(altUrl != null ? altUrl : id, pReal, "smil", baseUrl));

			if(document.getElementById("clipExtraInfo"))
				document.getElementById("clipExtraInfo").innerHTML = "<a href=\"" + buildClipUrl(altUrl != null ? altUrl : id, pReal, "smil", baseUrl) + "\">Play clip in Real Player</a>";

		}
		else if(BrowserDetect.browser == "Firefox")
		{
			document.getElementById("player").innerHTML = getWMPForFFString(playerWidth, playerHeight, buildClipUrl(altUrl != null ? altUrl : id, pWin, "asx", baseUrl));

			if(document.getElementById("clipExtraInfo"))
				document.getElementById("clipExtraInfo").innerHTML = "<a href=\"" + buildClipUrl(altUrl != null ? altUrl : id, pWin, "asx", baseUrl) + "\">Play clip in Windows Media Player</a>";

		}
		else if(BrowserDetect.browser == "Netscape" && BrowserDetect.OS == "Windows")
		{
			document.getElementById("player").innerHTML = getWMPForWinString(playerWidth, playerHeight,  buildClipUrl(altUrl != null ? altUrl : id, pWin, "asx", baseUrl));

			if(document.getElementById("clipExtraInfo"))
				document.getElementById("clipExtraInfo").innerHTML = "<a href=\"" + buildClipUrl(altUrl != null ? altUrl : id, pWin, "asx", baseUrl) + "\">Play clip in Windows Media Player</a>";
				
		}
		else
		{
			document.getElementById("player").innerHTML = getWMPForWinString(playerWidth, playerHeight,  buildClipUrl(altUrl != null ? altUrl : id, pWin, "asx", baseUrl));

			if(document.getElementById("clipExtraInfo"))
				document.getElementById("clipExtraInfo").innerHTML = "<a href=\"" + buildClipUrl(altUrl != null ? altUrl : id, pWin, "asx", baseUrl) + "\">Play clip in Windows Media Player</a>";
				
		}
		
		window.setInterval("document.getElementById(\"player\").style.visibility = \"visible\";", 100);
	}
}

function playLiveClip(id, pWin, pReal, altUrl)
{
	playClip(id, pWin, pReal, altUrl);
}

function playClipWithNoAd(id, pWin, pReal, altUrl)
{
	var _ads = enableAds;
	enableAds = false;
	playClip(id, pWin, pReal, altUrl);
	enableAds = _ads;
}

function playExplicitClip(id, pWin, pReal, altUrl) 
{
	document.getElementById("player").innerHTML = "<div class=\"warning\"><p>Please note the following programme is <strong>Suggested for mature audience (MA)</strong> and may feature profane dialogue or material of an explicit nature.</p><p><span class=\"warningOptions\">Are you over 18?<br /><img src=\"/images/player_yes.gif\" onclick=\"explicit=false;playClip("+id+","+pWin+","+pReal+","+altUrl+")\"  style=\"cursor:pointer;\" /> <img src=\"/images/player_no.gif\" onclick=\"tooYoungMessage()\" style=\"cursor:pointer;\" /></span></p></div>";
}

function tooYoungMessage() 
{
	document.getElementById("player").innerHTML = "<div class=\"warning\"><p><span class=\"warningOptions\">You must be over 18 to watch this clip.</span></p></div>";
}

function buildClipUrl(id, profile, fmt, baseUrl)
{
	if(!isNaN(parseInt(id)))
	{
		if(baseUrl && profile && fmt)
			return baseUrl + profile + "-" + id + "-" + videoWidth + "-" + videoHeight + "." + fmt
		else
			return "";
	}
	else
	{
		if(id != null && (id.indexOf("http://") > -1 || id.indexOf("rstp://") > -1 || id.indexOf("mms://") > -1))
			return id;
		else
			return "";
	}
}

function highlightItem(itemId)
{
	if(hlElement)
	{
		if (hlElement.nodeName == "UL")
			hlElement.className = "clipList";
		else 
			hlElement.className = "";
	}
	
	var el = document.getElementById(itemId);
	if(el != null && typeof(el) == "object")
	{
		if (el.nodeName == "UL")
			el.className = "clipList sel";
		else 
			el.className = "sel";
		
		hlElement = el;
		
		
		if(el.scrollIntoView)
		{
			var _el = document.getElementById("playerContent").getElementsByTagName("DIV");
			if(_el && _el[0] && (_el[0].offsetHeight < _el[0].scrollHeight))
				el.scrollIntoView(true);
		}
		
	}
}

//cookie
function setCookie(sName, sValue)
{
   var date = new Date();
   date.setTime(date.getTime() + 31536000000); 
   document.cookie = sName + "=" + escape(sValue) + ";expires=" + date.toUTCString() + ";path=/";
}

function getCookie(sName)
{
  // cookies are separated by semicolons
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    // a name/value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) 
      return unescape(aCrumb[1]);
  }

  // a cookie with the requested name does not exist
  return null;
}

function getSelectPlayerString()
{
	if(document.getElementById("clipExtraInfo"))
		document.getElementById("clipExtraInfo").innerHTML = "";

	return "<div id=\"playerSettings\">" +
	"<p>To watch RTÉ videos you must have Real Player installed on your computer. If you already have Real Player installed check the button below, otherwise you can download it by following the link. We&#39;ll remember your settings for future visits.</p>" +
	"<p><input type=\"radio\" name=\"ps\" value=\"real\" onclick=\"setPlayer('real')\" /> Real Media Player" +
	"&nbsp;|&nbsp;<a href=\"http://www.real.com/freeplayer/?rppr=rnwk\" target=\"_blank\">Download Free Player</a>" +
	"</div>"; 

/*	return "<div id=\"playerSettings\">" +
	"<p>To watch RTÉ videos, first choose a media player. We&#39;ll remember your settings for future visits. You can always change to a 	different player by clicking on &#39;Player settings&#39;. </p>" +
	"<p><input type=\"radio\" name=\"ps\" value=\"wmp\" onclick=\"setPlayer('wmp')\" /> Windows Media Player" +
	"&nbsp;|&nbsp;<a href=\"xxx\">Download Free Player</a>" +
	"<p><input type=\"radio\" name=\"ps\" value=\"real\" onclick=\"setPlayer('real')\" /> Real Media Player" +
	"&nbsp;|&nbsp;<a href=\"xxx\">Download Free Player</a>" +
	"</div>";
*/
}

function getWMPNotAvailableString()
{
	if(document.getElementById("clipExtraInfo"))
		document.getElementById("clipExtraInfo").innerHTML = "";

	return "<div id=\"playerSettings\">" +
	"<p>To watch this clip you must have Windows Media Player installed on your computer.</p>" +
	"<p>You can download it by following the link: <a href=\"http://www.microsoft.com/windows/windowsmedia/download/AllDownloads.aspx?displang=en&amp;qstechnology=\" target=\"_blank\">Windows Media Player</a></p>" +
	"</div>"; 
}

function getRealNotAvailableString()
{
	if(document.getElementById("clipExtraInfo"))
		document.getElementById("clipExtraInfo").innerHTML = "";

	return "<div id=\"playerSettings\">" +
	"<p>To watch this clip you must have Real Player installed on your computer.</p>" +
	"<p>You can download it by following the link: <a href=\"http://www.real.com/freeplayer/?rppr=rnwk\" target=\"_blank\">Free Real Player</a></p>" +
	"</div>"; 
}

function changeSettings()
{
	//document.getElementById("player").innerHTML = getSelectPlayerString(); 
	showHelp();
}

function showHelp()
{
	if(document.getElementById("player"))
	{
		document.getElementById("player").innerHTML = "<iframe src=\"/info/avfaq_simple.html\" name=\"adPanel\" id=\"adPanel\" width=\"512\" height=\"334\" scrolling=\"yes\" marginheight=\"0\" marginwidth=\"0\" frameborder=\"0\" status=\"no\" statusbar=\"no\" allowTransparency=\"true\"></iframe>";
	
		if(document.getElementById("clipExtraInfo"))
			document.getElementById("clipExtraInfo").innerHTML = "";
	}

}

function showAd()
{
	if(adPath && document.getElementById("player"))
	{
		document.getElementById("player").style.display = "none";

		document.getElementById("player").innerHTML = "<iframe src=\"" + adPath + "\" name=\"adPanel\" id=\"adPanel\" width=\"512\" height=\"288\" scrolling=\"no\" marginheight=\"0\" marginwidth=\"0\" frameborder=\"0\" status=\"no\" statusbar=\"no\" allowTransparency=\"true\"></iframe>" +
							"<div id=\"adMessage\"><p>Audio / Video will start after this short advertisement...</p></div>";

		document.getElementById("player").style.display = "block";

		if(document.getElementById("clipExtraInfo"))
			document.getElementById("clipExtraInfo").innerHTML = "";
		
		isPlayingAd = true;
	}
	
	//comment to make unbreakable ad
	playCtrl = playCtrlTimes;
}

function afterAd()
{
	isPlayingAd = false;
	
	//uncomment to make unbreakable ad
	//playCtrl = playCtrlTimes;

	playClip(clipId, profileW, profileR, clipUrl);
}


function attachEvents(objName)
{
//	x =	"Sub " + objName + "_OnPresentationClosed():playNext:End Sub";
//	window.execScript(x,"VBScript");
}

/*
function playNext()
{
	var el = document.getElementById("clip" + clipId);
	if(el.tagName == "LI")
	{
		var sb = el.nextSibling;
		
		if (sb)
		{
			at = sb.all.tags("A");

			if(typeof(at) == "object")
			{
				window.execScript(at[0].href);
			}
		}
	}
}
*/
