var playerType = "real";
var baseUrl = "http://dynamic.rte.ie/av/";
var playerHeight = 0;
var playerWidth = 480;

var videoHeight = 0;
var videoWidth = 0;

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 = 2;
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);
	
	if(document.getElementById("playerMainFeatuers"))
		document.getElementById("playerMainFeatuers").style.display = "block";
	
	
	playClip(clipId, profileW, profileR, clipUrl); 	
}

function highlightItem(id)
{
	if(hlElement)
	{
		if (hlElement.nodeName == "UL")
		{
			hlElement.className = "clipList";
		}
		else 
		{
			hlElement.className = "";
		}
		
	}
	
	var el = document.getElementById(id);
	if(el != null && typeof(el) == "object")
	{
		if (el.nodeName == "UL")
		{
			el.className = "clipList sel";
		}
		else 
		{
			el.className = "sel";
		}
		
		hlElement = el; 
	}
}

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)
		{
			if(document.getElementById("playerMainFeatuers"))
				document.getElementById("playerMainFeatuers").style.display = "none";
		
			if(document.getElementById("clipExtraInfo"))
				document.getElementById("clipExtraInfo").innerHTML = "";
		
			document.getElementById("player").innerHTML = getRealNotAvailableString();
			return;
		}		
		else if(pWin)
		{
			if(document.getElementById("playerMainFeatuers"))
				document.getElementById("playerMainFeatuers").style.display = "none";
		
			if(document.getElementById("clipExtraInfo"))
				document.getElementById("clipExtraInfo").innerHTML = "";
		
			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") && typeof(document.getElementById("playerObj").url) == "undefined")
	{
		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) 
{
	if(document.getElementById("playerMainFeatuers"))
		document.getElementById("playerMainFeatuers").style.display = "none";

	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() 
{
	if(document.getElementById("playerMainFeatuers"))
		document.getElementById("playerMainFeatuers").style.display = "none";
	
	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 "";
	}
}

//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()
{
	return "<div id=\"playerSettings\">" +
	"<p>To listen to RTÉ audio files 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 listen to RTÉ audio files, 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()
{
	return "<div id=\"playerSettings\">" +
	"<p>To listen to this audio 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()
{
	return "<div id=\"playerSettings\">" +
	"<p>To listen to this audio 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()
{
	if(document.getElementById("playerMainFeatuers"))
		document.getElementById("playerMainFeatuers").style.display = "none";

	document.getElementById("player").innerHTML = getSelectPlayerString(); 
}

