//* AdPlacer *//
if(typeof MTVi=="undefined"){MTVi={};}
if(typeof MTVi.ads=="undefined"){MTVi.ads={};}
MTVi.ads.isDefined=function(v){if(typeof v=='undefined'||v==null||v==''||v=='undefined'){return false}else{return true};}

MTVi.ads.AdPlacer = new function(){

   var protocol = "http://";
   var server = "ad.doubleclick.net";
   var calltype = "adj";
   var site="mtv.mtvi";
   var zone="index";
   this.sz="";
   this.tile=1;
   var ord;
   var includeUvalues=true;
   var autotile=false;
   this.usePtile=false;
   this.includeOrd=true;
   var uvals= new Array();
   var targets=new Array();
   var DS = "/";
   var SC = ";";
   var EQ = "=";
   var AMP = "&";
   var TILE_PARAM = "tile";
   var PTILE_PARAM = "ptile";
   var ORD_PARAM = "ord";
   var SZ_PARAM = "sz";
   var UVALUE_PARAM = "u";
   var UVALUE_EQ = "-";
   var UVALUE_SEP = "|";

   this.setProtocol = function(protocol){
      this.protocol = protocol;	
   }

   this.getProtocol = function(){
      return this.protocol;	
   }

   this.setServer = function(serv){
      this.server = serv;	
   }

   this.getServer = function(){
      return this.server;	
   }
   this.setSite = function(s){
      site = s;
   }

   this.getSite  = function(){
      return site;
   }

   this.setCallType = function(ctype){
      calltype = ctype;	
   }

   this.getCallType = function(){
      return calltype;	
   }

   this.setSz = function(size){
      this.sz = size;	
   }

   this.getSz = function(){
      return this.sz;	
   }

   this.setZone = function(z){
      zone = z;	
   }

   this.getZone = function(){
      return zone;	
   }


   this.setTile = function(t){
      tile = t;	
   }

   this.getTile = function(){
      return tile;	
   }



   this.setTarget = function(key,value){
      try{
         targets[key]=value;
      }catch(e){}
   }

   this.removeTarget = function(key,value){
      targets[key]=null;	
   }

   this.reset=function(key,value){
      targets=new Array();
      uvals=new Array();
      tile=1;
   }

   this.getTargetingString = function(){
      var targetingstring= "";
      for (var i in targets) {
         targetingstring += SC + this.makeParam(i,targets[i]);
      } 
      return targetingstring;
   }

   this.getTile = function(){
      var tileparam = TILE_PARAM;
      if(this.usePtile == true){
         tileparam = PTILE_PARAM;
      }
      return autotile ? this.makeParam(tileparam,tile):"";
   }

   this.generate=function(){

      var targetstr=this.getTargetingString();
      if(includeUvalues == true) {
         targetstr += SC + this.makeParam(UVALUE_PARAM,this.getUVals());
      }

      var tilestr= this.getTile();
      var size = this.makeParam(SZ_PARAM,this.getSz());
      var urlstring= protocol + server + DS + calltype + DS + site + DS + zone + targetstr + size;

      if(autotile == true ) {
         urlstring +=  SC +  this.getTile();
      }

      urlstring += this.getOrd();

      return urlstring;
   }

   this.getOrd = function(){
      return SC + this.makeParam (ORD_PARAM,this.getOrdNum());
   }

   this.setOrd = function(or){
      ord = or;
   }

   this.getOrdNum = function(){
      if(this.includeOrd==true){
         return Math.round(Math.random()*100000000000);
      }else{
         return ord;
      }
   }

   this.makeParam = function(key,value){
      return 	key + EQ + value;
   }

   this.getUVals = function(){
      var u= this.getTargetingString();
      var n= u.length;

      if(u.charAt(n-1) == SC)
         u = u.slice(0,n-1);

      u = u.split(EQ).join(UVALUE_EQ).split(SC).join(UVALUE_SEP);
      u = u + SC;

      return u;
   }

   this.getParameter = function(name){
      var qs = window.location.search;
      var param = name + EQ;

      try {
      var loc = qs.indexOf(param);
         if(loc !=-1){
            var start = loc + param.length;
            var ss = qs.substring(start);
            var end = ss.indexOf(AMP);
            if(end != -1){
               return ss.substring(0,end);
            }else{
               return ss.substring(0);
            }
         }
         }catch(e){
            return "";
         }
   }

   this.addScript = function(parent){

      while(parent.hasChildNodes() == true) {
               parent.removeChild(parent.childNodes[0]);
      }

      var s=document.createElement('script');
      s.setAttribute("type","text/javascript");
      s.setAttribute("src",this.generate());
      parent.appendChild(s);
   }

   this.getScriptTag = function(){
      return '<SCR' + 'IPT type="text/javascript" src="' + this.generate() + '"><\/SCR' + 'IPT>';
   }

}




