ads = function () {
	// public properties
  this.order = Math.random()*10000000000000;
  this.zone = 'other';
  this.keyvalues = '';
	// can't use site as a variable as it is overwritten...
	this.tgsite = 'P4681.topgear.com/';
	this.dcsite = 'http://ad.uk.doubleclick.net/adj/';
	
	// default properties
	var DC_UK = 'http://ad.uk.doubleclick.net/adj/';
	var DC_AU = 'http://ad.au.doubleclick.net/adj/';
	var UK_SITE = 'P4681.topgear.com/';
	var AU_SITE = 'topgearau/';
	var type = 'type=';
	var priceMin = 'minval=';
	var priceMax = 'maxval=';
	var kw = 'kw=';
	var tags = '';
	var carbar =  '';
	var cbmake = 'cbmake=';
	var cbmodel = 'cbmodel=';
	
	// private properties
  var loc = window.location.pathname;
  var host = window.location.hostname + '';
  // Note: applicationRoot is a global variable rendered to page
  // Below strips the applicationRoot for the purposes of 
  // extracting the zone/keyword from. 
  if (loc.indexOf(applicationRoot) === 0) {
    loc = loc.substring(applicationRoot.length - 1);
  }

	var locItems = loc.split('/');
	var pageName;
	var body = document.getElementsByTagName('body')[0];
	var bodyClass = body.className;
	var reviewSection = false;
	var query = '';
	
	
	setTerritory();
	setZonePageName();
	setKeywords();
	setCarBar();
	
	
	function setTerritory() {
		// assign the site url depending on whether we are in the UK or AU
		var territory = applicationRoot;
		// make sure the geoIpTerritory is actually defined!
		// this helps if the blog changes are not launched yet!
		if (typeof geoIpTerritory !== 'undefined') {
			territory = '/' + geoIpTerritory + '/';
		}
		if (territory === '/uk/') {
			this.dcsite = DC_UK;
			this.tgsite = UK_SITE;
		} else if (territory === '/au/') {
			this.dcsite = DC_AU;
			this.tgsite = AU_SITE;
		}
	};
	
	function setZonePageName() {
		// zones & pagenames
		// blogs
	  if (host.indexOf('blogs.topgear.com') !== -1) {
			this.zone = 'blogs/' + host.split('.')[0];
		// homepage
		} else if (loc.indexOf('Error.aspx') !== -1) {
			this.zone = '';
		} else if (loc === '/' || loc === '/uk' || loc === '/us' || loc === '/au') {
	    this.zone = 'homepage';
			pageName = 'homepage';
	// car chooser
		} else if (body.id === 'car-chooser') {
			this.zone = 'car-chooser';
		
	// car reviews subsections
	  } else if (loc.indexOf('road-test') !== -1) {
			pageName = 'road-test';
			this.zone = 'car-reviews';
		} else if (loc.indexOf('related-stuff') !== -1) {
			pageName = 'related-stuff';
			this.zone = 'car-reviews';
		} else if ((bodyClass.indexOf('specs') !== -1) || (bodyClass.indexOf('details-spec') !== -1)) {
			pageName = 'specs';
			this.zone = 'car-reviews';
		} else if (bodyClass.indexOf('verdict') !== -1) {
			pageName = 'tg-verdict';
			this.zone = 'car-reviews';
		} else if (bodyClass.indexOf('manufacturer') !== -1) {
			pageName = 'manufacturer';
			this.zone = 'car-reviews';
		} else if (bodyClass.indexOf('three-sixty') !== -1) {
			pageName = 'three-sixty';
			this.zone = 'car-reviews';
		} else if (bodyClass.indexOf('photos') !== -1) {
			pageName = 'photos';
			this.zone = 'car-reviews';
	
		// if we are on the auto trader pages
		} else if (loc.indexOf('cars-for-sale') !== -1) {
			pageName = 'trader';
			this.zone = 'trader';

	// all other sections
	  } else {
	    this.zone = locItems[1];
			if (locItems.length > 2) {
				pageName = locItems[locItems.length -1];
			} else if (locItems.length === 2) {
				pageName = 'homepage';
			}
		}

		// if we were in the car reviews section
		// we need to introduce some extra logic
		// trader is in car-reviews, but has a different zone
		if (this.zone === 'car-reviews' || this.zone === 'trader') {
			setCarreviewSection();
		}
	
		// if we are in the parking lot
		if (this.zone === 'car-chooser') {
			if (loc.indexOf('parkinglot') !== -1) {
				setParkingLotSection();
			}
		}
		
		// if we are on the search page
		if (this.zone === 'search') {
			setSearchWords();
		}
	};
	
	function setCarreviewSection() {
		if (locItems[1] && locItems[2]) {
			// add the make to the zone
			this.zone += '/' + locItems[1];

			// add the model key-value
			this.keyvalues += formatKeyValue('model', locItems[2]);
		}
	};
	
	function setParkingLotSection() {
		var queries, query, i;
		queries = querySplitter(location.search);
		for (i = 0; i < queries.length; i++) {
			query = queries[i];
			if (query.indexOf('cartypelist') !== -1) {
				this.keyvalues += formatKeyValue('type', query.split('=')[1]);
			}

			else if (query.indexOf('pricerangemin') !== -1) {
				this.keyvalues += formatKeyValue('minval', query.split('=')[1]);
			}
		
			else if (query.indexOf('pricerangemax') !== -1) {
				this.keyvalues += formatKeyValue('maxval', query.split('=')[1]);
			}
		}
	};
	
	function setKeywords() {
		var i;
		var tags = $('meta[name="Keywords"]').attr('content');

		if (tags) {
			tags = cleanValue(tags);
			tags = tags.split(',');
			for (i = 0; i < tags.length; i++) {
				this.keyvalues += formatKeyValue('tag', tags[i]);
			}
		}
	};
	
	function setCarBar() {
		var carbar, c, cbmake, cbmodel,	i;
			
		carbar = $('meta[name="carbar"]').attr('content');
		if (carbar) {
			carbar = carbar.split(';');
		
			for (i = 0; i < carbar.length; i++) {
				c = carbar[i].split(',');
				cbmake = cleanValue(c[0]);
				cbmodel = cleanValue(c[1]);
			
				this.keyvalues += formatKeyValue('cbmake', cbmake);
				this.keyvalues += formatKeyValue('cbmodel', cbmodel);
			}
		}
	};

	function setSearchWords() {
		var queries, query, i, j;

		queries = querySplitter(location.search);
		for (i = 0; i < queries.length; i++) {
			if (queries[i].indexOf('phrase') !== -1) {
				query = getQueryValue(queries[i]);
				kw = query.split('+');
				for (j = 0; j < kw.length; j++) {
					this.keyvalues += formatKeyValue('kw', kw[j]);
				}
			}
		}
	};
	
	// if there is a pagename, add it to the keyvalues
	if (pageName) {
		this.keyvalues += formatKeyValue('pagename', pageName);
	}
	
	// check whether we are on the uat site & add keyvalue if we are
	if (host.indexOf('uat.') === 0) {
		this.keyvalues += formatKeyValue('test', 'uat');
	}
	
  //	clean up the strings
	this.zone += ';';
	this.keyvalues = this.keyvalues.toLowerCase();
	this.zone = this.zone.toLowerCase();
	
	/**
	 * takes a querystring & returns an array of the queries
	 * @private
	 * @param {String} query query of a url
	 * @returns {Array} individual queries
	 */
	function querySplitter(query) {
		if (query.indexOf('?') === 0) {
			query = query.substring(1); // removes the leading question mark
		}
		query = query.toLowerCase();
		return query.split('&');
	};
	
	function getQueryValue(query) {
		return query.split('=')[1];
	};
	
	function formatKeyValue(key, value) {
		value = cleanValue(value);
		var retVal = key + '=' + value + ';';
		return retVal.toLowerCase();
	};
	
	function cleanValue(value) {
		value = trim(value);
		value = commaSpacesToComma(value);
		value = spacesToDashes(value);
		value = plusesToDashes(value);
		return value;
	};
	
	function plusesToDashes(value) {
		return value.replace('+', '-', 'gi');
	};
	
	function trim(value) {
		return value.replace(/^\s+|\s+$/gi, '');
	};
	
	function spacesToDashes(value) {
		return value.replace(/ /gi, '-');
	};
	
	function commaSpacesToComma(value) {
		return value.replace(/\s*,\s*/gi, ',');
	};
		
	
	// make the object publicly available via 'ads'
  return this;

}();
