var playerType = "real";
var scripting = false;
var baseUrl = "http://dynamic.rte.ie/av/";
var playerHeight = 192;
var playerWidth = 320;
var hlElement = null;
var firstClipId = null;
var defaultProfileR = null;
var defaultProfileW = null;

function setPlayer(value)
{
	if(!playerType)
		playerType = getCookie("player");

	scripting = isScriptingSupported(playerType);
	
	if(firstClipId != null && playerType == "wmp" && defaultProfileW == null)
	{
		document.getElementById("player").innerHTML = getWMPNotAvailableString();
		return;
	}

	if(firstClipId != null && playerType == "real" && defaultProfileR == null)
	{
		document.getElementById("player").innerHTML = getRealNotAvailableString();
		return;
	}
	

	if(playerType == "real")
	{
		if(BrowserDetect.browser == "Explorer")
			document.getElementById("player").innerHTML = getRealPlayerForIEString(playerWidth, playerHeight, buildClipUrl(firstClipId, defaultProfileR, "smil", baseUrl));
		else
			document.getElementById("player").innerHTML = getRealPlayerString(playerWidth, playerHeight, buildClipUrl(firstClipId, defaultProfileR, "smil", baseUrl));
	}
	else if(BrowserDetect.browser == "Explorer")
	{
		scripting = true;
		document.getElementById("player").innerHTML = getWMPForWinString(playerWidth, playerHeight, buildClipUrl(firstClipId, defaultProfileW, "asx", baseUrl));
	}
	else if(BrowserDetect.browser == "Firefox")
	{
		scripting = false;
		document.getElementById("player").innerHTML = getWMPForFFString(playerWidth, playerHeight, buildClipUrl(firstClipId, defaultProfileW, "asx", baseUrl));
	}
	else if(BrowserDetect.browser == "Netscape" && BrowserDetect.OS == "Windows")
	{
		scripting = true;
		document.getElementById("player").innerHTML = getWMPForWinString(playerWidth, playerHeight,  buildClipUrl(firstClipId, defaultProfileW, "asx", baseUrl));
	}
}


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 playClip(id, profileWin, profileReal, altUrl)
{
	if(id == null && altUrl == null)
	{
		return;
	}
	
	highlightItem("clip" + id);
	

	//get cookie
	if(!playerType)
		playerType = getCookie("player");


	//get scripting support
	scripting = isScriptingSupported(playerType);
	
	if(altUrl != null)
	{
		id = altUrl;
	}
	
	
	if(playerType == "wmp" && isNaN(parseInt(profileWin)))
	{
		firstClipId = id;
		document.getElementById("player").innerHTML = getWMPNotAvailableString();
		return;
	}

	if(playerType == "real" && isNaN(parseInt(profileReal)))
	{
		firstClipId = id;
		document.getElementById("player").innerHTML = getRealNotAvailableString();
		return;
	}
	
	//set player
	if(document.getElementById("playerObj") && scripting)
	{
		if(playerType == "wmp")
		{
			document.getElementById("playerObj").url = buildClipUrl(id, profileWin, "asx", baseUrl);
		}
		else if (playerType == "real")
		{
			document.getElementById("playerObj").SetSource(buildClipUrl(id, profileReal, "smil", baseUrl));
		}
	}
	else
	{
		if(playerType == "real")
		{
			if(BrowserDetect.browser == "Explorer")
				document.getElementById("player").innerHTML = getRealPlayerForIEString(playerWidth, playerHeight, buildClipUrl(id, profileReal, "smil", baseUrl));
			else
				document.getElementById("player").innerHTML = getRealPlayerString(playerWidth, playerHeight, buildClipUrl(id, profileReal, "smil", baseUrl));
		}
		else if(BrowserDetect.browser == "Firefox")
		{
			document.getElementById("player").innerHTML = getWMPForFFString(playerWidth, playerHeight, buildClipUrl(id, profileWin, "asx", baseUrl));
		}
		else if(BrowserDetect.browser == "Netscape" && BrowserDetect.OS == "Windows")
		{
			document.getElementById("player").innerHTML = getWMPForWinString(playerWidth, playerHeight,  buildClipUrl(id, profileWin, "asx", baseUrl));
		}
	}
}

function buildClipUrl(id, profile, fmt, baseUrl)
{
	if(!isNaN(parseInt(id)))
	{
		if(baseUrl && profile && fmt)
			return baseUrl + profile + "-" + id + "." + fmt
		else
			return "";
	}
	else
	{
		if(id != null && (id.indexOf("http://") > -1 || id.indexOf("rstp://") > -1 || id.indexOf("mms://") > -1))
		{
			return id;
		}
	}
}


//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 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()
{
	return "<div id=\"playerSettings\">" +
	"<p>The clip you've selected is only available in Real Media format. Would you like to play it in Real Player? You player preference wont be affected.</p>" +
	"<p><input type=\"button\" value=\"Yes, play it in Real Player\" onclick=\"setTmpPlayer('real')\"/></p>" +
	"<p><input type=\"button\" value=\"No, leave my existing settings\" onclick=\"setTmpPlayer('wmp')\" /></p>" +
	"</div>";
}

function getRealNotAvailableString()
{
	return "<div id=\"playerSettings\">" +
	"<p>The clip you've selected is only available in Windows Media format. Would you like to play it in Windows Media Player? You player preference won''t be affected.</p>" +
	"<p><input type=\"button\" value=\"Yes, play it in Windows Media Player\" onclick=\"setTmpPlayer('wmp')\"/></p>" +
	"<p><input type=\"button\" value=\"No, leave my existing settings\" onclick=\"setTmpPlayer('real')\" /></p>" +
	"</div>";
}

function changeSettings()
{
	document.getElementById("player").innerHTML = getSelectPlayerString(); 
}

