///////////////////////////////////////////////////////////////////
// combined 
// 1. ord= and 
// 2. http:,,images.tvnz.co.nz.tvnz_site_images,scripts,doubleclick.js and
// 3. http:,,images.tvnz.co.nz,on_demand,scripts,doubleclick.js
///////////////////////////////////////////////////////////////////

// random number for DC ad code
var ord=Math.random()*10000000000000000;

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}]);


//random number to be added to the DoubleClick URLs to prevent caching
//var ord=Math.random()*10000000000000000;
//var ord=Math.ceil(Math.random()*100);
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;

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">' + '</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";
	}
}

