/*Build Date: Mon Apr 9, 2007 14:45:28*/// Namespace
/*
var com=new function(){
	this.mtvi={
		util:{},
		logger:{},
		config:{},
		ads:{},
		reporting:{}
	};
};
*/
var com={
	mtvi:{
		util:{},
		logger:{},
		config:{},
		ads:{},
		reporting:{}
	}
};

// String Functions
String.prototype.trim=function(){
	return this.replace(/^\\s+|\\s+$/g,'');
};
String.prototype.chop=function(n){
	if(isNaN(n)){
		n=this.length-1;
	}
	return this.substring(0,n);
};
String.prototype.ucFirst=function(){
	return this.charAt(0).toUpperCase()+this.substr(1,this.length);
};
// DOM Functions
if(typeof document.getElementsByClassName=="undefined"){
	document.getElementsByClassName=function(s){
		var nodes=[];
		var name=new RegExp('\\b'+s+'\\b');
		var elems=this.getElementsByTagName('*');
		for(var i=0;i<elems.length;i++){
			if(name.test(elems[i].className)){nodes.push(elems[i])};
		}
		return nodes;
	};
}
// Math Functions
Math.getRnd=function(a_numA,a_numB){
	if(!com.mtvi.util.isDefined(a_numA)||isNaN(a_numA)||parseInt(a_numA)<0){a_numA=1;}
	if(!com.mtvi.util.isDefined(a_numB)||isNaN(a_numB)||parseInt(a_numB)<0){a_numB=0;}
	if(a_numA<a_numB){var tempNum=a_numA;a_numA=a_numB;a_numB=tempNum;}
	return(parseInt(Math.random()*(a_numA-a_numB+1)+a_numB));
};/************************************ 
	@package   util
	@author   rogermayorga@gmail.com
*************************************/
com.mtvi.util={
	isDefined: function(v){if(typeof v==='undefined'||v===null||v===''||v==='undefined'){return false}else{return true};},
	stringToObject: function(s,d){
		try{
			var o={};
			var d=d?d:",";
			var s=s.split(d);
			for(var i=0;i<s.length;i++){
				var p=s[i].split('=');
				o[p[0]]=p[1];
			}
			return o;
		}catch(e){}
	},
	objectToString: function(o){
		try{
			var a=[];
			for(var i in o){
				if(typeof o[i]=='string'){
					a.push(i+'='+o[i]);
				}
			}
			return a.join(',');
		}catch(e){}
	},
	queryStringToHash: function(str){
		try{
			var qs=[];
			var a=str.indexOf("?")>-1?str.split("?")[1].split("&"):str.split("&");
			for(var x=0;x<a.length;x++){
				var b=a[x].split("=");
				qs[b[0]]=b[1];
			}
			return qs;
		}catch(e){}
	},
	addOnloadEvent: function(func){
		try{
			if(window.attachEvent){window.attachEvent("onload",func);}
			if(window.addEventListener){window.addEventListener("load",func,false);}
			return this;
		}catch(e){}
	},
	setProperties: function(o){
		try{
			for(var i in o.properties){
				if(typeof o[i]!="object"){
					var name=i.ucFirst();
					eval("o.get"+name+"=function(){return this.properties['"+i+"'];}");
					eval("o.set"+name+"=function(v){this.properties['"+i+"']=com.mtvi.util.isDefined(v)?v:'';}");
				}
			}
		}catch(e){try{com.mtvi.logger.JsError(e);}catch(e){}}
	},
	readCookie: function(name){
		try{
			var name=name+"=";
			var ca=document.cookie.split(';');
			for(var i=0;i < ca.length;i++){
				var c=ca[i];
				while(c.charAt(0)==' '){c=c.substring(1,c.length)};
				if(c.indexOf(name)==0){return c.substring(name.length,c.length)};
			}
			return null;
		}catch(e){}
	},
	setCookie: function(name,value){
		try{
			document.cookie=name+"="+value+"; path=/";
		}catch(e){}
	},
	debug: function(msg){
		try{
			var c=document.getElementById("debug");
			if(!c){
				c=document.createElement("div");
				c.setAttribute("id","debug");
				document.getElementsByTagName("body")[0].appendChild(c);
			}
			c.innerHTML=c.innerHTML+msg+"<br>";
		}catch(e){}
	}
};// logger package
com.mtvi.logger={
	url: "http://viarnd.112.2o7.net/b/ss/viarnd/1/EMAIL?",
	send: function(msg){
		if(MTVi.util.isDefined(msg)){var img=new Image();img.src=msg;}
	},
	JsError: function(e){
		try{
			var map={message:'c21',name:'c22',stack:'c23'};
			var msg=this.url+"pageName=JS_ER-"+window.location;
			if(!MTVi.util.isDefined(e.stack)){e.stack=window.location.href;}
			for(var i in map){
				if(MTVi.util.isDefined(e[i])){
					msg+="&"+map[i]+"="+e[i];
				}
			}
			this.send(msg);
		}catch(e){
			try{this.send(this.url+"pageName=JS_ER-"+"com.mtvi.logger error:"+e.message);}catch(e){}
		}
	}
};/************************************ 
	@class    AdManager
	@author   Josef Blazef
*************************************/
com.mtvi.ads.AdManager=new function(){//singleton adManager object
	this.properties={
		protocol:"http://",										//adManager default protocol
		server:"ad.doubleclick.net",							//adManager default server
		dartSite:self.location.hostname.substring(self.location.hostname.indexOf(".")+1,self.location.hostname.lastIndexOf(".")),//adManager default dartSite
		media:"",												//adManager media
		tileCounter:0,											//adManager tile counter
		pageName:"",											//adManager pagName
		zone:"",												//adManager zone
		sectionArray:new Array(),								//adManager sectionArray (first array index will be gateway)
		namesValuesObj:{},										//admanager namesValuesObj
		debug:false,											//admanager default debug flag for error messages
		ord:Math.getRnd(100000000000000000,999999999999999999)	//adManager ord
	};
	this.DART_SITE_DOMAIN=".mtvi";
	com.mtvi.util.setProperties(this);
	this.adObjects=new Array();
	this.getNewAd=function(a_width,a_height,a_media,a_dartSite,a_gateway,a_pTileFlag,a_protocol,a_server,a_zone){//AdManager.getNewAd() method
		this.adObjects[this.adObjects.length]=new this.adObject(a_width,a_height,a_media,a_dartSite,a_gateway,a_pTileFlag,a_protocol,a_server,a_zone);
		com.mtvi.util.setProperties(this.adObjects[this.adObjects.length-1]);
		return this.adObjects[this.adObjects.length-1];
	};
	this.placeNewAd=function(a_width,a_height,a_media,a_dartSite,a_gateway,a_pTileFlag,a_protocol,a_server,a_zone){//AdManager.placeNewAd() method
		this.getNewAd(a_width,a_height,a_media,a_dartSite,a_gateway,a_pTileFlag,a_protocol,a_server,a_zone);
		this.adObjects[this.adObjects.length-1].write();
	};
	this.populate=function(){//AdManager.populate() method
		var path=self.location.pathname;//by default, set the path to extract section from to self window current url
		if(self.location.search.indexOf("url=")!=-1){//if there is url query with parameter "url", override the default url with this value
			path=self.location.search.substring(self.location.search.indexOf("url=")+4);
			if(path.indexOf("&")!=-1){
				path=path.substring(0,path.indexOf("&"));
			}
			if(path.length<=0){
				path=self.location.pathname;
			}
		}
		this.setSectionArray(path.substring(1).split("/"));
		var tempPageName=(com.mtvi.util.isDefined(this.getSectionArray()) && this.getSectionArray().length>0)? this.getSectionArray().pop():"";//strip off the page name
		tempPageName=(tempPageName.indexOf(".")!=-1)? tempPageName.substring(0,tempPageName.indexOf(".")):tempPageName;
		if(!com.mtvi.util.isDefined(tempPageName)){
			tempPageName="index";
		}
		this.setPageName(tempPageName);
		this.setZone(tempPageName);//asign zone value to be page name value as default
	};
	this.getOrdVal=function(){//adManager getOrdVal method -- looks for ord passed in query string to overwrite random number value
		var retVal=this.getOrd();
		if(self.location.search.indexOf("ord=")!=-1){
			retVal=self.location.search.substring(self.location.search.indexOf("ord=")+4);
			if(retVal.indexOf("&")!=-1){
				retVal=retVal.substring(0,retVal.indexOf("&"));
			}
			if(retVal.length<=0){
				retVal=this.getOrd();
			}
		}		
		return retVal;
	};
	this.populateNamesValuesObj=function(a_obj,a_overwrite){//AdManager.populateNamesValuesObj() method
		if(com.mtvi.util.isDefined(a_obj) && typeof(a_obj)==="object"){
			if(com.mtvi.util.isDefined(this.getNamesValuesObj())){
				if(com.mtvi.util.isDefined(a_overwrite) && a_overwrite===true){}//only override the default object when explicitly set
				else{
					for(var i in this.getNamesValuesObj()){
						a_obj[i]=this.getNamesValuesObj()[i];
					}
				}
			}
			this.setNamesValuesObj(a_obj);
		}
		else{
			this.setNamesValuesObj(new function(){
				var names=new Array();
				var values=new Array();
				for(var i in self.location.search.substring(1).split("&")){
					var paramName=self.location.search.substring(1).split("&")[i];
					if(paramName.indexOf("mockupMode")!=-1 || paramName.indexOf("url")!=-1 || paramName.indexOf("ord")!=-1){//don't add mockupMode  or url or ord value to the ad URL
					}
					else{
						for(var j=0;j<self.location.search.substring(1).split("&")[i].split("=").length;j=j+2){
							names.push(self.location.search.substring(1).split("&")[i].split("=")[j]);
							values.push(self.location.search.substring(1).split("&")[i].split("=")[j+1]);
						}
					}
				}
				for(var k=0;k<names.length;k++){
					this[names[k]]=(com.mtvi.util.isDefined(values[k]))? values[k]:"";//dynamically create property names and assign values to them
				}
			});
		}
	};
	this.adObject=function(a_width,a_height,a_media,a_dartSite,a_gateway,a_pTileFlag,a_protocol,a_server,a_zone){//adObject constructor
		this.properties={
			protocol:(com.mtvi.util.isDefined(a_protocol))? a_protocol:com.mtvi.ads.AdManager.getProtocol(),
			server:(com.mtvi.util.isDefined(a_server))? a_server:com.mtvi.ads.AdManager.getServer(),
			media:(com.mtvi.util.isDefined(a_media))? a_media:com.mtvi.ads.AdManager.getMedia(),
			dartSite:(com.mtvi.util.isDefined(a_dartSite))? a_dartSite:com.mtvi.ads.AdManager.getDartSite(),
			zone:(com.mtvi.util.isDefined(a_zone))? a_zone:com.mtvi.ads.AdManager.getZone(),
			width:(com.mtvi.util.isDefined(a_width))? a_width:"",
			height:(com.mtvi.util.isDefined(a_height))? a_height:"",
			gateway:(com.mtvi.util.isDefined(a_gateway))? a_gateway:"",
			gatewayOverride:(com.mtvi.util.isDefined(a_gateway))? true:false,
			uVal:"u=",
			pTileFlag:(com.mtvi.util.isDefined(a_pTileFlag))? a_pTileFlag:false,
			ord:com.mtvi.ads.AdManager.getOrdVal()
		};
		this.getTile=function(){//adObject.getTile() method
			com.mtvi.ads.AdManager.setTileCounter(com.mtvi.ads.AdManager.getTileCounter()+1);
			return com.mtvi.ads.AdManager.getTileCounter();
		}
		this.getAdURL=function(){//adObject.getAdURL() method
			var retVal='';
			var u_sections='';
			var u_namesValues='';
			retVal+=this.getProtocol();
			retVal+=this.getServer();
			retVal+='/'+this.getMedia();
			retVal+='/'+this.getDartSite()+com.mtvi.ads.AdManager.DART_SITE_DOMAIN;
			retVal+='/'+this.getZone()+';';
			retVal+='pagename='+com.mtvi.ads.AdManager.getPageName()+';';
			if(this.getGatewayOverride()===false){
				this.setGateway((com.mtvi.util.isDefined(com.mtvi.ads.AdManager.getSectionArray()) && com.mtvi.ads.AdManager.getSectionArray().length>0)? com.mtvi.ads.AdManager.getSectionArray()[0]:'');
			}
			retVal+='gateway='+this.getGateway()+';';
			for(var i=1;i<com.mtvi.ads.AdManager.getSectionArray().length;i++){
				retVal+='section_'+i+'='+com.mtvi.ads.AdManager.getSectionArray()[i]+';';
				u_sections+='|section_'+i+'-'+com.mtvi.ads.AdManager.getSectionArray()[i];
			}
			for(var i in com.mtvi.ads.AdManager.getNamesValuesObj()){
				retVal+=i+'='+com.mtvi.ads.AdManager.getNamesValuesObj()[i]+';';
				u_namesValues+='|'+i+'-'+com.mtvi.ads.AdManager.getNamesValuesObj()[i];
			}
			//begin u values
			retVal+=this.getUVal()+'|pagename-'+com.mtvi.ads.AdManager.getPageName();
			retVal+='|gateway-'+this.getGateway();
			retVal+=u_sections;
			retVal+=u_namesValues;
			retVal+=';';
			//end u values
			retVal+='sz='+this.getWidth()+'x'+this.getHeight()+';';
			retVal+=(this.getPTileFlag()===true)? 'ptile='+this.getTile()+';':'tile='+this.getTile()+';';
			retVal+='ord='+this.getOrd()+'?';
			if(this.isOK()===false){
				retVal="";
			}
			return retVal;
		};
		this.write=function(){//adObject.write() method
			var writeVal='';
			if(this.isOK()===true){
				if(self.location.search.indexOf("mockupMode=true")==-1){//skip tag in mockup mode
					writeVal+=this.getTagOpen();
				}
				writeVal+=this.getAdURL();
				if(self.location.search.indexOf("mockupMode=true")==-1){//skip tag in mockup mode
					writeVal+=this.getTagClose();
				}
			}
			document.write(writeVal);
		};
		this.getTagOpen=function(){//adObject.getTagOpen() method
			var retVal='';
			if(this.getMedia()=="adj"){
				retVal+='<scr'+'ipt type="text/javascript" src="';
			}
			else if(this.getMedia()=="adi"){
				retVal+='<ifr'+'ame frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="no"';
				retVal+=' width="';
				retVal+=this.getWidth();
				retVal+='"';
				retVal+=' height="';
				retVal+=this.getHeight();
				retVal+='"';
				retVal+=' src="';
			}
			else if(this.getMedia()=="ad"){
				retVal+='<img border="0" alt=""';
				retVal+=' width="';
				retVal+=this.getWidth();
				retVal+='"';
				retVal+=' height="';
				retVal+=this.getHeight();
				retVal+='"';
				retVal+=' src="';
			}
			return retVal;
		};
		this.getTagClose=function(){//adObject.getTagClose() method
			var retVal=''
			if(this.getMedia()=="adj"){
				retVal+='"><\/scr'+'ipt>';
			}
			else if(this.getMedia()=="adi"){
				retVal+='"><\/ifr'+'ame>';
			}
			else if(this.getMedia()=="ad"){
				retVal+='"\/>';
			}
			return retVal;
		};
		this.isOK=function(){//adObject.isOK() method (checks for allowed values in ad URL)
			var retVal=true;
			if(!com.mtvi.util.isDefined(this.getZone())){
				if(com.mtvi.ads.AdManager.getDebug()===true){
					com.mtvi.debug("Error - Missing zone value in ad URL!");
				}
				retVal=false;
			}
			if(this.isSizeOK()===false){
				if(com.mtvi.ads.AdManager.getDebug()===true){
					com.mtvi.debug("Error - Wrong or missing size value in ad URL!");
				}				
				retVal=false;
			}
			if(this.isMediaOK()===false){
				if(com.mtvi.ads.AdManager.getDebug()===true){
					com.mtvi.debug("Error - Wrong or missing media value in ad URL!");
				}
				retVal=false;
			}
			if(!com.mtvi.util.isDefined(this.getDartSite())){
				if(com.mtvi.ads.AdManager.getDebug()===true){
					com.mtvi.debug("Error - Missing dart site value in ad URL!");
				}
				retVal=false;
			}	
			if(this.is_pTileFlagOK()===false){
				if(com.mtvi.ads.AdManager.getDebug()===true){
					com.mtvi.debug("Error - Wrong or missing pTile usage flag value in ad URL!");
				}
				retVal=false;
			}
			return retVal;
		};
		this.is_pTileFlagOK=function(){//adObject.is_pTileFlagOK() method (checks for boolean pTile flag value in ad URL)
			var retVal=false;
			if(com.mtvi.util.isDefined(this.getPTileFlag()) && typeof(this.getPTileFlag())=="boolean"){
				retVal=true;
			}
			return retVal;
		};	
		this.isMediaOK=function(){//adObject.isMediaOK() method (checks for allowed media value in ad URL)
			var retVal=false;
			var okMedia=new Array("ad","adj","adx","pfadx","adi");
			for(var i=0;i<okMedia.length;i++){//check media values
				if(this.getMedia()==okMedia[i]){
					retVal=true;
					break;
				}
			}
			return retVal;
		};
		this.isSizeOK=function(){//adObject.isSizeOK() method (checks for numeric size value in ad URL)
			var retVal=false;
			if(com.mtvi.util.isDefined(this.getWidth()) && !isNaN(this.getWidth())===true && !isNaN(this.getHeight())===true){
				retVal=true;
			}
			return retVal;
		};
		return this;
	}//end of adObject constructor
	this.populate();				//populates values from path
	this.populateNamesValuesObj();	//populates arb. values from path as default
};
// end of adManager object