var pb;
var player;
var minWidth = 988; // 1024;
var minHeight = 660; // 768;
var episodeBrowserWidth = minWidth-216;
var playerWidth = episodeBrowserWidth-240;
var playerHeight = Math.round((playerWidth/16)*9);
var episodeBrowserHeight = playerHeight+24+80;
var overlayFlashObject;
var scrollIsBubbeling = true;
var fromFlash = false;
var showIDS = new Array();
(function($) {
  jQuery.Prima = {
		rssURL: "http://pipes.yahoo.com/pipes/pipe.run?_id=583fd68f46b09a42c089472c6648242f&_render=rss&id=",
		addRSS: function(title) {
			var locationHash = getLocationHash(); 
			var thisShowTitle = title.split("|");
			var thisShowID = locationHash.split("|");
			thisShowTitle = trim(thisShowTitle[0]);
			thisShowID = thisShowID[1];
			if(!isInArray(showIDS,thisShowID))
			{
				showIDS.push(thisShowID);
				$('head:eq(0)').append('<link rel="alternate" type="application/rss+xml" href="'+ jQuery.Prima.rssURL + thisShowID +'" title="SVT Play Prima - '+ thisShowTitle +'" />');
			}
		}
  }
	jQuery(document).ready(function()
	{
	  jQuery.historyInit(setAddressValue);
	});
})(jQuery);
/**
*
*  Javascript trim, ltrim, rtrim
*  http://www.webtoolkit.info/
*
**/
function trim(str, chars)
{
  return ltrim(rtrim(str, chars), chars);
}
function ltrim(str, chars)
{
  chars = chars || "\\s";
  return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
function rtrim(str, chars)
{
  chars = chars || "\\s";
  return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
function isInArray(array,value)
{
	var i;
	for(i=0; i<array.length; i++)
	{
		if(array[i] == value)
		{
		  return true;
		}
	}
	return false;
}
function setInstallTranslation()
{
	MN.QMPInstall.MSG_BADBROWSER = '<div id="moveplay_install">Vi beklagar, men endast följande webbläsare är kompatibla med ditt operativsystem för närvarande:<br><br>%s<br><br>Vi jobbar kontinuerligt för kompabilitet med flera webbläsare.</div>';
	MN.QMPInstall.MSG_BADOS = '<div id="moveplay_install">Vi beklagar, men endast följande operativsystem är kompatibla för närvarande:<br><br>%s<br><br>Vi jobbar kontinuerligt för kompabilitet med flera operativsystem.</div>';
	MN.QMPInstall.MSG_BASE_INSTALLING_PREFIX = '';
	MN.QMPInstall.MSG_BASE_INSTALLING_POSTFIX = '';
	MN.QMPInstall.MSG_CANINSTALL="<div id=\"moveplay_install\">\n<img src=\"img/install1.jpg\"/>\n<a class=\"moveplay_installstep1btn\" href=\"#\" onclick=\"MN.QMPInstall.StartInstall()\"><img src=\"img/install1btn.jpg\"/></a></div>".format(MN.URL.Join(QI.libBase,"moveplaywatchnow.jpg"),MN.URL.Join(QI.libBase,"moveplaywillload.jpg"));
	MN.QMPInstall.MSG_DMGINSTALLMETHOD = '<div id="moveplay_install"> <span class="moveplay_featuretitle">DMG Install.</span> <br><br> <span class="moveplay_featuredesc">Java är antingen inte installerat eller aktiverat. Vänligen genomför en manuell installation från<a href="%s">disk </a> och ladda om denna sida. Alternativt aktivera Java eller installera på <a href="http://java.com/en/download/index.jsp">java.com</a> och ladda om denna sida..</span> </div>';
	MN.QMPInstall.MSG_INSTALL_java = '<div id="moveplay_install"> <div id="moveplay_progressouter"><div id="moveplay_progressinner"></div></div><p><span id="moveplay_installprogressmsg">Laddar...</span></p><img src="img/mac_java_certificate.jpg"/><div class="moveplay_installfooter"><map name="footerMap" id="footerMap"><area href="http://www.svt.se/svt/jsp/Crosslink.jsp?d=62914&lid=hjalp&from=menu" target="_blank" shape="rect" coords="268, 26, 327, 37"/></map><img src="img/installation_msgFooter.jpg" usemap="#footerMap" class="moveplay_imagemap"/></div></div>';
	MN.QMPInstall.MSG_INSTALL_win_ie = '<div id="moveplay_install"> <img src="img/pc_ie_step123.jpg"/><div class="moveplay_installfooter"><map name="footerMap" id="footerMap"><area href="http://www.svt.se/svt/jsp/Crosslink.jsp?d=62914&lid=hjalp&from=menu" target="_blank" shape="rect" coords="268, 26, 327, 37"/></map><img src="img/installation_msgFooter.jpg" usemap="#footerMap" class="moveplay_imagemap"/></div>';
	MN.QMPInstall.MSG_INSTALL_win_mozilla = '<div id="moveplay_install"><img src="img/pc_ff_step123.jpg"/><div class="moveplay_installfooter"><map name="footerMap" id="footerMap"><area href="http://www.svt.se/svt/jsp/Crosslink.jsp?d=62914&lid=hjalp&from=menu" target="_blank" shape="rect" coords="289, 0, 347, 16"/></map><img src="img/installation_msgFooter_FFPC.jpg" usemap="#footerMap" class="moveplay_imagemap"/></div>';
	MN.QMPInstall.MSG_NEEDREINSTALL = '<div id="moveplay_install">För att kunna se detta innehåll behöver du en uppdatering av spelaren. Uppdateringen är snabb och enkel, <a href="javascript:MN.QMPInstall.StartInstall()">klicka här</a>.</div>';
	MN.QMPInstall.MSG_NEEDRESTART = '<div id="moveplay_install"><span class="moveplay_featuretitle">Vänligen starta om din webbläsare.</span> <br><br> <span class="moveplay_featuredesc">För att slutföra, vänligen stäng ner din webbläsare helt och starta om den.</div>.';
	MN.QMPInstall.MSG_NEEDUPGRADE = '<div id="moveplay_install">Vänligen vänta tills en uppdaterad version av spelaren är laddad.<br><br>När den är klar så börjar uppspelningen automatiskt.<br><br><span id="_qmpUpgradeProgress"></span></div>';
	MN.QMPInstall.MSG_NOINSTALLMETHOD = '<div id="moveplay_install"> <span class="moveplay_featuretitle">Kan inte ladda spelaren.</span> <br><br> <span class="moveplay_featuredesc">Det krävs Java för att kunna ladda spelaren. Vänligen aktivera Java eller besök <a href="http://java.com/en/download/index.jsp">java.com</a> för att installera Java.</span> </div>';
	MN.QMPInstall.MSG_UPGRADEFAILED = '<div id="moveplay_install">Spelaren kunde tyvärr inte laddas denna gång:<br>%s<br><br>. Vänligen kolla din uppkoppling och återkom senare.</div>';
	MN.QMPInstall.MSG_UPGRADE_BASE_WIN_MOZILLA = '<div id="moveplay_install"><img src="img/pc_ff_step123.jpg"/><div class="moveplay_installfooter"><map name="footerMap" id="footerMap"><area href="http://www.svt.se/svt/jsp/Crosslink.jsp?d=62914&lid=hjalp&from=menu" target="_blank" shape="rect" coords="289, 0, 347, 16"/></map><img src="img/installation_msgFooter_FFPC.jpg" usemap="#footerMap" class="moveplay_imagemap"/></div>';
	MN.QMPInstall.browsers = 'Microsoft Internet Explorer 5.5 eller senare, Mozilla Firefox 1.5 eller senare.';
	MN.QMPInstall.oses = 'Microsoft Windows 2000/XP/Vista, (i386) Apple Macintosh OS X eller senare, (PPC) Apple Macintosh OS X eller senare.'
}

function checkSystemRequirements()
{
	hw = new MN.Capabilities.HWInfo(player);
	var swf = document.getElementById("mn_flash");
	var ok = hw.meetsMinReqs();
	if(ok)
		swf.meetsMinRequirements(true);
	else
		setTimeout(onTimeout, 1000);
}
function onTimeout()
{
	var swf = document.getElementById("mn_flash");
	swf.meetsMinRequirements(false);
}
function setFlashDivHeight(a_height)
{
	var h = jQuery("#contentcontainer").height();
	if (h > a_height) 
		jQuery("#mn_flashDiv").css("height", h);
	else 
		jQuery("#mn_flashDiv").css("height", a_height);
}
function onWindowResize()
{	
	var w = jQuery("#contentcontainer").width();
	var h = jQuery("#contentcontainer").height();
	var left = Math.round((w - episodeBrowserWidth) / 2);
	var top = Math.round((h - episodeBrowserHeight) / 2) + 1;
	jQuery("#mn_playerDiv").css("margin-left",left);
	jQuery("#mn_playerDiv").css("margin-top",top);
	jQuery("#mn_flashOverlayDiv").css("left",left);
	jQuery("#mn_flashOverlayDiv").css("top",top);											
}
function onScrollWheel(event, delta)
{
	if(!scrollIsBubbeling)
	{
		var swf = document.getElementById("mn_flash");
		swf.onMouseWheel(delta);
	}
	onWindowResize();
	return scrollIsBubbeling;
}
function onPageLoaded() 
{
	setInstallTranslation();
	createFlashBase();
	jQuery(window).resize(onWindowResize);
	onWindowResize();
	jQuery("#contentcontainer").mousewheel(onScrollWheel);
}
function onUIStateChanged(a_event)
{
	if(a_event == 0 && player.isFullScreen())
	{
		//0 = the mouse either moved off the video stage (in windowed mode) or stopped moving (in full screen mode)
		hideControls();overlayFlashObject.callExt("hideControls");			
	}
	else if(a_event == 1 && player.isFullScreen())
	{
		// 1 == the mouse either moved on to the video stage (in windowed mode) or is moving (in full screen mode). 
		//In full screen mode, this event will fire once every 5 seconds while the mouse is moving.
		showControls();	
	}
	else if (a_event == 2) // enter full screen
	{
		setFullscreen();
	}
	else if (a_event == 3) // exit fullscreen
	{
		exitFullscreen();
	}				
}
function onPlayerLoaded(p) 
{
	player = p;
	player.addEventListener("UIStateChanged", onUIStateChanged);
	hideMOVEPlayer();	
	
	createFlashOverlay();
	
	var swf = document.getElementById("mn_flash");
	swf.onMOVEPlayerCreated();
}
function setScrollBubbeling(a_val)
{
	scrollIsBubbeling = a_val;
}
function showMOVEPlayer()
{
	jQuery("#mn_playerDiv").css("visibility", "visible");
}
function hideMOVEPlayer()
{
	jQuery("#mn_playerDiv").css("visibility", "hidden");
}
function createMOVEPlayer()
{
	onWindowResize();
	MN.QVT.CreatePlayer("mn_playerDiv", onPlayerLoaded, playerWidth, playerHeight);
}
function createFlashBase() 
{					
	var so = new SWFObject("playpremium.swf", "mn_flash", "100%", "100%", "8", "#000000");
	so.addParam("scale", "noscale");
	so.addParam("allowFullScreen", "true");
	so.addParam("wmode", "opaque");
	so.addParam("menu", "false");
	so.addVariable("movieID", "mn_flash");
	so.addVariable("playerParentID", "mn_playerDiv");
	so.addVariable("channelUrl", "http://cms009.dc2.movenetworks.com/cms/publish/vod2/vodchannel/svtinteraktiv/4/listing.json");
	so.addVariable("titleUrl", "http://cms009.dc2.movenetworks.com/cms/publish/site/playlist/svtinteraktiv/{id}.qvt"); 
	so.addVariable("helpUrl", "http://svt.se/svt/jsp/Crosslink.jsp?d=62924%26a=1253655");
	so.addVariable("contactUrl", "mailto:svtplay@svt.se?subject=Play%20Prima");
	so.addVariable("playSeUrl", "http://www.svt.se/play");
	so.addVariable("svtSeUrl", "http://www.svt.se");
	so.addVariable("geoCopy", "This material can only be viewed within Sweden, and your location seems to be {country}. If this is incorrect, please contact your internet provider. We regret that you currently will be unable to view this material.");
	so.addVariable("statisticUrl", "http://ld.svt.se/svt/svt/s"); //url to statistics 
	so.addVariable("locale", MN_geo.country);
	so.write("mn_flashDiv");
}
function thisMovie(movieName) {
  if (navigator.appName.indexOf("Microsoft") != -1) {
      return window[movieName]
  }
  else {
      return document[movieName]
  }
}
function setTitle(title)
{
	jQuery.Prima.addRSS(title);
	document.title = title;
	location.href = location.href;
}
function setHistoryFromFlash(hash)
{
	fromFlash = true;
	if(hash == "null" || hash == undefined)
	{
		hash = "";
	}
	jQuery.historyLoad(hash);
}
function setAddressValue(hash)
{
	if(fromFlash)
	{
		fromFlash = false;
	}
	else
	{
		if(thisMovie("mn_flash").onSVTAddressChange != undefined)
		{
			thisMovie("mn_flash").onSVTAddressChange(hash);
		}
	}
}
function getLocationHash()
{
	return location.hash;
}
function createFlashOverlay()
{
	var flashWidth = 168 + 712;
	var flashLeft = Math.round((screen.width - 712) * 0.5) - 168;
	var bottomMargin = Math.round(screen.height * 0.08);
	overlayFlashObject = new MN.Flash.FlashObject({
        url: "playpremiumOverlay.swf",
        id: "mn_flashOverlay",
        playerID: "mn_playerDiv_QMP",
        width:"1px",
        height: "1px",
        flashVersion: 8,
        wmode: "transparent",
		menu:"false",
		fullscreenPosition:"left:"+flashLeft+"px, bottom:"+bottomMargin+"px, width:"+flashWidth+"px, height: 300px"});
		overlayFlashObject.addParam("scale", "noscale");
		overlayFlashObject.addParam("allowFullScreen", "true");
    overlayFlashObject.addVariable("movieID", "mn_flashOverlay");    // Makes the flash aware of its own ID    (REQUIRED)
    overlayFlashObject.addVariable("playerParentID", "mn_playerDiv");
  	overlayFlashObject.addVariable("bitRateCopy", "Play Prima i högupplöst kvalitet levereras med en bit rate upp till 2400 kBit/s. Vilken hastighet du får beror på din internetuppkoppling.\n\nVill du veta hur du kan förbättra uppkopplingen eller mer om Play Prima?\nGå in på hjälpsidorna på svt.se.");
    overlayFlashObject.write ("mn_flashOverlayDiv");
}