/*
*  DartForPublisher Advertsing Object / Auto.it
*/
function DFP_Advertising() {
  this.adserver = "http://ad.it.doubleclick.net";
  this.defSite    = ((typeof(adSite)!='undefined')?adSite:"auto");
  this.defArea    = ((typeof(adArea)!='undefined')?adArea:"home");
  this.defSubArea = ((typeof(adSubArea)!='undefined')?adSubArea:"home");
  this.defMarca = ((typeof(adMarca)!='undefined')?adMarca:"");
  this.defSeg = ((typeof(adSeg)!='undefined')?adSeg:"");
  this.dfp_random = Math.round(Math.random() * 10000000000000000);
  this.dfp_tile = 0;
  
  //pneumatici
  if (document.location.href.match(/http:\/\/pneumatici/)) {
	  this.defArea = "partner";
	  this.defSubArea = "pneumatici";
  }
}
DFP_Advertising.prototype.setSite = function(siteTag) {
    this.defSite = siteTag;
}
DFP_Advertising.prototype.setArea = function(areaTag) {
    this.defArea = areaTag;
}
DFP_Advertising.prototype.setSubArea = function(subAreaTag) {
    this.defSubArea = subAreaTag;
}
DFP_Advertising.prototype.setSquadra = function(squadraTag) {
    this.defSquadra = squadraTag;
}
DFP_Advertising.prototype.insertAd = function(args) {                                
  if (typeof(args.format) != 'undefined') {
      
      this.dfp_tile++;

      ad_site    = this.defSite; //DFP fixed site prefix
      ad_area    = ((typeof(args.area)!='undefined')?args.area:this.defArea).toLowerCase();
      if (ad_area=="homepage" || ad_area=="") {ad_area="home"}
      dfp_site   = (ad_site+"."+ad_area).toLowerCase();  //DFP site

      ad_subarea = ((typeof(args.subarea)!='undefined')?args.subarea:this.defSubArea);
      ad_subarea = ((ad_subarea.length>0)?"/"+ad_subarea:"");
      dfp_zone   = ad_subarea.toLowerCase();

      ad_marca = ((typeof(args.marca)!='undefined')?args.marca:this.defMarca);
      ad_marca = (((ad_marca.length>0)?";marca="+ad_marca:"")).toLowerCase();
	  
	  ad_seg = ((this.defSeg.length>0)?";seg="+this.defSeg:"");

      ad_pos     = ((typeof(args.pos)!='undefined')?";pos="+args.pos:";pos=1");     
      randDFPnum = ((typeof(args.ord)!='undefined')?";ord="+args.ord:";ord="+this.dfp_random);
      advTile    = ((typeof(args.tile)!='undefined')?";tile="+args.tile:";tile="+this.dfp_tile);

	  if (args.format == "300x250")
		formatMod = "300x250,300x600";
	  else formatMod = args.format;
	  
      dfp_keyval = '';
      for (x in args) {
        if (!x.match("format|pos|site|area|squadra|ord|tile"))
          dfp_keyval += ';'+x+'='+args[x];
      }
      if (this.dfp_tile==1) dfp_keyval += ';dcopt=ist';
      
      //create target DFP string site.area/zone;squdra=x;format=123x456;pos=1...
      target = ""+dfp_site+dfp_zone+";sz="+formatMod+ad_pos+ad_marca+ad_seg+dfp_keyval+advTile+randDFPnum+"?"

      if (!debugADV) {
        document.write('<scr');
        document.write('ipt src="' + this.adserver + '/adj/' + target + '">');
        document.write('</scr');
        document.write('ipt>');
      } else {
        var sep=args.format.toLowerCase().indexOf('x');
        var w=args.format.substr(0,sep);
        var h=args.format.substr(sep+1);
        document.write('<div style="width:'+w+'px; height:'+h+'px; margin: 0 auto; background: url(http://www.auto.it/res/img/grey2.png) repeat center center #fff; font-family: \'Lucida Console\'; color:#606060; font-size:10px; text-align: left; overflow:hidden">'+
          '&nbsp;#'+dfp_site+dfp_zone+";sz="+args.format+ad_pos+ad_marca+dfp_keyval+advTile);
        
        if (showADV) {
          document.write('<scr');
          document.write('ipt src="' + this.adserver + '/adj/' + target + '">');
          document.write('</scr');
          document.write('ipt>');
        }

        document.write('</div>');
      }
      
  } else {
      document.write('<!-- insertAd called with no format parameter -->');
  }
}

// Create advertising management object
var debugADV=(window.location.href.indexOf('debugADV')>1);
var showADV=(window.location.href.indexOf('showADV')>1);
var adParams = new DFP_Advertising;

// Generic insert advertising
function insertAd(args) {
   adParams.insertAd(args);
}

