//constants updated

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'] = 494;
windowDim['100'] = new Array();
windowDim['100']['w'] = 410;
windowDim['100']['h'] = 380;
windowDim['200'] = new Array();
windowDim['200']['w'] = 610;
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

/* addSearchQueryParams(uri)
 *   Takes a URI string and adds the search query parameters to it. The
 *   search query parameters are extracted from the current location.search
 *   property and filtered to remove non-search parameters, e.g. rhs. This
 *   function is used to add the search parameters to the rhs URL in order
 *   to support the "Back to search results" link.
 */
function addSearchQueryParams(uri)
{
        // filter non-search parameters from query
        var sourceQueryParams = location.search.substring(1).split('&');
        var searchQueryParams = new Array();
        var j = 0;
        for(i in sourceQueryParams) {
              if(sourceQueryParams[i].substr(0,4) == "rhs=") continue;

              searchQueryParams[j++] = sourceQueryParams[i];
        }

        // add ? or & to uri if needed
        if(uri.indexOf("?") == -1) {
              uri += "?";
        } else {
             var lastChar = uri.charAt(uri.length-1);
             if(lastChar != "&" && lastChar != "?") uri += "&";
        }

        // now add query params
        uri += searchQueryParams.join("&");

        return uri;
}
  
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.location = banner;
	}
	else if (url.banner)
	{
		window.parent.topframe.location = url.banner;
	}
	
	if (clipurl)
	{
	window.parent.videoframe.location = "/sol/ukfs_sport/hi/av/player/player.stm?"+"title="+title+"&clipurl="+hackClipUrl(clipurl)+"&cs="+cs;
	}
	
	if (promo)
	{
		window.parent.footerframe.location = promo;
	}
	
	if (wywindex)
	{
		window.parent.lhsframe.location = wywindex;
	}
}

function fsOnLoad()
{
	resize(windowDim['normal']['w'],windowDim['normal']['h'],'');	
	
	getRHSFrameUrl();
	
	if (url.clipurl)
	{
	videoframeurl = "/sol/ukfs_sport/hi/av/player/player.stm?"+"title="+url.title+"&clipurl="+hackClipUrl(url.clipurl)+"&clippos="+url.clippos+"&cs="+url.cs+"&bw="+url.bw;
	}
}

function getRHSFrameUrl()
{
	var base = "/sol/ukfs_sport/hi/av/";
	
	if (url.rhs)
	{
	rhsframeurl = addSearchQueryParams(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())
	{
		window.parent.location.href = "http://www.bbc.co.uk/mediaselector/check/sol/ukfs_sport/hi/av?redirect=fs_fp.stm&prefs=1&nbram=1&bbram=1&nbwm=1&bbwm=1&news=1";
	}
}

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")
	{
		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 (navUrl.match("/hi/av/front_page/"))
	{
		window.parent.location.href = "http://www.bbc.co.uk/mediaselector/check/sol/ukfs_sport/hi/av?redirect=fs_fp.stm&bbram=1&bbwm=1&news=1&nbram=1&nbwm=1";
	}
	else if ((framename == "bb_rm_fs_fp") || (framename == "bb_wm_fs_fp") || (framename == "nb_rm_fs_fp") || (framename == "nb_wm_fs_fp"))
	{
		window.parent.location.href= "http://news.bbc.co.uk/sol/ukfs_sport/hi/av/"+window.parent.prefix+"_fs.stm?rhs=http://news.bbc.co.uk"+navUrl;
	}
	else
	{
		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 isStandAloneStory() {			
	return false;
}
