/*
@author Jeffrey Greenman @modified: 02222008 @Description Add integration library @Notes Scripts Third Parties To Be Place Before calling this script;
@key g = Global, S or s = String, O or o = Object, i = integer;

Usage: oFoxContrAd.initAds('pos','336','250', '300x250,336x280');
*/
var wXH="",calledOnce=0,gSAdLoc,gSSiteZone,gSKeywords,gSValues,gSUrl,gSInfo,gSTest,gITile=1,gOAdParent,gSAdPosition,gSAdWidth,gSAdHeight, gSDcopt, gSRScience, gIRandom, gAdv="";
var ranNum = new String(Math.random()); gIRandom = ranNum.substring (2, 11);
var oFoxContrAd = {
	buildAdLoc:function(sWLocation){ 
		var sAdLoc="";
		var adcookie = (oFoxContrAd.getCookie("p_AD") != "")?(oFoxContrAd.getCookie("p_AD").substring(0,oFoxContrAd.getCookie("p_AD").length-1)+"united_states;").replace(/&/g, ';'):'';
		sAdLoc+="http://ad.doubleclick.net/adj/fbn/";
		sAdLoc+=oFoxContrAd.getSiteZone(); //set once per pg
		sAdLoc+=oFoxContrAd.getDcopt();
		sAdLoc+=oFoxContrAd.getSValues(); //set once per pg
		sAdLoc+=";kw="+oFoxContrAd.getKeywords(); //set once per pg
		sAdLoc+=";ptype="+oFoxContrAd.getPType(); //set once per pg
		sAdLoc+=";pageid="+oFoxContrAd.getPageId(); // set once per pg
		sAdLoc+=";col="+oFoxContrAd.getColumn(); //set once per pg
		sAdLoc+=";url="+oFoxContrAd.getUrl(); //set once per pg
		if(sWLocation.indexOf("referrer=fn")!=-1){
                        sAdLoc+=";referrer=fn";
                }
		sAdLoc+=";fbn=ad";
		sAdLoc+=oFoxContrAd.getTest(); //set once per pg
		sAdLoc+=oFoxContrAd.getRScience(); //add revenue science
		sAdLoc+=";!c=";
		sAdLoc+=";pos="+oFoxContrAd.oAd.position()+";";
		sAdLoc+=adcookie;
		var adsize = oFoxContrAd.oAd.size();
		if (adsize!='') {
			sAdLoc+="sz="+adsize;
		} else {
			sAdLoc+="sz="+oFoxContrAd.oAd.width()+'x'+oFoxContrAd.oAd.height();
		}
		sAdLoc+=";tile="+oFoxContrAd.getTile()+";";
		oFoxContrAd.setTile();  //increment tile
		sAdLoc+="ord="+oFoxContrAd.getRandom();
		sAdLoc+="?";
		sAdLoc+=";adv="+oFoxContrAd.getAdv();
		oFoxContrAd.setAdLoc(sAdLoc);
	},
	buildInfo: function(){
		var sLCkInfo = Get_Cookie('fbn_cui');
		if (sLCkInfo != null) {
			var stats = new Array();
			try {
				stats = loginCookie.split('|');
				r1 = new Date().getFullYear() - stats[1];
				r2 = stats[2];
				r3 = stats[3];
				r4 = stats[4];
				r5 = stats[5];
			} catch (e) {}
			sInfo = ";r1="+r1+";r2"+r2+";r3"+r3+";r4"+r4+";r5"+r5;
				 }
		oFoxContrAd.setInfo(sInfo);
	},
	buildKw:function(sWLocation){
		var sMeta = document.getElementsByTagName('meta');
		for(i=0;i<sMeta.length;i++)
			if(sMeta[i].name=="keywords") oFoxContrAd.setKeywords(sMeta[i].getAttribute('content').replace(/, /g,',').replace(/ /g,'_').replace(/,/g,'+').replace(/'/g,'').toLowerCase());
	},
	buildParentMetadata: function(sPosition, sWidth, sHeight, sSz){
		oFoxContrAd.setAdPosition(sPosition);
		oFoxContrAd.setAdWidth(sWidth);
		oFoxContrAd.setAdHeight(sHeight);
		oFoxContrAd.setAdSize(sSz);
	},
	buildRScience: function(){
		//var rsinetsegs = new Array();
		if(typeof(rsinetsegs) != "undefined") {
			var sRScience = rsinetsegs.length > 0 ? ";segs=" + rsinetsegs[0] : "";
			var sTempSize = rsinetsegs.length;
                        if(sTempSize>10){
                                sTempSize=10;
                        }
			for (var i = 1; i < sTempSize; i++) sRScience += ("&segs="  + rsinetsegs[i]);
			oFoxContrAd.setRScience(sRScience.replace(/&/g,';'));
		} else {
			oFoxContrAd.setRScience('');
		}
	},
	buildSiteZone: function(sWLocation){
		sPath = sWLocation.split('com/')[1].split('.')[0].split('?')[0];
		ptype='';
	    if(sPath.indexOf("story/")!=-1){
	      sPath = sPath.substring(0, sPath.length-1); //assumption last char will be /
	      sPath = sPath.replace('story/', ''); //story workaround
	      sPath=sPath.substring(0, sPath.lastIndexOf('/'));
	      ptype='story';
	    } else if(sPath.indexOf("slideshow/")!=-1){
	    	if(oFoxContrAd.getAdPosition() == "sponsor") {
	    		oFoxContrAd.setTile();
	    		oFoxContrAd.setAdv("businesstest");
	    	} else {
	    		oFoxContrAd.setAdv("");
	    	}
	        sPath = sPath.substring(0, sPath.length-1); //assumption last char will be /
		    sPath = sPath.replace('slideshow/', ''); //slideshow workaround
		    sPath=sPath.substring(0, sPath.lastIndexOf('/'));
		    ptype='slideshow';
	    }

		if(typeof(pageType) !="undefined" && ptype !='story' && ptype!='slideshow'){
                        ptype=pageType;
                }

		if(typeof(pageType) !="undefined" &&  (pageType == 'story' || pageType == 'slideshow')){
			sSiteZone = sPath.substring(0, sPath.lastIndexOf('/'));
			if(pageType=='slideshow'){
				sSiteZone=sPath;
			}
			return oFoxContrAd.setSiteZone(sSiteZone);
		} //article
		if(sPath==""){return oFoxContrAd.setSiteZone('root');} //root
		else if(sWLocation.indexOf("foxbusiness.com/index.html") !=-1){return oFoxContrAd.setSiteZone('root');}
		else if(sPath!="" && sPath.lastIndexOf('/')==sPath.length){ sSiteZone += 'index';} //section or subsection - nonroot without index
		else{sSiteZone = sPath;}//regular path}
		return oFoxContrAd.setSiteZone(sSiteZone.replace(/-/g,'_').toLowerCase());
	},
	buildSValues: function(sWLocation){ //extends buildSiteZone
		sSiteZone = oFoxContrAd.getSiteZone().split('/');
		var sS1 = typeof(sSiteZone[0])!= "undefined" ? ';s1='+sSiteZone[0] : ';s1=';
		var sS2 = typeof(sSiteZone[1])!= "undefined" ? ';s2='+sSiteZone[1] : ';s2=';
		var sS3 = typeof(sSiteZone[2])!= "undefined" ? ';s3='+sSiteZone[2] : ';s3=';
		oFoxContrAd.setSValues(sS1+sS2+sS3);
	},
	buildTest:function(){
		oFoxContrAd.getQString('test=on') ? sTest=";test=on" : sTest="";
		oFoxContrAd.getQString('test=pa') ? sTest+=";test=pa" : sTest+="";
		oFoxContrAd.getQString('test=smbhub') ? sTest+=";test=smbhub" : sTest+="";
		oFoxContrAd.getQString('test=smb_block') ? sTest+=";test=smb_block" : sTest+="";
		oFoxContrAd.setTest(sTest);},
	buildUrl:function(sWLocation){
		var sSite = sWLocation.split("com/")[1].split('?')[0].split('.')[0].replace(/\//g,'_').replace(/\./g,'_').replace(/-/g,'_').replace(/#.*/g,'').toLowerCase();
		if(sSite==""){sSite='root';}
		else if(sWLocation.indexOf("foxbusiness.com/index.html")!=-1){sSite='root';}
    	if(sSite.indexOf("story_")!=-1){sSite = sSite.replace(/story_/, "");};
    	if(sSite.indexOf("slideshow_")!=-1){sSite = sSite.replace(/slideshow_/, "");};
		oFoxContrAd.setUrl(sSite);
	},
	getAd:function(){document.write('<scr'+'ipt type="text/javascript" src="'+oFoxContrAd.getAdLoc()+'"></scr'+'ipt>');return '';},
	getAdLoc: function(){ return gSAdLoc;},
	getAdWidth: function(){	return gSAdWidth;},
	getAdHeight: function(){return gSAdHeight;},
	getAdSize: function(){return gSAdSize;},
	getAdParent: function(){return gOAdParent;},
	getAdPosition: function(){return gSAdPosition;},
	getColumn: function(){return col = typeof(col)!="undefined" ? col=col : '';},
	getCurrentElement: function(){return oCurrentElement;},
	getDcopt: function(){if(oFoxContrAd.oAd.width()=="728" || oFoxContrAd.oAd.width() == "938"){return ";dcopt=ist";}else{return "";}},
	getElementsByClassName: function(oElm, strTagName, strClassName){
		var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
		var arrReturnElements = new Array();
		strClassName = strClassName.replace(/\-/g, "\\-");
		var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
		var oElement;
		for(var i=0; i<arrElements.length; i++){
			oElement = arrElements[i];
			if(oRegExp.test(oElement.className)){
				arrReturnElements.push(oElement);
			}
		}
		return (arrReturnElements)
	},
	getInfo: function(){return gSInfo;},
	getPageId: function(){return pageid = typeof(pageid)!="undefined" ? pageid=pageid : '';},
	getPType: function(){return ptype = typeof(ptype)!="undefined" ? ptype=ptype : '';},
	getKeywords: function(){gSKeywords = (typeof(gSKeywords)!="undefined")? gSKeywords : '';return gSKeywords;},
	getQString:function(sMatch){
		var query = window.location.search.substring(1);
		if(query.indexOf(sMatch)!=-1){return true;}else{return false;};
	},
	getRandom:function(){return gIRandom;},
	getAdv:function(){return gAdv;},
	getRScience: function(){gSRScience = (typeof(gSRScience)!="undefined")? gSRScience : '';return gSRScience},
	getSiteZone: function(){return gSSiteZone;},
	getSValues: function(){	return gSValues;},
	getTest:function(){return gSTest},
	getTile:function(){return gITile;},
	getUrl:function(){return gSUrl},
	getCookie:function (c_name) {
		if (document.cookie.length>0)
		  {
		  c_start=document.cookie.indexOf(c_name + "=");
		  if (c_start!=-1)
		    { 
		    c_start=c_start + c_name.length+1; 
		    c_end=document.cookie.indexOf(";",c_start);
		    if (c_end==-1) c_end=document.cookie.length;
		    return unescape(document.cookie.substring(c_start,c_end));
		    } 
		  }
		return "";
	},
	initAds: function(sPosition, sWidth, sHeight, sSz){
		var sWLocation = window.location.toString().toLowerCase();
		if(sWLocation !=null && sWLocation.indexOf("#") == sWLocation.length-1){
			sWLocation=sWLocation.substring(0, sWLocation.length-1);
		}
		//set one time variables
		sSz = sSz != undefined ? sSz : '';
		oFoxContrAd.buildParentMetadata(sPosition, sWidth, sHeight, sSz);
		oFoxContrAd.buildSiteZone(sWLocation);
		oFoxContrAd.buildSValues(sWLocation); //extends buildSiteZone
		oFoxContrAd.buildKw(sWLocation);
		oFoxContrAd.buildUrl(sWLocation);
		oFoxContrAd.buildRScience();
		oFoxContrAd.buildTest();
		oFoxContrAd.buildAdLoc(sWLocation);
		oFoxContrAd.getAd(); //build url and inserts into dom
		oFoxContrAd.getCookie("p_AD");
	},
	maven: {
			dartSiteZone: function(){return gSSiteZone;},
			baseKeyValue: function(){var sMAdLoc='';sMAdLoc+=oFoxContrAd.getSValues().substring(1, oFoxContrAd.getSValues().length);return sMAdLoc;},
			revenueScience: function(){return gSRScience;}
	},
	oAd: {
		height: function(){return oFoxContrAd.getAdHeight();},
		parent: function(){return oFoxContrAd.getAdParent();},
		position: function(){return oFoxContrAd.getAdPosition();},
		width: function(){return oFoxContrAd.getAdWidth();},
		size: function(){return oFoxContrAd.getAdSize();}
	},
	setAdHeight: function(sAdHeight){gSAdHeight=sAdHeight;},
	setAdSize: function(sAdSize){gSAdSize=sAdSize;},
	setAdLoc: function(sAdLoc){gSAdLoc = sAdLoc;},
	setAdParent: function(oAdParent){gOAdParent=oAdParent;},
	setAdPosition: function(sAdPosition){gSAdPosition=sAdPosition;},
	setAdWidth: function(sAdWidth){gSAdWidth=sAdWidth;},
	setInfo: function(sInfo){sInfo = gSInfo;},
	setKeywords: function(sKeywords){gSKeywords = sKeywords;},
	setRScience: function(sRScience){gSRScience = sRScience;},
	setSiteZone: function(sSiteZone){gSSiteZone = sSiteZone;},
	setSValues: function(sSiteValues){gSValues = sSiteValues;},
	setTest: function(sTest){gSTest = sTest;},
	setTile:function(){gITile++;},
	setUrl:function(sUrl){gSUrl = sUrl;},
	setAdv:function(sAdv){gAdv = sAdv;}
}
