// NOTE: Requires coda.js

function createBlogDispatcher (root, channel, pseudoFile) {
	
	var docPath = location.pathname;
	docPath = docPath.replace(/\/page\/\d+/, '');
	var pageName = docPath;
//	var subSite = location.host.replace(/^(?:origin.)?([^\.]+)\..*/, '$1');
//	if (subSite == 'www') subSite = 'blog';
	
	if (!pageName.match(/\.[^\/]$/)) {
                if (pageName.charAt(pageName.length-1) != '/') pageName += '/';
                pageName += pseudoFile;
        }

	var blogRollup = '';

	if (is404) {
		// not found
		pageName = '/notfound' + pageName;
		blogRollup = '404';
	} else if (location.search.match(/\?s=/)) {
		// search
		var blogSearch = location.search.replace(/\?s=([^\&]+).*/, '$1');
		blogSearch = blogSearch.replace(/\+/g, ' ');
		blogSearch = unescape(blogSearch);
		blogSearch = blogSearch.replace(/^ */, '').replace(/ *$/, '');
		pageName = '/search/' + blogSearch + '/' + pseudoFile;
		blogRollup = 'SEARCH';
	} else if (location.search.match(/akst_action=share-this/)) {
		// share this
		pageName = '/share-this/' + pseudoFile;
		blogRollup = 'SHARE-THIS';
	} else if (docPath == '/') {
		pageName = pseudoFile;
		blogRollup = 'INDEX';
	} else if (docPath.match(/^\/\d{4}-\d{2}-\d{2}\/.+/)) {
		// blog post
		var postName = pageName.replace(/^\/\d{4}-\d{2}-\d{2}\/([^\/]+).*/, '$1');
		pageName = '/post/' + postName + '/' + pseudoFile;
		blogRollup = 'POST: ' + postName;;
	} else if (docPath.match(/^\/\d{4}\//)) {
		// date based archive
		var arcDate = docPath.replace(/^\//, '');
		arcDate = arcDate.replace(/\/$/, '');
		arcDate = arcDate.replace(/\//g, '-');
		blogRollup = 'ARCHIVE: ' + arcDate;
	} else if (docPath.match(/^\/tag\//)) {
		// tag
		blogRollup = 'TAG: ' + pageName.replace(/^\/tag\/([^\/]+).*/,'$1');
	} else if (docPath.match(/^\/utag\//)) {
		// uma tag
		var matches = pageName.match(/\/utag\/([^\/]+)\/([^\/]+)\/(\d+)/);
		blogRollup = 'TAG: ' + matches[1].toUpperCase() + ': ' + matches[2] + '#' + matches[3];
		pageName = pageName.replace(/\/utag\//, '/tag/');
	} else {
		// unknown type of page
		blogRollup = 'OTHER: ' + pageName.replace(/.*\/([^\/]+)\/[^\/]+$/, '$1');
	}

	if (pageName.charAt(0) == '/') pageName = pageName.substring(1);

	dispatcher = new Object();
	dispatcher.pageName =      pageName;
	dispatcher.hier1 =         pageName;
	dispatcher.channel =       'bestweekever';
	dispatcher.currencyCode =  'USD';
	dispatcher.prop35 =        blogRollup;
	
	return dispatcher;
}

function configureBlogAdManager(po, dispatcher, pseudoFile) {
	
	var pseudoRE = new RegExp(pseudoFile+"$");
	var adPath = dispatcher.pageName.replace(pseudoRE,'').replace(/-/g,'_');
	var rollup = dispatcher.prop35.split(/: /);
	var sitesection = rollup[0];

	po.contentType = "adj"
	po.keyValues = "!category=blog;";

	if (sitesection == 'INDEX') {
		adPath = append_part(adPath, '_hp');
	} else if (sitesection != 'POST') {
		adPath = append_part(adPath, '_mn');
	}

	if (sitesection == 'TAG') {
		po.keyValues += "!category=btag;";
	} 

	adPath = adPath.replace(/\/tag/, '');

	po.sections = append_part('bestweekever', adPath);
}

function append_part(left, right) 
{
	if (left.charAt(left.length-1) != '/' && right.charAt(0) != '/') left += '/';
	return left + right;
}

function place_leaderboard()
{
	if (location.search.indexOf('disable_ads=all') == -1 && location.search.indexOf('disable_ads=leaderboard') == -1) {
		place_any("728x90");
	}
}

function place_box() {
	if (location.search.indexOf('disable_ads=all') == -1 && location.search.indexOf('disable_ads=box') == -1) {
		place_any("300x250");
	}
}

function place_sky() {
	if (location.search.indexOf('disable_ads=all') == -1 && location.search.indexOf('disable_ads=sky') == -1) {
		place_any("160x600");
	}
}

function place_any(size) {
	if (have_placed_ad == false) {
		have_placed_ad = true;
		
		//commented out because Flux libraries somehow hate this
		firstAd = cloneObject(com.vh1.bestweekever.adParamsObj);
		
		firstAd.keyValues += "Dcopt=ist;";
		firstAd.size = size;
		com.mtvi.ads.AdManager.placeAd(firstAd);
	} else {
		com.vh1.bestweekever.adParamsObj.size = size;
		com.mtvi.ads.AdManager.placeAd(com.vh1.bestweekever.adParamsObj);
	}
}

com.mtvi.reporting.Account={
	name:'viabestweekever',
	dynamicAccountSelection:'true',
	dynamicAccountList:'viavh1scandalistdev=bestweekever-d,bestweekever-q',
	registerLinks:'true',
	dartSite:'vh1.vt'
};


function cloneObject(o) {
	x = new Object();
	for (i in o) {
		x[i] = o[i];
	}
	return x;
}

com.vh1 = new Object();
com.vh1.bestweekever = new Object();
com.vh1.bestweekever.adParamsObj = new Object();

var have_placed_ad = false;

var blogName = 'bestweekever';
var pathRoot = '/' + blogName;
var pseudoFile = 'index.html';

var dispatcher = createBlogDispatcher(pathRoot, blogName, pseudoFile);

configureBlogAdManager(com.vh1.bestweekever.adParamsObj, dispatcher, pseudoFile);

com.mtvi.ads.AdManager.setPositionThreshold(2);
com.mtvi.reporting.Controller.initialize();
com.mtvi.reporting.Controller.sendCall(dispatcher);
