// Ad module

Manager.addMethods({
	'Ad': new Module('Ad', 'this module handles all add display functions')
});

makeOrd = function() {	
	var ord = Math.round(10000000*Math.random())
	while (ord.length<9){
		ord="0"+ord
	}
	return ord;
}

USN.Ad.site = USN.site;
USN.Ad.zone = USN.zone;
USN.Ad.ord = makeOrd();
USN.Ad.tile = 1;


USN.Ad.positionMap = $H({
	leaderboardA: {dim: '728x90' },
	leaderboardB: { dim: '468x60' },
	badgeA:	{dim: '180x90'},
	badgeB: {dim: '120x90'},
	badgeC: {dim: '120x90'},
	badgeD: {dim: '120x90'},
	badgeE: {dim: '120x90', 'deferred': true},
	rectangleA: {dim: '300x250'},
	rectangleB: {dim: '300x250'},
    rectangleC: {dim: '300x150'},
	skyscraperA: {dim: '160x600'},
	skyscraperB: {dim: '160x600'},
	skyscraperC: {dim: '160x600'},
	verticalA: {dim: '120x240'},
	video1: {dim: '468x60'},
	boxA: {dim: '300x125'},
	boxB: {dim: '330x140'}
})

USN.Ad.getSafeString = function(str, len) {
	var str_len = (arguments.length == 2) ? len : 32;
	return str.split(':').last().replace(/[^A-Za-z]+/g, '').substring(0,str_len).toLowerCase();
}

USN.Ad.checkReferer = function() {
	var refs = $H({
		'aol.money' : /^http:\/\/money\.aol\.com/
	});
	var exclusions = $H({
		'aol.money': "aolmoney"
	});
	var exclusion = false
	refs.each( function(ref) {
		ref_id = ref[0];
		regexp = ref[1];
		if (document.referrer.match(regexp)) {
			exclusion = "!c=" + exclusions.get(ref_id) + ";";
		}
	});
	return exclusion;
}

USN.Ad.checkKeys = function() {
	var meta_keys = $$('meta').findAll( function(tag) { return tag.name === "keys" } );
	this.keyObj = {};
	this.keyCheckDone = true;
	for (i = 0; i < meta_keys.length; i++) {
		content = $H(meta_keys[i].content.evalJSON());
		this.keyObj[content.keys()[0]] = content.values()[0]
	}
}


USN.Ad.make = function(adPosition) {
	if (!this.keyCheckDone) { this.checkKeys();}
	//var ord = Math.round(10000000*Math.random())
	//while (ord.length<9){
	//	ord="0"+ord
	//}
	var ord = this.ord;
	var tile = this.tile;
	this.tile++;
	//#FIXME: raise exception if adPosition not in positionMap
	position = this.positionMap.get(adPosition)
	position = $H(position);
	if (position.keys().include('deferred') && position.get('deferred')) {
		return;
	}
	//take everthing after last ":", remove any digits and whitespace at start of string and use only first 64 chars 
	this.site = this.getSafeString(this.site, 28);
	this.zone = this.getSafeString(this.zone);
	exclusion = this.checkReferer();
	exclusion = (exclusion) ? exclusion : "";
	if (this.keyObj) {
		var kv = "";
		$H(this.keyObj).each( function(pair) {
			kv += ";" + pair.key + "=" + pair.value;
		});
	}
	//#FIXME: need to handle keywords too
	adURI = new Template("http://ad.doubleclick.net/adj/usn.#{site}/#{zone};sz=#{dim};tile=#{tile};pos=#{position};#{exclusion}ord=#{ord}#{kv}?").evaluate({'site': this.site, 'zone': this.zone, 'dim': position.get('dim'), 'position': adPosition, 'exclusion': exclusion, 'ord': ord, 'tile': tile, 'kv': kv});
	adURI = encodeURI(adURI)
	document.write("<script type=\"text/javascript\" src=\""+adURI+"\"><\/script>");
	return adURI;


}
//Preserves backward-compatibility by aliasing dblclick to the 'make' method of the Ad module
dblclick = USN.Ad.make.bind(USN.Ad);
