﻿//constants

var consoleName = "console";
var lang = "fa";
var pathtovideplayer = "/persian/avconsole/player/";
var changeprefalert = "در اين مرحله شما می توانيد تنظيمات نحوه نمايش را تغيير دهيد.\n\nپس از آنکه تنظيمات را تغيير داديد، گزارش صوتی يا تصويری در حال نمايش، قطع خواهد شد و شما به صفحه اول باز می گرديد.\n\nآيا مايليد وارد اين مرحله شويد؟";
var mspathtoavconsole = "http://www.bbc.co.uk/mediaselector/check/persian/avconsole";

var playerDim = new Array();
playerDim['bb'] = new Array();
playerDim['bb']['w'] = 400; // broadband width
playerDim['bb']['h'] = 225; // broadband height
playerDim['bb']['rpw'] = 300; // broadband width
playerDim['bb']['rph'] = 225; // broadband height
playerDim['nb'] = new Array();
playerDim['nb']['w'] = 256; // narrowband width
playerDim['nb']['h'] = 144; // narrowband height
playerDim['nb']['rpw'] = 192; // narrowband width
playerDim['nb']['rph'] = 144; // narrowband height
playerDim['nb']['image'] = 'http://www.bbc.co.uk/persian/images/furniture/avconsole/16_9nb_persian_audio_brand.gif'
playerDim['bb']['image'] = 'http://www.bbc.co.uk/persian/images/furniture/avconsole/16_9bb_persian_audio_brand.gif'

var windowDim = new Array();
windowDim['normal'] = new Array();
windowDim['normal']['w'] = 681;
windowDim['normal']['h'] = 425;
windowDim['normal']['bbh'] = 520;
windowDim['100'] = new Array();
windowDim['100']['w'] = 450;
windowDim['100']['h'] = 320;
windowDim['100']['bbh'] = 425;
windowDim['200'] = new Array();
windowDim['200']['w'] = 620;
windowDim['200']['h'] = 390;
windowDim['200']['bbh'] = 520;


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("ws_","")] = 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;
	}
	
	var agent  = navigator.userAgent.toLowerCase();
  	var major  = parseInt(navigator.appVersion);
	var isIE     = ((agent.indexOf("msie")!= -1) &&
                (agent.indexOf("opera")== -1));
  	var ie7    = (isIE && (major == 4) &&  
  				(agent.indexOf("msie 7.0")    != -1));

	if(!ie7) {
		targetwin.resizeTo(w+12,h+31);
				} else {
				
		targetwin.resizeTo(w+12,h+51);		
				}

}

function hackClipUrl(urlString)
{
	return urlString.replace(".asx","!asx");
}

function unHackClipURL(urlString)
{
	return urlString.replace("!asx",".asx");
}
  
function indexOnLoad()
{
	storyOnLoad();
}
  
function storyOnLoad()
{
	// if the banner variable is specified (story or index page) and there is a frame called topframe then set the topframe to have the path banner specifies
	if (banner && window.parent.topframe)
	{                
  	  window.parent.topframe.location = banner;
	}
	// url banner is only specified when the story is standalone
	else if(url.banner && window.parent.topframe)
	{
	  window.parent.topframe.location = url.banner;
	}
	
	// if there is a clip url specified in the story
	if (clipurl)
	{
	
	title = escape(title);

//for backward compatibilty
if(typeof (lp) == 'undefined') { lp = 0; }
if(typeof (mt) == 'undefined') { mt = 'v'; }
	// if the video frame exists then set the page in the video frame clip url
		if (window.parent.videoframe) window.parent.videoframe.location = pathtovideplayer+ "player.shtml?lang="+lang+"&title="+title+"&clipurl="+hackClipUrl(clipurl)+"&cs="+cs+"&bw="+url.bw+"&lp="+lp+"&mt="+mt;
	}
	
	// is the promo frame path specified in the story or index
	if (promo)
	{
		if (window.parent.footerframe) window.parent.footerframe.location = promo;
	}
	
	// this is for full screen
	if (wywindex)
	{
		if (window.parent.lhsframe) window.parent.lhsframe.location = wywindex;
	}
}

// this function is loaded each time the fs_shtml file is loaded
function fsOnLoad()
{

if (document.location.href.indexOf('bb_') == -1) {
	// resize the window to what you set above to be the normal size
	resize(windowDim['normal']['w'],windowDim['normal']['h'],'');
	}
else { //broadband needs bigger window
resize(windowDim['normal']['w'],windowDim['normal']['bbh'],'');
}

	// what is the path of the RHS frame
	getRHSFrameUrl();
	
	//if the url clip url is specified then load the clip
	if (url.clipurl)
	{
	url.title = escape(url.title);

//for backward compatibilty
if(typeof (url.lp) == 'undefined') { url.lp = 0; }
if(typeof (url.mt) == 'undefined') { url.mt = 'v'; }
	
	videoframeurl = pathtovideplayer+ "player.shtml?lang="+lang+"&title="+url.title+"&clipurl="+hackClipUrl(url.clipurl)+"&clippos="+url.clippos+"&cs="+url.cs+"&bw="+url.bw+"&lp="+url.lp+"&mt="+url.mt;
	}
}

/// tries to figure out the RHS frame url based on what you set in the index or story
function getRHSFrameUrl()
{
	var base = "/persian/";
	//var base='';
	
	if (typeof url.pathtostory == 'undefined') { url.pathtostory = ''; }
	
	// you might specify the rHS location specifically for that story
	if (url.rhs)
	{
	rhsframeurl = url.rhs;//fm1
	}
	
	// you might have a different RHS per index like news or entertainment
	if (url.pathtostory)
	{
	rhsframeurl = url.pathtostory + "/" + prefix + "default.shtml";
	}
	
	// is the story id specified then use a specific rhs per story
	if (url.storyid)
	{
	
	//this one below is for rss path to story
	//rhsframeurl = base + "newsid_" + (Math.floor(url.storyid/10000)*10000) + "/newsid_" + (Math.floor(url.storyid/100)*100) + "/"+ prefix + url.storyid + ".shtml";

rhsframeurl = url.pathtostory + prefix + url.storyid + ".shtml";
	}
	
	if (typeof(rss) != 'undefined') {
	if (rss)
	{
rhsframeurl = base + "avconsole/player.shtml?prefix="+ prefix+"&storyid="+url.storyid;
	}
	}
	
	return rhsframeurl;
}

// the alert message when you want to change between players
function alertToChangePrefs()
{
	return confirm(changeprefalert);
}


function resizeThenChangeMSPrefs()
{
	if (alertToChangePrefs())
	{
		resize(windowDim['normal']['w'],windowDim['normal']['h'],'');
		changeMSPrefs();
	}
}

// change versions
function changeMSPrefs()
{
	if (alertToChangePrefs())
	{
	
	// standalone means the video frame is not specified
		if (isStandAloneStory()) {
			window.parent.location.href = mspathtoavconsole+"?redirect=st.shtml&prefs=1&nbram=1&nbwm=1&bbram=1&bbwm=1&lang="+lang + parent.mediaselectorurl;
		} else {
			window.parent.location.href = mspathtoavconsole+ "?redirect=fs.shtml&prefs=1&nbram=1&nbwm=1&bbram=1&bbwm=1&lang="+lang+ parent.rhsframe.mediaselectorurl;
		}	
	}
}


function isStandAloneStory() {			
	return (typeof(window.parent.videoframe)=="undefined");
}


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('.shtml','');
}


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 = mspathtoavconsole + "?redirect=fs.shtml&nbram=1&nbwm=1&bbram=1&bbwm=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";
}

// not used now
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="mb10"><a href="'+rssURI+'" target="_blank"><img height="15" hspace="0" vspace="0" border="0" width="32" alt="RSS Feed" src="http://newsimg.bbc.co.uk/shared/img/v3/rss_feed.gif" title="Really Simple Syndication" align="left" /></a> &nbsp;| <a href="'+url+'" title="What is RSS?" target="_blank" class="story">What is RSS?</a></div>')}
	}
}
