var playerType = "real";
var scripting = false;
var baseUrl = "http://dynamic.rte.ie/av/";
var playerHeight = 288;
var playerWidth = 512;
var hlElement = null;
var firstClipId = null;
var defaultProfileR = null;
var defaultProfileW = null;

function buildPlayer()
{
	//check cookie
	if(!playerType)
		playerType = getCookie("player");

	scripting = isScriptingSupported(playerType);

	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);
	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 setTmpPlayer(value)
{
	if(value == "real")
	{
		scripting = BrowserDetect.browser == "Explorer";
		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 initHl(id, profile1, profile2, url)
{
	var str = document.location.search;
	
	var a = str.split(",");
	if(a.length >= 3)
	{
		firstClipId = a[0].substr(1);
		defaultProfileW = a[1];		
		defaultProfileR = a[2];		
	}

	if(a.length == 4 && a[3] != '')
	{
		url = a[3];
	}
	
	if(firstClipId == null || firstClipId == "") 
	{
		firstClipId = id;
		defaultProfileW = profile1;		
		defaultProfileR = profile2;		
	}
	
	if(firstClipId == null && url == null)
	{
		return;
	}

	var el = document.getElementById("clip" + firstClipId);

	if(el != null && typeof(el) == "object")
	{

		if (el.nodeName == "UL")
		{
			el.className = "clipList sel";
		}
		else 
		{
			el.className = "sel";
		}
		hlElement = el;
		
		hlElement.scrollIntoView(true); 
	}

	//now lets try to play clip
	playClip(firstClipId, defaultProfileW, defaultProfileR, url); 	
}

function playClip(id, profileWin, profileReal, altUrl)
{
	if(id == null && altUrl == null)
	{
		return;
	}
	
	highlightItem("clip" + id);
	
	if(!playerType)
		playerType = getCookie("player");
	
	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(scripting && document.getElementById("playerObj"))
	{
		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 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()
{
	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(); 
}

