var SERVER_URL = 'http://www.bbc.co.uk';
try {
	var tmp1 = window.location.host.toString();
	if(tmp1!=null&&tmp1.length>0)
	SERVER_URL = 'http://'+tmp1;
}
catch(e) { }
var RANDOMCACHEBEATER=Math.floor(Math.random()*100000);

var CookieManager = function() {
	/*
	Slightly restructured from http://www.quirksmode.org/js/cookies.html
	createCookie('ppkcookie','testcookie') - name,value
	*/
	return {
		createCookie: function(name,value) {
			// sets the expiry to the next monday at 5pm
			var date = new Date();
			var currentDay = parseInt(date.getDay()); // 0 = Sunday
			var currentTime = date.getTime(); // not date.getTime()
			var dayOffset = (7-currentDay)+1; // +1 to make it monday
			date.setTime(currentTime+(dayOffset*24*60*60*1000));
			var monthname = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
			var currentMonth = monthname[parseInt(date.getMonth())];
			var date2 = new Date(currentMonth+' '+date.getDate()+', '+date.getFullYear()+' 17:00:00');
			//alert(date2.toGMTString());
			
			var expires = "; expires="+date2.toGMTString();
			document.cookie = name+"="+value+expires+"; path=/";
		},
		readCookie: function(name) {
			var nameEQ = name + "=";
			var ca = document.cookie.split(';');
			for(var i=0;i < ca.length;i++) {
				var c = ca[i];
				while (c.charAt(0)==' ') c = c.substring(1,c.length);
				if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
			}
			return null;
		},
		eraseCookie: function(name) {
			var date = new Date();
			date.setTime(date.getTime()+(-1*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
			document.cookie = name+"="+expires+"; path=/";
		}
	}
}();


var FlashHistoryChecker = function() {
	return {
		IsInFlashMode: function(id) {
			var result = (CookieManager.readCookie('fhcheck') === 'true');
			
			return result;
		},
		SetFlashMode: function(input) {
			CookieManager.eraseCookie('fhcheck');
			CookieManager.createCookie('fhcheck',input,7); // read back updated cookie details
			
			// alert('input: '+input);
			// alert('CookieManager.readCookie(\'fhcheck\'): '+CookieManager.readCookie('fhcheck'));
		}
	}
}();

glow.ready(function() {
	// remove right hand column if it's empty - http://vdev.national.core.bbc.co.uk/images/45234/
	if(glow.lang.trim(glow.dom.get('#vision-gallery-ppa-container #ppa-col-b').html()).length==0)
		glow.dom.get('#vision-gallery-ppa-container #ppa-col-b').remove();
	
	// check to see if we are in the /programmes section
	var ioprog = window.location.pathname.indexOf('/programmes/');
	var ioimages = window.location.pathname.indexOf('/images/');
	if(ioprog>-1||ioimages>-1) {
		// check to see if the previous viewing mode was basic (html) or enhanced (flash)
		if(FlashHistoryChecker.IsInFlashMode()) {
			glow.dom.get('#blq-local-nav #apslocalnav-enhanced').show();
			glow.dom.get('#blq-local-nav #apslocalnav-basic').hide();
		}
	}
	else {
		// if we are on the basic (html) or enhanced (flash) version
		if(merlinNoFlash)
			FlashHistoryChecker.SetFlashMode(false);
		else
			FlashHistoryChecker.SetFlashMode(true);
	}
	
	if(FlashHistoryChecker.IsInFlashMode()) {
		if(glow.env.ie.toString()=='NaN') {
			var tmp1 = window.location.href;
			if(tmp1.indexOf('/programmes/b00mjlxv')>-1||tmp1.indexOf('/programmes/b00mjrwk')>-1) {
				glow.dom.get('#blq-main').prepend(
					'<div id="cloke" style="background:transparent url(/merlin/images/clokebg.png) no-repeat; height:100%; position:absolute; width:974px; z-index:1000;">&nbsp;</div>'
				);
				var clokeAnim = glow.anim.fadeOut("#cloke", 3.5);
				glow.events.addListener(clokeAnim, "complete", function() {
					glow.dom.get("#cloke").remove();
				});
				//setTimeout("clokeAnim.start();",6000);
			}
		}
	}
})