
BBC.adverts = function() {

	/*
	 
	 	an overly long list of variables - could well be reduced
	 
	// */

	var
	UNDEFINED    = "undefined",
	KEY_VALUES   = "keyValues",
	EMPTY        = "",
	SLASH        = "/",
	SEMI         = ";",
	EQUALS       = "=",
	DISPLAY_NONE = "bbccom_display_none",
	
	SCRIPT_START = "<script type=\"text/javascript\" src=\"",
	SCRIPT_END   = "\"></script>",

	SCRIPT_ROOT  = "/js/app/bbccom/",
	SCRIPT_TEST  = "bbccom_capable",
	JS           = ".js",

	LOCATION     = "location",
	DOMAIN       = "domain",
	ADS_BLOCKED  = true,
	ZONE_VERSION = "zoneVersion",
	ZONE_OVERRIDE= "zoneOverride",

	YES          = "yes",
	NO           = "no",

	ID_START     = "bbccom_",

	BBC_ORD      = "&ord=",

	DC_BASE      = "http://ad.doubleclick.net/adj/",
	DC_BASE_COMPANION      = "http://ad.doubleclick.net/pfadx/bbccom.live.site.news/;tile=6;sz=512x288;dcgzip=0",
	DC_PREROLL_BASE = "http://ad.doubleclick.net/pfadx/",
	
	DC_SLOT      = ";slot=",
	DC_SZ        = ";sz=",
	DC_TILE      = ";tile=",
	DC_ORD       = ";ord=",
	DC_END       = "?",
	AD_LABEL	 = "<div class=\"bbccom_text\">Advertisement</div>",

	AD_INFO      = {
		leaderboard : {tile: 1, size: "728x90"},
		skyscraper : {tile: 2, size: "160x600"},
		bottom : {tile: 3, size: "468x60"},
		mpu250x250  : {tile: 4, size: "250x250"},
		mpu  : {tile: 4, size: "300x250"},
		mpu120x240  : {tile: 4, size: "120x240"},
		button : {tile: 5, size: "120x240"},
		companion: {tile: 6, size: "512x288"}
	};

	//	create unique id
	var ordLength = 14,	ord = EMPTY;
	
	var pageVersion = undefined, SITEVERSION = "4";
	var cssPostFix = ["_v4","_v3_5","_v3"];
	var mastHeadPresent = false;

	while(ordLength--) ord += (Math.floor(Math.random() * 10));

	//	define data objects
	var config = {}, zoneData = {};


	/*				

		Public methods	
	
	//	*/
	
	
	/*				

		getCustomKeyValues
		
		Returns a string containing the keyValues relating to the current slot as parsed from the zonefile. 
		@param: slot [string]
	
	
	//	*/
	var getCustomKeyValues = function(slot) {
		
		var customKeyValues = [];
		var len = 0;

		if(zoneData.keyValues) {
			for(var key in zoneData.keyValues) {
				customKeyValues[len] = SEMI;
				customKeyValues[len + 1] = key;
				customKeyValues[len + 2] = EQUALS;
				customKeyValues[len + 3] = zoneData.keyValues[key];

				len += 4;
			};
		};
		
		return customKeyValues.join(EMPTY);
	};


	/*				

		getAdvertTag
		
		Returns a script tag, as a string, containing the information parsed from the zonefile. 
		@param: slot [string]
	
	
	//	*/
	var getAdvertTag = function(slot)	{
	
	
	if(slot=="companion"){
		
		
		return [
				DC_PREROLL_BASE,
				zoneData.site,
				SLASH,
				zoneData.zone,
				getCustomKeyValues(slot),
				DC_SLOT,
				slot,
				DC_SZ,
				AD_INFO[slot].size,
				DC_TILE,
				AD_INFO[slot].tile
						
			].join(EMPTY);
				
				
				
	} else {
	
		
		
		
		return [
				SCRIPT_START,
				DC_BASE,
				zoneData.site,
				SLASH,
				zoneData.zone,
				getCustomKeyValues(slot),
				DC_SLOT,
				slot,
				DC_SZ,
				AD_INFO[slot].size,
				DC_TILE,
				AD_INFO[slot].tile,
				DC_ORD,
				ord,
				DC_END,
				SCRIPT_END
			  ].join(EMPTY);
			  
			  
		}

	};


	/*				

		hide
		
		Forces an advert to stay hidden from the document flow.  
		@param: slot [string]
	
	//	*/
	var hide = function(slot) {
		config[slot] = NO;
		document.getElementById(ID_START + slot).className = DISPLAY_NONE;
	};


	/*				

		loadTestFile
		
		Loads the bbccom_capable.js into the head of the document.
		If this does not laod no more advert code is run
		
		a failover protection method
	
	//	*/
	var loadTestFile = function() {
		
		var str = [
					SCRIPT_START,
					SCRIPT_ROOT,
					SCRIPT_TEST,
					JS,
					DC_END,
					ord,
					SCRIPT_END,
				].join(EMPTY);
						
		document.write(str);

	};

	
	/*				

		loadZonefile
		
		Loads the zonefile into the head of the document.
		Overwrites the src attribute with an absolute url if provided in the config
	
	//	*/
	var loadZonefile = function() {
		
		var src = config[ZONE_OVERRIDE] === true ? config[ZONE_VERSION] : SCRIPT_ROOT + config[ZONE_VERSION] + JS;  
		var str = [
					SCRIPT_START,
					src,
					SCRIPT_END
				].join(EMPTY);
						
		document.write(str);
	};
	


	/*				

		configure
		
		Binds the initial setup parameters into the config array
		@param: data [object]
		
	
	//	*/
	var configure =  function(data) {
		for(var id in data) config[id] = data[id];
	};

	/*				

		Public methods	
	
	//	*/
	
	return {


		/*				
	
			init
			
			Initialises object with set up parameters. Should be called from initialise.inc
			@param: data [object]
			
			Wrapper function that calls
				configure
				loadTestFile
				loadZoneFile
		
		//	*/
		init: function(data) {
			configure(data);
			loadTestFile();
			loadZonefile();
		},


		
		/*				
	
			isCapable
			
			Method that turns off ad blocking. All ads are blocked unless this methos is executed
			
			This called from the file bbccom_capable.js which is loaded from the method loadTestFile#
		
		
		//	*/
		isCapable: function () {
			ADS_BLOCKED = false;
		},
	
		
		/*				
	
			getConfig
			
			Used for unit testing of config values.  
		
		//	*/
		getConfig: function(key) {
			return config[key];
		},
	

		/*				
	
			getAdvertTag
			
			Used for unit testing of zonefile value  
		
		//	*/
		getAdvertTag: function(slot) {
			return getAdvertTag(slot);
		},


		
		/*				
	
			setZone
			
			parses the zone file against the documents location and domain.  
			@param: zones [object]
			
			Extracts the relevant ad campaign data from the zonefile and sets the value of variable zoneData
			This recursively loops through the zone file, testing the page domain and url against against each zone layer.
			
			It returns the last match found.
			
		
		//	*/
		setZone: function (zones) {
			
			var url  = config[LOCATION];
			var site = config[DOMAIN];
			var data = {keyValues:{}};
			
			var process = function(base, level) {

				for(var key in level.data) {
					if(key === KEY_VALUES) {
						for(var kw in level.data.keyValues)	{
							data.keyValues[kw] = level.data.keyValues[kw];
						};
					}
					else {
						data[key] = level.data[key];
					};
				};

				if(level.zones)	{
					var ct = level.zones.length;
					while(ct--)	{
						if(url.indexOf(base + level.zones[ct].uri) !== -1) {
							return process(base + level.zones[ct].uri, level.zones[ct]);
						};
					};
				};
	
				return data;
			}
	
			zoneData = zones.process(process(EMPTY, zones.zones), site, url);
		},
	
	
		/*				
	
			write
			
			Writes a script tag to the page.  
			@param: slot [string]
			
			Does nothing if ADS_BLOCKED === true
			Does nothing if zoneData.ads is not set
			
			This function is called from within the actual ad includes
				ad1.inc
				ad2.inc
				ad3.inc
				ad4.inc
			
		
		//	*/
		write: function(slot) {
			if(ADS_BLOCKED === false && zoneData.ads) {
				config[slot] = YES;
				document.write(AD_LABEL + getAdvertTag(slot));
			}
			else hide(slot);
		},
	

		/*				
	
			show
			
			Removes the classname from the advert div
			@param: slot [string]
			
			Does nothing if slot is blocked
			Otherwise effectively adds the div to document body, as it has a display: none property setting on page load

			This function is called from within the actual ad includes
				ad1.inc
				ad2.inc
				ad3.inc
				ad4.inc
		
		//	*/
		show: function (slot,newClass)	{
			if (config[slot] === YES) {
				
				if(mastHeadPresent && pageVersion === SITEVERSION){
					newClass = slot + cssPostFix[0];
				}
				else if(mastHeadPresent && pageVersion !== SITEVERSION){
					newClass = slot + cssPostFix[1];
				}
				else{
					newClass = slot + cssPostFix[2];
				}
	
				document.getElementById(ID_START + slot).className = newClass;
			};
		},
		
		
		/*				
	
			close
			
			sets the config value of the slot to false. Effectively meaning that ad will never be displayed upon the current page.
			@param: slot [string]
			
			This function is called on response from double click if no advert has been sold for this slot
			Which is why you will not find any refernce to it elsewhere
			
			
		//	*/
		close: function(slot) {
			config[slot] = NO;
		},
		
		/*
		 * getIsHomePage
		 * Allows leaderboard to find out if this is a homepage
		 */
		getIsHomePage: function(){
			return config["isEditionHomePage"];
		},
		
		/*
		 * setMastHead
		 */
		setPageVersion: function(version){
			mastHeadPresent = true;
			if(version === "4"){
				pageVersion = version;
			}
			
		},
	
		/*				
	
			empCompanion
			
			Returns the doubleclick url for the preroll advert used in the Embedded Media Player.

			This is called from the fmtj_emp.js file object
			
		//	*/
		empCompanion: function() {
		

			
			var prerollURL = getAdvertTag("companion");
			
			return prerollURL;

		},
		
		/*				
	
			empCompanionResponse
			
			Writes the advert script tag for the emp companion banner ad into the page
			@param: src [string: url]
			
			This called by the function syncRoadBlock defined in the companion.inc ad file
			syncroadBlock is called from the Embedded Media Player
			
		//	*/
		empCompanionResponse: function(src) {
			
			
		
			var slot = "companion";
			config[slot] = YES;
			
			var companionHolderDiv = document.createElement("div");
			companionHolderDiv.setAttribute("class","comp_banner_holder")		
			var companionHolder = document.createElement("iframe");
			companionHolder.setAttribute("width","300");
			companionHolder.setAttribute("height","100%");
			companionHolder.setAttribute("align","left")
			companionHolder.setAttribute("scrolling","no");
			companionHolder.setAttribute("frameBorder","no");
			companionHolder.setAttribute("src",src)		
			var parentDiv = document.getElementById("bbccom_companion");
			parentDiv.setAttribute("class","bbccom_visibility_show");
			companionHolderDiv.appendChild(companionHolder)
			parentDiv.appendChild(companionHolderDiv);
			
			this.show(slot);
		}
		
	};

}();


