//constants

var consoleName = "console";

var playerDim = new Array();
playerDim['bb'] = new Array();
playerDim['bb']['w'] = 400; // broadband width
playerDim['bb']['h'] = 225; // broadband height
playerDim['nb'] = new Array();
playerDim['nb']['w'] = 256; // narrowband width
playerDim['nb']['h'] = 144; // narrowband height

var windowDim = new Array();
windowDim['normal'] = new Array();
windowDim['normal']['w'] = 681;
windowDim['normal']['h'] = 516;
windowDim['100'] = new Array();
windowDim['100']['w'] = 440;
windowDim['100']['h'] = 380;
windowDim['200'] = new Array();
windowDim['200']['w'] = 630;
windowDim['200']['h'] = 495;


var qs = location.search.substring(1);
  var nv = qs.split('&');
  var url = new Object();
  for(i = 0; i < nv.length; i++)
  {
    eq = nv[i].indexOf('=');
    url[nv[i].substring(0,eq).toLowerCase().replace("nol_","")] = unescape(nv[i].substring(eq + 1));
  }

if(url.clipurl)
{
	url.clipurl = unHackClipURL(url.clipurl);
}
  
//begin functions



function resize(w,h,parent)
{
	var targetwin;
	if (parent)
	{
		targetwin = window.parent;
	}
	else
	{
		targetwin = window;
	}

		targetwin.resizeTo(w+12,h+31);
}

function hackClipUrl(urlString)
{
	return urlString.replace(".asx","!asx");
}

function unHackClipURL(urlString)
{
	return urlString.replace("!asx",".asx");
}
  
function indexOnLoad()
{
	storyOnLoad();
}
  
function storyOnLoad()
{
	if (banner && window.parent.topframe)
	{                
  	  window.parent.topframe.location = banner;
	}
	else if(url.banner && window.parent.topframe)
	{
	  window.parent.topframe.location = url.banner;
	}
	
	if (clipurl)
	{
		if (window.parent.videoframe) window.parent.videoframe.location = "/nolavconsole/shared/player/player.stm?"+"title="+title+"&clipurl="+hackClipUrl(clipurl)+"&cs="+cs;
	}
	
	if (promo)
	{
		if (window.parent.footerframe) window.parent.footerframe.location = promo;
	}
	
	if (wywindex)
	{
		if (window.parent.lhsframe) window.parent.lhsframe.location = wywindex;
	}
}

function fsOnLoad()
{
	resize(windowDim['normal']['w'],windowDim['normal']['h'],'');
	
	getRHSFrameUrl();
	
	if (url.clipurl)
	{
	videoframeurl = "/nolavconsole/shared/player/player.stm?"+"title="+url.title+"&clipurl="+hackClipUrl(url.clipurl)+"&clippos="+url.clippos+"&cs="+url.cs+"&bw="+url.bw;
	}
}

function getRHSFrameUrl()
{
	var base = "/nolavconsole/"+edition+"_news/hi/";
	
	if (url.rhs)
	{
	rhsframeurl = url.rhs;//fm1
	}
	if (url.index)
	{
	rhsframeurl = base + url.index + "/" + prefix + "default.stm";
	}
	if (url.storyid)
	{
	rhsframeurl = base + "newsid_" + (Math.floor(url.storyid/10000)*10000) + "/newsid_" + (Math.floor(url.storyid/100)*100) + "/"+ prefix + url.storyid + ".stm";
	}
	
	return rhsframeurl;
}

function alertToChangePrefs()
{
	return confirm("You are about to change your viewing preferences.\n\nAfter you have made your changes any media currently playing will stop and you will return to the front page of the player.\n\nDo you wish to proceed?");
}

function resizeThenChangeMSPrefs()
{
	if (alertToChangePrefs())
	{
		resize(windowDim['normal']['w'],windowDim['normal']['h'],'');
		changeMSPrefs();
	}
}

function changeMSPrefs()
{
	if (alertToChangePrefs())
	{
		if (isStandAloneStory()) {
			window.parent.location.href = "http://www.bbc.co.uk/mediaselector/check/nolavconsole/" + parent.edition + "_news/hi?redirect=st.stm&prefs=1&news=1&bbram=1&bbwm=1&nbram=1&nbwm=1" + parent.mediaselectorurl;
		} else {
			window.parent.location.href = "http://www.bbc.co.uk/mediaselector/check/nolavconsole/"+parent.edition+"_news/hi?redirect=fs.stm&prefs=1&nbram=1&nbwm=1"+parent.msbbops+"&news=1" + parent.rhsframe.mediaselectorurl;
		}	
	}
}

function frameFileName(parent)
{
	if (parent)
	{
		targetwin = window.parent;
	}
	else
	{
		targetwin = window;
	}

	var u = targetwin.location.href;
	if (u.indexOf('?') != -1)
	{
		u = u.substr(0,u.indexOf('?'));
	}
	return u.substr((u.lastIndexOf('/')+1),u.length).replace('.stm','');
}

function navChange(navUrl,loadIntoSelf)
{
	framename = frameFileName(1);

	if (framename == "fs_wyw")
	{
		if (window.parent.videoframe) window.parent.videoframe.switchFSforNav(navUrl);
	}
	else if (loadIntoSelf)
	{
		window.parent.location.href = "http://www.bbc.co.uk/mediaselector/check/sol/ukfs_sport/hi/av?redirect=fs.stm&bbram=1&bbwm=1&news=1&nbram=1&nbwm=1&nol_rhs="+navUrl;
	}
	else
	{
		if (window.parent.rhsframe) window.parent.rhsframe.location.href=navUrl;
	}		
}

function makeFrame(name,url,scrolling)
{
	document.write('<frame id="'+name+'" name="'+name+'" src="'+url+'" scrolling="'+scrolling+'" marginwidth="0" marginheight="0" noresize frameborder="0"></frame>');
}

function setPos(time)
{
	if (window.parent.videoframe) window.parent.videoframe.setPos(time);
}

function iFrameHeight(iframename)
{	
	i = parent.document.getElementById(iframename);
	iHeight = document.body.scrollHeight;
	i.style.height = iHeight + 5 + "px";
}

function getRssUrl()
{ 
	if (document.getElementsByTagName)
	{
		var url = "http://news.bbc.co.uk/1/hi/help/3223484.stm";
		var linkTags = document.getElementsByTagName("link");
		var rssURI;
		for (var i = 0; i < linkTags.length; i++) {if (linkTags[i].getAttribute('title') == "rss")
			{rssURI = linkTags[i].getAttribute('href');}}
		if (rssURI)
			{document.write('<div class="feedslink"><a href="'+rssURI+'" target="_blank"><img height="16" hspace="0" vspace="0" border="0" width="16" alt="News feeds" src="http://newsimg.bbc.co.uk/shared/img/v3/feed.gif" title="News feeds" align="left" /></a> <span class="feedbar">&nbsp;|</span> <a href="'+url+'" title="News feeds help" target="_blank" class="story">News feeds</a></div>')}
	}
}

function isStandAloneStory() {			
	return (typeof(window.parent.videoframe)=="undefined");
}