/* calculate ratio number of 10000*/
var p=parent;var d=document;var axel=Math.random()+"";var rNum=(axel*10000)+"";var dotat=rNum.indexOf(".");var aNum=rNum.substring(0,dotat);
var count=1;var dUnit=false;var dUnitSky=false;var dUnitBox=false;var sops="";var spos;var skyAd=false;
if(!snc){if(p.snc){var snc=p.snc;}else{var snc="";}}
if(!spos){if(p.spos){var spos=p.spos;}else{var spos="";}}
if(!sky_exist){if(p.sky_exist){var sky_exist=p.sky_exist;}else{var sky_exist=false;}}

// set variable defaults

useHouseAds = true;

// house list array - edited by Rachel and Donna. 
// If you aren't Rachel or Donna and want to edit
// this file make sure you're using the latest version
// format is:
// <link><728x90 filename><alt text><stats counter><sky filename><szone exclude><sadpg exclude><szone include><sadpg include><target>
// link applies to images only
// flash is selected by filename. you can have a swf leaderboard and a jpg sky or vice-versa. If the swf is created, as for dart, with a clickTag, the same link as for the image will be used.
// leaving include empty means the file may appear anywhere, inserting sadpg or szone include means that a unit will not appear in a page that doesn't have either its szone or sadpg listed
// exclude is performed after include so a page that is first included then excluded will not run the unit.
// include and exclude can contain multiple entries separated by commas -- <Idol,Junos,TopStories>
// default for target is "_top". Use "_blank" to open in a new window. Target doesn't work for flash.
// all artwork, except for swf files, need to filed using the img2 option in the pub tool. Path = /CTVShows/img2/Programs/Ads/
// all swf artwork needs to be FTP'd to: /generic/images/ads/SWF/

var houseList_ctv = new Array(

"<http://www.ctv.ca/servlet/ArticleNews/show/CTVShows/20050124/Ghost_Whisperer-default/20051017/><ghost_whisperer_728.jpg><Ghost Whisperer><GhostWhisperer><ghost_whisperer_120.jpg><><><><><>",
"<http://www.ctv.ca/servlet/ArticleNews/show/CTVShows/20030923/CSI-default/20051117/><csi_728.jpg><CSI><CSI><csi_120.jpg><><><><><>",
"<http://www.ctv.ca/insider><insider_leaderboard2.jpg><eTALKDAILY Entertainment Insider><insider_contest><insider_vert2.jpg><><><><><>",
"<http://www.robtv.com/articles/rob.tv/110/your_money_month><robtv728.jpg><Report on Business Television presents Your Money Month><robtv><robtv120.jpg><><><><><target='_blank'>",
"<http://www.ctv.ca/entertainment><etalk_leaderboard2.jpg><eTALKDAILY><eTALKDAILY><etalk_vert2.jpg><><><><><>");

// format is:
// <link><Box ad filename><alt text><stats counter><szone exclude><sadpg exclude><szone include><sadpg include><target>
// ads on hold:
// none


var boxList_ctv = new Array(

"<http://www.ctv.ca/insider><insider_big_box2.jpg><eTALKDAILY Entertainment Insider><insider_contest><><><><><>",
"<http://www.robtv.com/articles/rob.tv/110/your_money_month><robtv300.jpg><Report on Business Television presents Your Money Month><robtv><><><><><target='_blank'>",
"<http://www.ctv.ca/entertainment><etalk_box2.jpg><eTALKDAILY><eTALKDAILY><><><><><>");

// ads on hold:
// none

// pick house ad
function selectImg(format){

		if (format != "300x250"){
			
			houseArray = houseList_ctv ;
			houseArrayParse     = /<(.*)><(.*)><(.*)><(.*)><(.*)><(.*)><(.*)><(.*)><(.*)><(.*)>/g ;
			szoneExclIndex = "6";
			sadpgExclIndex = "7";
			szoneInclIndex = "8";
			sadpgInclIndex = "9";
			popWin = "10";
			
			}
		else {

			houseArray = boxList_ctv;
			houseArrayParse     = /<(.*)><(.*)><(.*)><(.*)><(.*)><(.*)><(.*)><(.*)><(.*)>/g ;
			szoneExclIndex = "5";
			sadpgExclIndex = "6";
			szoneInclIndex = "7";
			sadpgInclIndex = "8";
			popWin = "9";

		
			}
dropAds(format);
	randNum = Math.floor(Math.random()*houseArray.length) ;
	url = houseArray.slice(randNum,randNum+1) ;
	
	left  = houseArray.slice(0,randNum) ;
	right = houseArray.slice(randNum+1) ;
	houseArray = left.concat(right) ;
	return url ;
	

	
}

function dropAds(format){

		for (i=(houseArray.length) ; i > 0 ; --i) {
		
			var currentZone = houseArray[i-1].replace(houseArrayParse,"$"+szoneExclIndex);
			var currentAdpg = houseArray[i-1].replace(houseArrayParse,"$"+sadpgExclIndex);	
			
			var currentiZone = houseArray[i-1].replace(houseArrayParse,"$"+szoneInclIndex);
			var currentiAdpg = houseArray[i-1].replace(houseArrayParse,"$"+sadpgInclIndex);
			
			var testCurrentZone = currentZone.split(/\,/g);
			var testCurrentAdpg = currentAdpg.split(/\,/g);
			
			var testCurrentiZone = currentiZone.split(/\,/g);
			var testCurrentiAdpg = currentiAdpg.split(/\,/g);		
			
			var currentAdIncl = 1;
			if((currentiZone != "") || (currentiAdpg != "")){
			
			currentAdIncl = 0;
			for (j=(testCurrentiZone.length) ; j > 0 ; --j) {
			if(testCurrentiZone[j-1] == szone){
				currentAdIncl = 1;
			}
			}

			for (j=(testCurrentiAdpg.length) ; j > 0 ; --j) {
			if(testCurrentiAdpg[j-1] == sadpg){
				currentAdIncl = 1;
				
			}			
			}
			}

			for (j=(testCurrentZone.length) ; j > 0 ; --j) {
			if((testCurrentZone[j-1] == szone) && (szone != "")){
				currentAdIncl = 0;
			}
			}
			for (j=(testCurrentAdpg.length) ; j > 0 ; --j) {
			if((testCurrentAdpg[j-1] == sadpg) && (sadpg != "")){
				currentAdIncl = 0;
				
			}			
			}		
			
			if(currentAdIncl == 0){

				houseArray.splice((i-1),1);

			}		
			
		}
}

// house ad code
function writeHouseAd(format){

	pick = 1 ;
		for (i=0 ; i <= pick-1 ; i++ ) {
display = selectImg(format);
			var urlVal  = display[0].replace(houseArrayParse,"$1") ;
			var text  = display[0].replace(houseArrayParse,"$3") ;
			var adCounter  = display[0].replace(houseArrayParse,"$4") ;
			var currentDiv = document.getElementById(format+"temp");}

			
		if(format=="120x600"){
			var ad_width="120";
			var ad_height="600";
			var img=display[0].replace(houseArrayParse,"$5") ;

		}	
		else if(format=="728x90") {
			var ad_width="728";
			var ad_height="90";
			var img=display[0].replace(houseArrayParse,"$2") ;
			
		}
		else if (format=="300x250"){
			var ad_width="300";
			var ad_height="250";
			var img=display[0].replace(houseArrayParse,"$2") ;	
}
		var currentPopWin = display[0].replace(houseArrayParse,"$"+popWin);
		var	imgParse     = /(.*)\.(.*)/g ;
		var imgFormat = img.replace(imgParse,"$2") ;

		if(currentPopWin == ""){
		currentPopWin = "_top";
		}
		
			var adString = "";
					if(imgFormat=="swf"){
adString += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=3,0,0,0" id="FLASH_AD" height="'+ad_height+'" width="'+ad_width+'"><param name="movie" value="http://www.ctv.ca/generic/images/ads/SWF/'+img+'?clickTag='+urlVal+'"><param name="quality" value="autohigh"><param name="bgcolor" value="#ffffff"><param name="wmode" value="opaque"><embed wmode="opaque" src="http://www.ctv.ca/generic/images/ads/SWF/'+img+'?clickTag='+urlVal+'" quality="autohigh" swliveconnect="TRUE" bgcolor="#ffffff" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" height="'+ad_height+'" width="'+ad_width+'"></object>'
                        adString += '<div style="display: none"><img src="http://adcounter.theglobeandmail.com/servlet/AdletCounter?ad1=ctv_'+adCounter+'House_'+ad_width+'x'+ad_height+'swf_ctv" width="1" height="1" border="0"></div>';

}
		else {
			adString += '<a href="'+urlVal+'" target="'+currentPopWin+'"><img src="http://www.ctv.ca/archives/CTVShows/img2/Programs/Ads/'+img+'" width="'+ad_width+'" height="'+ad_height+'" alt="'+text+'" border="0"></a>';
			adString += '<div style="display: none"><img src="http://adcounter.theglobeandmail.com/servlet/AdletCounter?ad1=ctv_'+adCounter+'House_'+ad_width+'x'+ad_height+'_ctv" width="1" height="1" border="0"></div>'; 

}
if(document.getElementById(format+"temp")){

			currentDiv.innerHTML = adString;
			currentDiv.style.display = "block";
}
else{

document.write(adString);
}

	
		
	}	

// doubleclick code

function writeDClick(size) {
if(!ord){var abc=Math.random()+"";var ord=abc.substring(2,abc.length);}
if((size=="728x90")&&(sky_exist)){spos="dsky";}
var adCodeTxt='<scr'+'ipt type=\"text/javascript\" src=\"http://ad.ca.doubleclick.net/adj/ctv/'+szone+';abr=!webtv;mode='+smode+';loc=hub;adpg='+sadpg+';'+sarena+';ops='+sops+';pos='+spos+';kw=;nc='+snc+';sz='+size+';tile='+count+';ord='+ord+'?\">'+'<\/scr'+'ipt>';
/*alert(adCodeTxt);*/
document.write(adCodeTxt);
document.write('<div style="display: none"><img src="http://adcounter.theglobeandmail.com/servlet/AdletCounter?ad1=ctv_'+sadpg+'Dart_'+size+'_ctv" width="1" height="1" border="0"></div>');
	var currentDiv = document.getElementById(size+"temp");
	currentDiv.style.display = "none";
	count=count+1;spos="";
	}










// main functions
// the stardard way to place an ad is writeHouseAd("120x600");
function placeRandom(format){

// ratio of doubleclick ads to house ads is 1 to nth (right now 1 to 6) 
var nth = 6;
var rnd = Math.floor(Math.random() * nth) + 1;
if (rnd == nth) {
     useHouseAds = false; 
}
// initialize variables and set conditions
// exceptions to this ratio follow:
// only doubleclick ads on the Contest Hub 
// - traffic is low on this hub and it 
// makes it easy to see dart ads to have 
// one hub with only doubleclick.

 if(sadpg == "ContestsHub" || sadpg == "Sports" || sadpg == "Weather" || sadpg == "CanadaAM" || sadpg=="test" && format == "728x90"){
	useHouseAds = false;  }

// only house ads on Consumer
if((szone == "Consumer")){
	useHouseAds = true;
	}

// only house ads on idol
if((szone == "Idol" || szone == "idol")){
	useHouseAds = true;
	}

// Degrassi
if((szone == "Degrassi")){
	useHouseAds = true;
	if(format=="728x90"){if(aNum>=10000){useHouseAds=false;}}
	if(format=="300x250"){if(aNum>=10000){useHouseAds=false;}}		
	}

// Instant Star
if((szone == "InstantStar")){
	useHouseAds = true;
	}


// Happy box ads
if((szone == "happy" && format == "300x250")){
	useHouseAds = false;
	}

// Happy leaderboard
if((szone == "happy" && format == "728x90")){
	useHouseAds = false;
	}


// Playlist box ads
if((szone == "playlist" || szone == "ETalkPlayList" && format == "300x250")){
	useHouseAds = false;
	}

// Playlist leaderboard ads
if((szone == "ETalkPlayList" && format == "728x90")){
	useHouseAds = false;
	}




if((dUnitSky)||(dUnitBox)){useHouseAds=false;}

if(useHouseAds == true){
	writeHouseAd(format);
	}
else {
	writeDClick(format);
}
	}
// these functions are for reverse-compatibility 
function placeCTVRandom120x600Img(isBlank){

placeRandom("120x600");
}
function placeRandom728x90Img(isBlank){

placeRandom("728x90");
}

function placeRandom300x250Img(isBlank){
placeRandom("300x250");

}


