/* Logic for generating a dart tag. Documentation stripped from this version.
 * See AdProviderDART.php for more info
 */ 

AthenaDART = {};
AthenaDART.sites = {
	'Auto' : 'wka.auto',
	'Creative' : 'wka.crea',
	'Education' : 'wka.edu',
	'Entertainment' : 'wka.ent',
	'Finance' : 'wka.fin',
	'Gaming' : 'wka.gaming',
	'Green' : 'wka.green',
	'Humor' : 'wka.humor',
	'Lifestyle' : 'wka.life',
	'Music' : 'wka.music',
	'Philosophy' : 'wka.phil',
	'Politics' : 'wka.poli',
	'Science' : 'wka.sci',
	'Sports' : 'wka.sports',
	'Technology' : 'wka.tech',
	'Test Site' : 'wka.test',
	'Toys' : 'wka.toys',
	'Travel' : 'wka.travel'};

AthenaDART.callAd = function (slotname, size, network_options){
	// Hack for dart sizes 
	switch (size){
	  case '300x250': size = '300x250,300x600'; break;
	  case '728x90': size = '728x90,468x60'; break;
	  case '160x600': size = '160x600,120x600'; break;
	}

	var url = 'http://ad.doubleclick.net/' + 
		AthenaDART.getAdType() + '/' +
		AthenaDART.getDARTSite(Athena.getPageVar('hub')) + '/' +
		AthenaDART.getZone1(Athena.getPageVar('wgDBname')) + '/' +
		AthenaDART.getZone2() + ';' +
		's0=' + AthenaDART.getDARTSite(Athena.getPageVar('hub')).replace(/wka\./, '') + ';' +
		's1=' + AthenaDART.getZone1(Athena.getPageVar('wgDBname')) + ';' +
		's2=' + AthenaDART.getZone2() + ';' +
		'@@WIKIA_PROVIDER_VALUES@@' +
		AthenaDART.getArticleKV() +
		AthenaDART.getDomainKV(Athena.getPageVar('hostname')) +
		'AQ=@@WIKIA_AQ@@;' +
		'pos=' + slotname + ';' +
		AthenaDART.getDcoptKV(slotname) +
		"sz=" + size + ';' +
		AthenaDART.getTileKV(slotname) +
		"ord=@@WIKIA_RANDOM@@?";

	url = url.replace(/@@WIKIA_AQ@@/, Athena.getMinuteTargeting());
	url = url.replace(/@@WIKIA_RANDOM@@/, Athena.random());
	url = url.replace(/@@WIKIA_PROVIDER_VALUES@@/, ProviderValues.string);
	Athena.dartUrl = url;
	Athena.debug("Dart URL = " + Athena.dartUrl, 4);
	return '<script type="text/javascript" src="' + url + '"></script>';
};

AthenaDART.getAdType = function(){
	// Someday we may want to change this dynamically.
	return 'adj';
};

AthenaDART.getDARTSite = function(hub){
	if(AthenaDART.sites[hub] != undefined){
		return AthenaDART.sites[hub];
	} else {
		return 'wka.wikia';
	}
};

// Effectively the dbname, defaulting to wikia.
AthenaDART.getZone1 = function(dbname){
	// Zone1 is prefixed with "_" because zone's can't start with a number, and some dbnames do.
	if (dbname == '' ){
		return '_wikia';
	} else {
		return '_' + dbname.replace('/[^0-9A-Z_a-z]/', '_');
	}
};

// Page type, ie, "home" or "article"
AthenaDART.getZone2 = function(){
	if(Athena.getPageVar('isMainPage') == 'true') {
		return 'home';
	} else {
		return 'article';
	}
};

AthenaDART.getTileKV = function (slotname){
	switch(slotname) {
		case 'TOP_RIGHT_BOXAD': return 'tile=1;';
		case 'TOP_LEADERBOARD': return 'tile=2;';
		case 'LEFT_SKYSCRAPER_1': return 'tile=3;';
		case 'LEFT_SKYSCRAPER_2': return 'tile=3;';
		case 'LEFT_SKYSCRAPER_3': return 'tile=6;'; 
		case 'FOOTER_BOXAD': return 'tile=5;';
		case 'HOME_TOP_RIGHT_BOXAD': return 'tile=1;';
		case 'HOME_TOP_LEADERBOARD': return 'tile=2;';
		case 'HOME_LEFT_SKYSCRAPER_1': return 'tile=3;';
		case 'HOME_LEFT_SKYSCRAPER_2': return 'tile=3;';
		default: return '';
	}
};

AthenaDART.getDcoptKV = function(slotname){
	switch (slotname){
		case 'TOP_LEADERBOARD': return 'dcopt=ist;';
		case 'HOME_TOP_LEADERBOARD': return 'dcopt=ist;';
		default: return '';
	}
};

// Title is one of the always-present key-values
AthenaDART.getArticleKV = function(){
	if (Athena.getPageVar('article_id') != ''){
		return "artid=" + Athena.getPageVar('article_id') + ';';
	} else {
		return '';
	}
};


AthenaDART.getDomainKV = function (hostname){
	var lhost, pieces, sld, np;
	lhost = hostname.toLowerCase();

	pieces = lhost.split(".");
	np = pieces.length;

	if (pieces[np-2] == 'co'){
		// .co.uk or .co.jp
		sld = pieces[np-3] + '.' + pieces[np-2] + '.' + pieces[np-1];
	} else {
		sld = pieces[np-2] + '.' + pieces[np-1];
	}

	if (sld != ''){
		return 'dmn=' + sld.replace(/\./g, '_') + ';';
	} else {
		return '';
	}

};
