//////////////////////////////////////////////
// Code for the takeover animation / hiding showing ads etc.
//
// since the code was written the term pre-roll animation is now used but
// the code has not been modified to reflect this. In this file
// takeover means pre-roll and does not affect what is now termed 
// the takeover "css" file. - PL 02/10/2007
/////////////////////////////////////////////


function setAdvertVisibility(visible) {
	// PL 20070919
	// amended to use the diaplay property to hide the div rather than visibility
	// Tango Zebra ads were visible over the top of the pre-roll 
	var displayParam = "none";
	if (visible) {
		displayParam = "block";
	}
	document.getElementById("skyscraper").style.display=displayParam;
	document.getElementById("topbanner").style.display=displayParam;
}

function InitTakeOver() {
}

function TakeOverTweensOut(takeoverid) {
	setAdvertVisibility(true);

}

function TakeOverFinished(takeoverid) {
	//alert("TakeOverFinished takeoverid="+takeoverid);
	setAdvertVisibility(true);
	// called by the takeover loader when the animation has finished.
	var divid = "takeoverdiv"+takeoverid;
	var takeoverdiv = document.getElementById(divid);
	if (takeoverdiv != null) {
		takeoverdiv.style.display="none";
		takeoverdiv.parentNode.removeChild(takeoverdiv);
	}

}

// checks the users cookies to decide if a cookie should be visible
// returns true if the take
// "takeoverFrequency" possible values as set in Pluto CMS 
//	session
//	once
//	always
function isTakeOverVisible(takeoverFrequency, preRollSwfPath) {
	var cookieName =createCookieNameFromFilePath(preRollSwfPath);
	var cookieValue = readCookie(cookieName);
	if (cookieValue != null) {
		// a cookie has been set re-record the viewing the value in the cookie
		// this means if the frequency has been changed in the cms the new frequency will
		// be reflected in the cookie
		createTakeOverCookie(takeoverFrequency,preRollSwfPath,cookieValue);
	}
	if (cookieValue == null || cookieValue=="false") {
		// no cookie has been set yet.
		return true;
	} else {
		if (takeoverFrequency=="always") {
			return true;
		}
		if (takeoverFrequency=="session" || takeoverFrequency=="once") {
			return (cookieValue!="true");
		}
		throw "takeoverFrequency = \""+takeoverFrequency+"\" is not a valid value in \"createTakeOverCookie\" function";
		return false;
	}
}

// records that a user has seen 
// returns true if the take
// "takeoverFrequency" possible values as set in Pluto CMS 
//	session
//	once
//	always
function recordedTakeoverViewToCookie(takeoverFrequency, preRollSwfPath) {
	createTakeOverCookie(takeoverFrequency,preRollSwfPath,"true");
}

function createCookieNameFromFilePath(preRollSwfPath) {
	var cookieName = "tk"+escape(preRollSwfPath);
	return cookieName;
}

// "takeoverFrequency" possible values as set in Pluto CMS 
//	session
//	once
//	always
function createTakeOverCookie(takeoverFrequency, preRollSwfPath, isViewed) {
	var cookieName =createCookieNameFromFilePath(preRollSwfPath);
	if (takeoverFrequency=="always") {
		// no cookie is set.
		return;
	}
	if (takeoverFrequency=="session") {
		createCookie(cookieName,isViewed); // no days parameter to make a session cookie
		return;
	}
	if (takeoverFrequency=="once") {
		createCookie(cookieName,isViewed, 5*365); // 5 years - should be enough
		return;
	}
	throw "takeoverFrequency = \""+takeoverFrequency+"\" is not a valid value in \"createTakeOverCookie\" function";
}



function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(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;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
