/*
*   Adv Configuration
*/
function AdvConfiguration() {
	this.defSite    = "corrieredellosport";
	this.defArea    = (((typeof(adArea)!='undefined') && adArea!="")?adArea:"home");
	this.defSubArea = (((typeof(adSubArea)!='undefined') && adSubArea!="")?adSubArea:"home");
	this.defSquadra = ((typeof(adSquadra)!='undefined')?adSquadra:"");
	if (this.defSquadra.indexOf("|")>0)
	this.defSquadra = "";

	this.site="";
	this.zone="";
	this.pos=1;
	this.format="";

	this.bType = {
		square:"336x600",
		leaderboard:"728x90",
		box:"336x100",
		strip: "980x60",
		one: "1x1"
	};
  
	try {	
		var script = document.getElementById('adscript');
		if (typeof(script)!='undefined') {
			src = script.src;
			query = script.src.substring(src.indexOf("?")+1);
			if (query) {
				vars = query.split("&");
				for (var i = 0; i < vars.length; i++) {
					var pair = vars[i].split("=");
					if (pair[0] == "area") {this.defArea = pair[1]};
					if (pair[0] == "subarea") {this.defSubArea = pair[1]};
				}
			}  
		}
	}catch(ex){} 
}
AdvConfiguration.prototype.getBaseTarget = function(args) {
	ad_site    = ((typeof(args.site)!='undefined')? args.site:this.defSite).toLowerCase();
	ad_area    = ((typeof(args.area)!='undefined')? args.area:this.defArea).toLowerCase();
	this.site  = ad_site+"."+(ad_area=="homepage"?"home":ad_area); //DFP style site

	ad_subarea = ((typeof(args.subarea)!='undefined')?args.subarea:this.defSubArea);
	ad_subarea = ((ad_subarea.length>0)?"/"+ad_subarea:"");
	this.zone  = ad_subarea.toLowerCase();	
	this.pos   = ((typeof(args.pos)!='undefined')?";pos="+args.pos:";pos=1");     
	
	this.format=args.format.toLowerCase();
	
	if (this.format.match(/^[^\d]/)) {
		this.format = this.bType[this.format];
		this.format = ((typeof(this.format)!='undefined') ? this.format : "336x600");
	}
	
	if (this.format=='300x600') this.format='336x600';
	
	return ""+this.site+this.zone+";sz="+this.format+this.pos;
}

/*
*   DartForPublisher Advertsing Object
*/
function DFP_Advertising() {
  this.adserver = "http://ad.it.doubleclick.net";
  this.dfp_random = Math.round(Math.random() * 10000000000000000);
  this.dfp_tile = 0;
}

DFP_Advertising.prototype.getAd = function(args) {	
	ad_squadra = ((typeof(args.squadra)!='undefined')?args.squadra:baseConf.defSquadra);
	ad_squadra = ((ad_squadra.length>0)?";squadra="+ad_squadra:"");
	dfp_kw_squadra = ad_squadra.toLowerCase();
	
	randDFPnum = ((typeof(args.ord)!='undefined')?";ord="+args.ord:";ord="+this.dfp_random);
	advTile    = ((typeof(args.tile)!='undefined')?";tile="+args.tile:";tile="+this.dfp_tile);
	
	dfp_keyval = '';
	for (x in args) {
	if (!x.match("format|pos|site|area|squadra|ord|tile|target"))
	  dfp_keyval += ';'+x+'='+args[x];
	}
	if (args.format=='728x90') dfp_keyval += ';dcopt=ist';
	if (document.location.href.match(/\/foto\//)) dfp_keyval += ';tipo=foto';
	
	//create target DFP string site.area/zone;squdra=x;format=123x456;pos=1...
	target = args.target+dfp_kw_squadra+dfp_keyval+advTile+randDFPnum+"?"
	
	return target;
}
DFP_Advertising.prototype.insertAd = function(args) {
	this.dfp_tile++;
	target = this.getAd(args);
	document.write('<scr');
	document.write('ipt src="' + this.adserver + '/adj/' + target + '">');
	document.write('</scr');
	document.write('ipt>');
}
DFP_Advertising.prototype.getVideoAd = function(args) {
	var target = this.getAd(args);		
	return this.adserver+'/pfadx/'+target;
}
//DartForPublisher Advertsing Object

/*
*	Alt
*/
function Alt_Advertising() {
	//init	
	this.dfp2oa = {
		"corrieredellosport.calcio/.+;sz=336x100;pos=4":"DART",
		"corrieredellosport.+;sz=120x60":false,
		"corrieredellosport\\.(?!home).+;sz=336x100":false
		//"corrieredellosport\\.(?!home).+;sz=300x100":false
	};
}
Alt_Advertising.prototype.getReplacingTag = function(tag) {
	for (altTag in this.dfp2oa) {
		if (tag.match(altTag)) {
			return this.dfp2oa[altTag];
		} 
	}
	return "DART";
}
Alt_Advertising.prototype.insertAd = function(aZone) {
	if (aZone!="") {
		try {		
			document.write('<scr');
			document.write('ipt src="' + aZone + '">');
			document.write('</scr');
			document.write('ipt>');
		}catch(ex){}
	}
}
//Alt



//init
var debugADV=(window.location.href.indexOf('debugADV')>1);
var adParams = null;
var altParams = null;
var baseConf  = null;

function adInit() {
	baseConf  = new AdvConfiguration;
	adParams  = new DFP_Advertising;
	altParams = new Alt_Advertising;
}

//insert advertising
function insertAd(args) {
	if (baseConf == null) adInit();
	if (typeof(args.format) != 'undefined') {			
		args.target = baseConf.getBaseTarget(args);
		zone = altParams.getReplacingTag(args.target);
		
		if (debugADV) {
			var sep=args.format.indexOf('x');
			var w=args.format.substr(0,sep);
			var h=args.format.substr(sep+1);
			document.write('<div style="border: 1px solid #999; width:'+w+'px; height:'+h+'px; margin: 0 auto; background: url(/test/adv/grey2.png) repeat center center #fff; font-family: \'Lucida Console\'; color:#777; font-size:9px; text-align: left; overflow:visible">'+
			  ' #'+args.target+' z:'+zone);
		}

		if (zone == "DART")
			adParams.insertAd(args);
		else if (zone)
			altParams.insertAd(zone);
				
		if (debugADV) {
			document.write('</div>');
		}
	}
}
//insert Alt advertising
function insertAltAd(args) {
	if (baseConf == null) adInit();	
	if (typeof(args.zone) != 'undefined') {	
		atParams.insertAd(args.zone);
	}
}
//video advertising
function getVideoAd(args) {
	if (baseConf == null) adInit();
	if (typeof(args.format) != 'undefined') {
		args.target = baseConf.getBaseTarget(args);
		return adParams.getVideoAd(args);
	}
}


