/*
	ad.js
	Logic to handle ad calls to the IGN server and displaying of ads on the page
*/
var compIndex = 0;
var returnScreen = null;
var compFlashAd = "http://ad.doubleclick.net/adj/fbc.video/short;srnd=%s;sz=300x250;ord=%s";
function inAd(show){
	if(show){
		//Hide buttons
		hideControls(null, false, true);	 //Needs to execute before playingAd is set
	}
	playingAd = show;
	if(show){
		
		if(ccOn){
			hideClosedCaption();	
		}
		if($("shareScreen").style.display == "block"){
			returnScreen = toggleShare;
			toggleShare(false);
		}
		else if($("infoScreen").style.display == "block"){
			returnScreen = toggleInfo;
			toggleInfo(false);
		}
		else if($("rateScreen").style.display == "block"){
			returnScreen = toggleRate;
			toggleRate(false);
		}
		else{
			returnScreen = null;	
		}
		
		$('mnSidebars').style.visibility = "hidden";
	}
	else{
		if(ccOn){
			showClosedCaption();
		}
		if(returnScreen != null){
			returnScreen();	
			returnScreen = null;
		}
		if (screenMode=="normal" || screenMode=="flash"){
			$('mnSidebars').style.visibility = "visible";
			setSidebarsVersion(screenMode=="flash");
	  		$("mn_sidebars").fadeInJS();
			hideDelayControlsAfterAd();
		}
	}
}

function getAdTest(){
	var params = MN.GetPageParams();
    if(params.adtest) {
    	return true;
    }
	return false;
}

function getClipID(){
	return mn_clipID;
}

function getShowName(){
	return mn_showName;	
}

function getEpisodeNumber(){
	return mn_episodeNumber;	
}

function getShowTitle(){
	return mn_showTitle;	
}

function getSeasonNumber(){
	return mn_seasonNumber;	
}

function getCCFile(){
	return mn_ccFile;	
}

function toggleAdCountdown(show){
	if(show && screenMode != "full"){
		$('adCountdown').style.display = "block";	
	}
	else{
		$('adCountdown').style.display = "none";
	}
}

function updateAdCountdown(number){
	if(screenMode != "full" && playingAd){
		$('adCountdown').style.display = "block";
	}
	$('adCounter').innerHTML = number;
	var num = parseInt(number);
	if(num == 1){
		$('plural').style.display = "none";	
	}
	else{
		$('plural').style.display = "inline";	
	}
}

function hideAd(obj)
{
	$(obj).style.width = '0px';
	$(obj).style.height = '0px';
}

function showAd300(obj)
{
	$(obj).style.width = '300px';
	$(obj).style.height = '250px';
}

function showAd88(obj)
{
	$(obj).style.width = '88px';
	$(obj).style.height = '33px';
}

function showAdFeatures(obj)
{
	$(obj).style.width = '160px';
	$(obj).style.height = '90px';
}


function displayAd300(ad300, click300){
	$('ad300x250').innerHTML = ""; // clear out previous ad if there was one.
	if(ad300.indexOf('.swf') != -1)
	{
		var movie = "%s?clickTAG=%s".format(ad300, escape(click300));
		//$('ad300x250').innerHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="300" height="250"><param name="movie" value="%s" /><embed src="%s" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="300" height="250"></embed></object>'.format(movie, movie);
		var so = new SWFObject(movie, 'flashAd300', '300', '250', '8', '#000000');
		so.addParam('wmode', 'opaque');
		so.write('ad300x250');
		showAd300($('ad300x250'));
	}
	else if(ad300.indexOf('.jpg') != -1 || ad300.indexOf('.gif') != -1)
	{
		$('ad300x250').innerHTML = '<a href="%s" target="_blank"><img src="%s" border="0"></a>'.format(click300,ad300);
		showAd300($('ad300x250'));
	}
	else if(ad300.indexOf('.html') != -1)
	{
		log("Show ad300");
		showAd300($('ad300x250I'));
		log("Set ad300");
		window.frames['i300'].location = ad300;
	}
}

function displayAd88(ad88, click88){
	$('ad88x31').innerHTML = ''; // clear out previous ad if there was one.
	window.frames['i88'].location = "includes/ad.htm";
	if(ad88.indexOf('.swf') != -1)
	{
		log('ad88x31', 'type=swf');
		var movie = '%s?clickTAG=%s'.format(ad88,escape(click88))
		//$('ad88x31').innerHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="88" height="31"><param name="movie" value="%s" /><embed src="%s" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="88" height="31"></embed></object>'.format(movie, movie);
		var so = new SWFObject(movie, 'flashAd88', '88', '31', '8', '#000000');
		so.addParam('wmode', 'opaque');
		so.write('ad88x31');
		showAd88($('ad88x31'));
	}
	else if(ad88.indexOf('.jpg') != -1 || ad88.indexOf('.gif') != -1)
	{
		log('ad88x31', 'type=jpg or gif');
		$('ad88x31').innerHTML = '<a href="%s" target="_blank"><img src="%s" border="0"></a>'.format(click88, ad88);
		showAd88($('ad88x31'));
	}
	else if(ad88.indexOf('.html') != -1)
	{
		log('ad88x31', 'type=html');
		window.frames['i88'].location = ad88;
		showAd88($('ad88x31I'));
	}
}

function displayAdFeatures(adFeat, clickFeat){
	$('adFeatures').innerHTML = ""; // clear out previous ad if there was one.
	if(adFeat.indexOf('.swf') != -1)
	{
		var movie = "%s?clickTAG=%s".format(adFeat, escape(clickFeat));
		//$('adFeatures').innerHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="160" height="90"><param name="movie" value="%s" /><embed src="%s" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="160" height="90"></embed></object>'.format(movie, movie);

		var so = new SWFObject(movie, 'flashAdFeatures', '160', '90', '8', '#000000');
		so.addParam('wmode', 'opaque');
		so.write('adFeatures');
		showAdFeatures($('adFeatures'));
	}
	else if(adFeat.indexOf('.jpg') != -1 || adFeat.indexOf('.gif') != -1)
	{
		$('adFeatures').innerHTML = '<a href="%s" target="_blank"><img src="%s" border="0"></a>'.format(clickFeat,adFeat);
		showAdFeatures($('adFeatures'));
	}
	else if(adFeat.indexOf('.html') != -1)
	{
		log("Show adFeat");
		showAdFeatures($('adFeaturesI'));
		log("Set adFeat");
		window.frames['iFeatures'].location = adFeat;
	}
}

function writeFlashComp(adid){
	//Hide Move Ads
	hideAd('ad300x250');
	hideAd('ad300x250I');
	hideAd('ad88x31');
	hideAd('ad88x31I');
	hideAd('adFeatures');
	hideAd('adFeaturesI');
	
	//Set Flash comp
	// var dToday = new Date();
	// var rand = dToday.getTime();
	var compUrl = "/rpc/ad-short.php?page=short/%s;dc_seed=%s".format(mn_showName,adid);
	//var compUrl = "/rpc/ad-short.php?page=short;dc_seed=%s".format(adid);
	log("Embedding Companion Ad: " + compUrl);
	showAd300($('ad300x250I'));
	window.frames['i300'].location = compUrl;
	showCompanion();
}

//Tracking
function sendPixelHit(url, comp) // comp -> It is from a companion display ad
{
	log('sendPixelHit', url)
	if(comp)
		url += '?count=%s'.format(compIndex++); // This will help make sure that the pixel requests dont get cached.
	var image = new Image(1,1);
	setTimeout(function() { image.src = url; }, 1);
}

function sendNielsenCall(url)
{
	//log('sendNielsenCall: ', url);
	var image = new Image(1,1);
	setTimeout(function() { image.src = url; }, 1);
}

var mn_prepause = {};
var mn_pause_ad = {};
var mn_pause_preload = null;
var inPauseAd = false;
var origScreenMode = "";
var prePauseFS = false;

function preloadPauseAd(ad_url, click_url)
{
	log("PRELOAD PAUSE AD - %s - %s".format(ad_url,click_url));
	if (ad_url && click_url)
	{
		mn_pause_ad.ad_url = ad_url;
		mn_pause_ad.click_url = click_url;
		if (ad_url.endswith(".swf"))
		{
			mn_pause_ad.ad_format = "flash";
		}
		else
		{
			mn_pause_ad.ad_format = "image";		
			mn_pause_preload = new Image();
			mn_pause_preload.src = mn_pause_ad.ad_url;
		}
	}
}

function showPauseAd(show)
{
	if (show == inPauseAd || mn_pause_ad.ad_url == null) return;
	
	var skipNormal = (!show && arguments.length > 1);
	
	inPauseAd = show;
	
	SetStreamLimits(!show);
	
	if (show && mnPlayer_QMP_Get('IsFullscreen') == "1")
	{
		prePauseFS = true;
		mnPlayer_QMP_Set('FullScreen', '0');
	}
	else if (!show && prePauseFS)
	{
		prePauseFS = false;
		mnPlayer_QMP_Set('FullScreen', '1');
	}
	
	var plyr = $("mnPlayer").style;
	var ctrl = $('mnControls').style;
	var btns = $("mnSidebars").style;
	var media = $("player").style;
	var pausead = $('pauseAd').style;
	var pausebut = $("pauseAdContinue").style;
	var video = $("videoPlayer").style;
	var mnplyr_qmp = $("mnPlayer_QMP").style;
	var adfrm = $('adBlock').style;
	var featad = $("featuresAd").style;
	var smallad = $("smallAd").style;
	
	if (show)
	{
		mn_prepause.ctrl_width = ctrl.width;
		mn_prepause.ctrl_height = ctrl.height;
		mn_prepause.btns_width = btns.width;
		mn_prepause.btns_height = btns.height;
		mn_prepause.mnplyr_qmp_height = mnplyr_qmp.height;
		mn_prepause.mnplyr_qmp_width = mnplyr_qmp.width;
		
		origScreenMode = screenMode;
	}
	
	btns.width = (show)?"1px":mn_prepause.btns_width;
	btns.height = (show)?"1px":mn_prepause.btns_height;
	pausead.display = (show)?"block":"none";
	
	if(screenMode == "full")
	{
		var pageSize = MN.GetWindowSize();
		mnplyr_qmp.height = (show)?"1px":mn_prepause.mnplyr_qmp_height;
		mnplyr_qmp.width = (show)?"1px":mn_prepause.mnplyr_qmp_width;
		pausead.margin = (show)?"auto":"";
		pausead.top = (show)?((pageSize[1]-380)/2)+"px":"";
		pausead.left = (show)?"0px":"";
		smallad.visibility = (show)?"hidden":"";
		if (!MN.nonIE)
		{
			pausead.position = (show)?"absolute":"";
			pausead.left = (show)?"-488px":"";
		}
	}
	else
	{
		if (show)
		{
			setMiniPlayer(true);
		}
		else if (origScreenMode != "normal")
		{
			setMiniPlayer(false);
		}
		else if (!skipNormal)
		{
			setTimeout("setMiniPlayer(false);redrawAdBreaks();",250);
		}
		
		ctrl.width = (show)?"1px":mn_prepause.ctrl_width;
		ctrl.height = (show)?"1px":mn_prepause.ctrl_height;
		adfrm.display = (show)?"none":"";
		featad.display = (show)?"none":"";
		pausebut.display = (show)?"block":"none";
		if (show)
		{
			setTimeout("$('pauseAd').style.zIndex = 399;",1);
		}
		else
		{
			pausead.zIndex = 0;
		}
		
		if (!MN.nonIE)
		{
			pausead.top = (show)?"-395px":"";
			pausebut.top = (show)?"-712px":"";
			$("videoPlayer").firstChild.style.height = (show)?"422px":"";
			$("videoPlayer").firstChild.style.overflow = (show)?"hidden":"";
		}
	}
	$('pauseAd').innerHTML = "";
	pausead.backgroundImage = "none";
	if (show)
	{
		if (mn_pause_ad.ad_format=="flash")
		{
			var movie = '%s?clickTAG=%s'.format(mn_pause_ad.ad_url,escape(mn_pause_ad.click_url));
			var so = new SWFObject(movie, 'flashPauseAd', '976', '325', '8', '#000000');
			so.addParam('allowScriptAccess', 'always');
			so.addParam('wmode', 'transparent');
			so.addParam('scale', 'default');
			so.addParam('align', 'l');
			so.addParam('salign', 'tl');
			so.write('pauseAd');
		}
		else
		{
			pausead.backgroundImage = "url(%s)".format(mn_pause_ad.ad_url);
		}
	}
	if (show)
	{
		MN.Event.Observe("pauseAdContinue","click",resumePlayer);
		if (mn_pause_ad.ad_format=="image") MN.Event.Observe("pauseAd","click",onClickPauseAd);
	}
	else
	{
		MN.Event.StopObserving("pauseAdContinue","click",resumePlayer);
		if (mn_pause_ad.ad_format=="image") MN.Event.StopObserving("pauseAd","click",onClickPauseAd);
		if (origScreenMode.indexOf("full")==-1)
		{
			redrawAdBreaks();
		}
		
		if (origScreenMode.indexOf("mini")!=-1)
		{
			setMiniPlayer(true);
		}
	}
}

function onClickPauseAd()
{
	window.open(mn_pause_ad.click_url);
}

function resumePlayer()
{
	$('mn_controls').Resume();
	showPauseAd(false);
}

//Flash Logo Overlay functions

//Calls into logo overlay to set the ad image and redirect url
function loadAd(imageUrl, redirectUrl){
	//imageUrl = "http://ads.fox.com/fod/epson/epson_88x31.jpg";
	log("Load logo ad: " + imageUrl + " redirects to " + redirectUrl);
	if (typeof logoObject.callExt == 'function') {
		logoObject.callExt("loadAd", imageUrl, redirectUrl);
	}
}

//Closes full screen and opens popup with redirect
function logoClicked(redirectUrl){
	var os = MN.QMPInstall.OS();
	if(os != "mac"){
		setTimeout('window.open("'+redirectUrl+'", "logo");', 1000);
		//window.open(redirectUrl, "logo");
	}
	if (mnPlayer_QMP_Get('FullScreenSupport') =="1")
	{
		mnPlayer_QMP_Set('FullScreen', '0');
	}
}

//Interactive Ad functions

var mn_inter = {};
var inInteractiveAd = false;
var preInterFS = false;

function showInteractiveAd(show, url)
{
	if (!show && !inInteractiveAd) return;
	
	SetStreamLimits(!show);
	
	if (show && mnPlayer_QMP_Get('IsFullscreen') == "1")
	{
		preInterFS = true;
		mnPlayer_QMP_Set('FullScreen', '0');
	}
	else if (!show && preInterFS)
	{
		preInterFS = false;
		mnPlayer_QMP_Set('FullScreen', '1');
	}
	
	inInteractiveAd = show;
	
	var plyr = $("mnPlayer").style;
	var ctrl = $('mnControls').style;
	var btns = $("mnSidebars").style;
	var media = $("player").style;
	var interad = $('interactiveAd').style;
	var intercd = $('interactiveAdCountdown').style;
	var video = $("videoPlayer").style;
	var mnplyr_qmp = $("mnPlayer_QMP").style;
	var adfrm = $('adBlock').style;
	var featad = $("featuresAd").style;
	var smallad = $("smallAd").style;
	
	if (show)
	{
		mn_inter.ctrl_width = ctrl.width;
		mn_inter.ctrl_height = ctrl.height;
		mn_inter.btns_width = btns.width;
		mn_inter.btns_height = btns.height;
		mn_inter.mnplyr_qmp_height = mnplyr_qmp.height;
		mn_inter.mnplyr_qmp_width = mnplyr_qmp.width;
		
		hidePlayer();
	}
	
	interad.display = (show)?"block":"none";
	intercd.display = (show)?"block":"none";
	ctrl.width = (show)?"1px":mn_inter.ctrl_width;
	ctrl.height = (show)?"1px":mn_inter.ctrl_height;
	ctrl.top = (show)?"-10px":"";
	btns.width = (show)?"1px":mn_inter.btns_width;
	btns.height = (show)?"1px":mn_inter.btns_height;
	adfrm.display = (show)?"none":"";
	featad.display = (show)?"none":"";
	media.backgroundImage = (show)?"none":"";
	
	if (show)
	{
		if (!MN.nonIE)
		{
			interad.top = (show)?"-395px":"";
			intercd.left = (show)?"0px":"";
			$("videoPlayer").firstChild.style.height = (show)?"422px":"";
			$("videoPlayer").firstChild.style.overflow = (show)?"hidden":"";
		}
		
		var so = new SWFObject(url, 'flashInteractiveAd', '976', '325', '9', '#000000');
		so.addParam('allowScriptAccess', 'always');
		so.addParam('wmode', 'transparent');
		so.addParam('scale', 'default');
		so.write('interactiveAd');
		
		startInteractiveCountdown();
	}
	else
	{
		$('interactiveAd').innerHTML = "";
		MN.Event.StopObserving($('interactiveContinue'), "click", function(){showInteractiveAd(false);});
		redrawAdBreaks();
		$("mn_controls").returnFromIntAd();
		showPlayer();
	}
}

var mn_inter_cd = -1;
function startInteractiveCountdown()
{
	mn_inter_cd = 30;
	updateCountdown();
	$('interactiveText').style.display = "block";
	$('interactiveContinue').style.display = "none";
}

function updateCountdown()
{
	if (mn_inter_cd >= 1)
	{
		$('interactivePlural').style.display = (mn_inter_cd > 1)?"inline":"none";
		$('interactiveCounter').innerHTML = mn_inter_cd;
		mn_inter_cd--;
		setTimeout(updateCountdown, 1000);
	}
	else
	{
		$('interactiveText').style.display = "none";
		$('interactiveContinue').style.display = "block";
		MN.Event.Observe($('interactiveContinue'), "click", function(){showInteractiveAd(false);});
	}
}