//Displays ads on chiller, requires jQuery
//DIVAL is the object that adds the di pixel on the page after every refresh

var DIVAL;
if(AD_CODES.di_url!==undefined){
	DIVAL = new diValue(AD_CODES.di_url);
}

function dartAds(codes){
	this.codes=codes;
	//sets the random value for this iteration
	this.getRandom();
	//found array is a list of what ad sizes we found on the page
	this.found={};
	//iframe used for refreshing ads
	this.iframe_url=this.codes.iframe_url;
	//base url for all add calls, consists of combining the json values shown above
	this.base_dart_url=
		this.codes.ad_base+
		this.codes.params.dart_site+'/'+
		this.codes.params.ad_tag_page_name;
	var params={site:false,sect:false,sub:false,sub2:false,pageid:false,genre:false,daypart:false,xa:'n'};
	//this loop now takes the global params and adds them to the base_dart_url
	for(var i in params){
		if(params.hasOwnProperty(i)){
			if(this.codes.params[i]!==undefined){
				this.base_dart_url+=';'+i+'='+this.codes.params[i];
			}
			else if(params[i]!==false){
				this.base_dart_url+=';'+i+'='+params[i];
			}
		}
	}
	//the ___OE___ is a replacement token that gets taken out and replaced with local category excludes
	this.base_dart_url+=';'+this.codes.params.category_excludes+'___OE___';
	//add the pixelman code
	if(top.__nbcudigitaladops_dtparams!==undefined){
		this.base_dart_url+=';'+(top.__nbcudigitaladops_dtparams||'');
	}
	//this array contains the individual calls for each size
	this.dart_urls=[];
	for(var i in this.codes.sizes){
		if(this.codes.sizes.hasOwnProperty(i)){
			var base=this.base_dart_url;
			var url='';
			for(var j in this.codes.sizes[i]){
				if(this.codes.sizes[i].hasOwnProperty(j)){
					if(j==='category_excludes'){
						base=base.replace(/___OE___/,this.codes.sizes[i][j]);
					}
					else{
						url+=';'+this.codes.sizes[i][j];
					}
					
				}
			}
			base=base.replace(/___OE___/,'');
			this.dart_urls[i]=base+url+';ord='+this.randDartNumber+'?';
			this.dart_urls[i]=this.dart_urls[i].replace(/;;/g,';');
		}
	}
	
}

//This displays an ad, and must be called inline where you want the ad to appear
dartAds.prototype.displayAd = function(size){
	if(this.dart_urls[size]!==undefined){
		var divId;
		if(this.found[size]===undefined){
			
			var counter=0;
			while($('#'+size+'_'+counter).length){
				counter++;
			}
			divId=size+'_'+counter;
			this.found[size]=divId;
		}
		else{
			divId=this.found[size];
		}
		document.write('<div id="'+divId+'" class="beforeAd"></div>'); //this is here to enable the ad refresh so that the ad can live in the proper container
		document.write('<scr'+'ipt src="'+this.dart_urls[size]+'"><\/scr'+'ipt>');
		// var wh=size.split(/x/);
		// $('#'+divId).css({width:parseInt(wh[0]),height:parseInt(wh[1])});
		$('#'+divId).css({width:0,height:0,position:'absolute'});
	}
	
};
dartAds.prototype.getRandom = function(){
	this.randDartNumber=Math.round(Math.random()*1000000000000);
}
dartAds.prototype.refreshAds = function(){
	if(arguments[0]!==undefined && !isNaN(arguments[0])){
		this.randDartNumber=arguments[0];
	}
	else{
		this.getRandom();
	}
	
	for(var i in this.found){
		
		if(this.found.hasOwnProperty(i)){
			var wh=i.split(/x/);
			var url=this.dart_urls[i].replace(/;ord=\d+\?/,';ord='+this.randDartNumber+'?'); //setting the random number
			var divId=this.found[i];
			//this block finds the item with the 'beforeAd' class and uses that to find the parent container of the ad
			//then it sticks a div in there to contain the iframe
			if($('div#'+divId+'.beforeAd').length){
				$('div#'+divId+'.beforeAd').parent().html('<div id="'+divId+'" class="adHolder"></div>'); 
				$('#'+divId).css({width:parseInt(wh[0]),height:parseInt(wh[1]),overflow:'hidden'});
			}
			//empty out the adHolder div, and add the iframe
			$('div#'+divId).html();
			$('div#'+divId).html('<iframe class="adframe" marginheight="0" marginwidth="0" frameborder="0" scrolling="no" frameborder="0" src="' +this.iframe_url + '?c=' + escape(url) + '" width="'+wh[0]+'" height="'+wh[1]+'"></iframe>');
		
		}
	}
	
	//this block of code runs all the omniture calls 
	if(window.s && window.s !== undefined && window.s.t!==undefined && typeof(window.s.t)==='function'){
		s.t();
	}
	if(window.nbc_s && window.nbc_s !== undefined && window.nbc_s.t!==undefined && typeof(window.nbc_s.t)==='function'){
		nbc_s.t();
	}
	if(window.msn_s && window.msn_s !== undefined && window.msn_s.t!==undefined && typeof(window.msn_s.t)==='function'){
		msn_s.t();
	}
	if(window.smsn && window.smsn !== undefined && window.smsn.t!==undefined && typeof(window.smsn.t)==='function'){
		smsn.t();
	}
	//this runs the divalue code
	if(window.DIVAL && window.DIVAL !== undefined && window.DIVAL.d !== undefined && typeof(window.DIVAL.d)==='function'){
		DIVAL.d();
	}
}
var DARTad=new dartAds(AD_CODES);


//this appends the divalue pixel on every ad refresh
function diValue(url){
	this.url=url;
}
diValue.prototype.get_url = function(){
	if(this.url.match(/\?/)){
		return this.url+'&rand='+DARTad.randDartNumber;
	}
	else{
		return this.url+'?rand='+DARTad.randDartNumber;
	}
}
diValue.prototype.get_image = function(){
	return '<img class="ctag" width="1" height="1" alt="" src="'+this.get_url()+'" />';
}
diValue.prototype.d = function(){
	$('body').append(this.get_image());
}
diValue.prototype.dw = function(){
	document.write(this.get_image());
}



