///////////////////////////////////////////////////////////////////
// combined 
// 1. http:,,images.tvnz.co.nz.tvnz_site_images,scripts,doubleclick.js and
// 2. http:,,images.tvnz.co.nz,on_demand,scripts,doubleclick.js
///////////////////////////////////////////////////////////////////

// create a new namespace to keep all our stuff in and keep the global namespace tidy


var advertisement = new Object();

try {
	adSlot1 = googletag.defineSlot('/tvnz.co.nz/', [760, 120]);
	adSlot1.set("ad_type", "image");
	adSlot1.addService(googletag.companionAds());
	googletag.enableServices();
}
catch (e) {
	// ignore 
}

function RoadBlocks(p_roadBlocks) {
	this.roadBlocks = p_roadBlocks;
	this.commandRe = /\/pfadx\//;
	this.szRe = /;sz=\d+x\d+;/;
	this.tileRe = /;tile=\d+;/;
}

RoadBlocks.prototype.setup = function (p_adTag) {
	for (var i=0; i<this.roadBlocks.length; i++) {
		var adTag = p_adTag;
		// replace command
		adTag = adTag.replace(this.commandRe, '/' + this.roadBlocks[i].command + '/');
		// replace size
		if ( (this.roadBlocks[i].width=='760') && (this.roadBlocks[i].height== '120') ) {
			adTag = adTag.replace(this.szRe, ';sz=' + this.roadBlocks[i].width + "x" + this.roadBlocks[i].height + ",728x90;");
		} else {
			adTag = adTag.replace(this.szRe, ';sz=' + this.roadBlocks[i].width + "x" + this.roadBlocks[i].height + ';');
		}
		// replace tile
		adTag = adTag.replace(this.tileRe, ';tile=' + this.roadBlocks[i].tile + ';');
		// store it for sync
		this.roadBlocks[i].adTag = adTag;
	}
}

RoadBlocks.prototype.sync = function (d_dcSeed) {
	for (var i=0; i<this.roadBlocks.length; i++) {
		var adTag = this.roadBlocks[i].adTag;
		// chop off and remember ord
		var adTagOrdIndex = adTag.indexOf(";ord=");
		var adTagShort = adTag.substring(0, adTagOrdIndex);
		
		var adTagOrd = adTag.substring(adTagOrdIndex);
		// append dc_seed
		if (d_dcSeed) adTagShort += "dc_seed=" + d_dcSeed;
		// append ord
		adTagShort += adTagOrd + "?"; 
		
		// update iframe
		$('#'+this.roadBlocks[i].id).attr('src', adTagShort);
	}
}
//skyscrapper
var roadBlocks = new RoadBlocks([{id:'minibanner',width:320, height:55, command:'adi', tile:3},{id:'skyscrapper',width:160, height:600, command:'adi', tile:4},{id:'bigbanner',width:760, height:120, command:'adi', tile:1}]);

// we will need to 'alias' the namespaced functions back into the global namespace for compatibility in some cases */

//random number to be added to the DoubleClick URLs to prevent caching
function Advertisement(id, host, url, width, height){
	this.id = id;
	this.host = host;
	this.url = url;
	this.width = width;
	this.height = height;
}
var companionAdvertisements = new Array();
var currentCompanion = null;
var dcTimeout = null;
var dcTimeoutLength = 20000;
var dcDoTimeout = false;
//incremented for each Ad on the same page
var dc_tile = 0;
//random number used in requests for DoubleClick ads. Should be different for each page view, but the same within the page.
var ord = ord ? ord : Math.ceil(Math.random()*10000000000000000);

function setCompanionAds(dc_seed) {
	for ( i in companionAdvertisements ) {
		currentCompanion = companionAdvertisements[i];
		re = new RegExp("dc_seed=;", "ig");
		if (!dc_seed || dc_seed=="null") {
	 		currentCompanion.url = currentCompanion.url.replace(re, "");		 
	 	} else {
	 		currentCompanion.url = currentCompanion.url.replace(re, "dc_seed=" + dc_seed + ";");
	 	}
		var s = document.getElementById(currentCompanion.id);
		var iframeName = "iframe_" + currentCompanion.id;
		if (s) {
			s.innerHTML = '<iframe id="' + iframeName + '" name="' + iframeName + '" src="' + currentCompanion.host + '/adi/' + currentCompanion.url + 'ord=' + ord + '?" width="' + currentCompanion.width + '" height="' + currentCompanion.height + '" tile="' + currentCompanion.tile + '" marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="0" scrolling="no">' + '</iframe>';
		}
		// add a timeout/refresh to the skyscraper 'companion'
		if (iframeName.match("skyscraper") && dcDoTimeout) {
			var dcTimeout=setInterval("dc_timeout('" + iframeName + "');",dcTimeoutLength);
		}
	}
}

function dc_timeout(iframeName) {
	//get src of iframe, and replace the ord random number with a new number
	var src = document.getElementById(iframeName).src;
	document.getElementById(iframeName).src=src.substring(0,src.indexOf("ord=")) + "ord=" + ord;
}

var enableDebug = true;
var debugOutput = null;
function logCallback(text) {
	if (enableDebug && document.location.search.match(/(\?|\&)debug=true(&?)/)) {
		var c = document.getElementById('video_popup_container');
		if (c) {
			var p = c.parentNode;
			if (!debugOutput) debugOutput = document.createElement('textarea');
			debugOutput.setAttribute('style','position:absolute; top:0; left:420px; height: 500; width:420px; border: black 1px solid; background:#eee;');
			debugOutput.style.position='absolute'; 
			debugOutput.style.top='0';
			debugOutput.style.left='420px'; 
			debugOutput.style.height='500px'; 
			debugOutput.style.width='420px'; 
			debugOutput.style.border='black 1px solid'; 
			debugOutput.style.background='#eee';
			p.appendChild(debugOutput);
		}
		if (debugOutput) debugOutput.value += text + "\n---\n";
	}
}

advertisement.initPushDown = function() {
	var openBanner = true;
	$("#pushDownAd img").each(function() {
		if ($(this).attr("src").match(/817-grey.gif$/) != null) {
			openBanner = false;
		}
	});
	if (openBanner) {
		advertisement.openPushDown();
	}
}

advertisement.openPushDown = function(delay) {
	$("#pushDownAd").fadeIn(1500, function() {
		if (delay != undefined) {
			setTimeout(function() {
				advertisement.closePushDown();
			}, (delay * 1000));
		}
	});
}

advertisement.closePushDown = function() {
	if ($.browser.mozilla) {
		var version = parseInt($.browser.version.split(".")[0]) + (parseInt($.browser.version.split(".")[1]) / 10);
		if (version < 2) {
			setTimeout(advertisement.fireFoxRemovePushDownSWF, 250);
		} else {
			$("#pushDownAd").fadeOut();//animate({height: 'hide'}, 1500, "easeout");
		}
	} else {
		$("#pushDownAd").fadeOut();//animate({height: 'hide'}, 1500, "easeout");
	}
}

advertisement.fireFoxRemovePushDownSWF = function() {
	$("#pushDownAd").html("<!-- -->");
	$("#pushDownAd").css({display: "none"});
}



