
// Convert a three letter country-code from the geo-targetting system
// to an YahooSm specific geographic region.
function getYahooSmGeoRegion(countryCode) {
	switch (countryCode) {
		case 'gbr':
			return 'uk';
		case 'usa':
		case 'can':
			return 'us';
		case 'fra':
			return 'fr';
		case 'deu':
			return 'de';
	}
	return 'row';
}

// Create Url to request that will provide YahooSm ad data.
function buildYahooSmUrl(yahooSmCatId, countryCode, isLiveProd) {
	var geo = getYahooSmGeoRegion(countryCode);

	var market = geo;
	if (market == 'row') {
		market = 'uk';
	}

	var url = 'http://cmhtml.overture.com/d/search/p/guardian/js/' + market + '/ctxt/';

	url += '?maxCount=3&amp;keywordCharEnc=latin1&amp;outputCharEnc=latin1&amp;adultFilter=clean';

	url += '&amp;mkt=' + market;

	if (isLiveProd) {
		url += '&amp;Partner=' + 'guardian_js_' + geo + '_ctxt';
		url += '&amp;type=' + yahooSmCatId;
	} else {
		url += '&amp;Partner=guardian_js_uk_ctxt_test';
	}

	url += '&amp;ctxtId=' + 'guardian_' + geo + '_' + yahooSmCatId;

	// Cache busting parameter. Generate some randomness.

	url += '&amp;cb=' + (new Date()).getTime();

	return url;
}

// Create Url to request that will provide Hotspots data.
function buildHotspotsUrl(yahooSmCatId, countryCode, isLiveProd) {
	var geo = getYahooSmGeoRegion(countryCode);

	var url = 'http://cmhtml.uk.overture.com/d/search/p/standard/eu/js/flat/ctxt/ls/';

	url += '?ctxtId=' + 'guardian_' + geo + '_' + yahooSmCatId;

	if (isLiveProd) {
		url += '&amp;Partner=' + 'guardian_js_' + geo + '_linkspot_implem';
	} else {
		url += '&amp;Partner=guardian_js_uk_ctxt_test';
	}

	url += '&amp;NGrp=1';

	url += '&amp;NKw=5';

	url += '&amp;Pg=1';

	// Cache busting parameter. Generate some randomness.
	url += '&amp;cb=' + (new Date()).getTime();

	return url;
}

// Replace the portion of the string after last space before the trimPoint with ellipses
function trimAndEllipses(string, trimPoint) {
	if (string.length > trimPoint) {
		var spaceBeforeTrimPoint = string.lastIndexOf(" ", trimPoint);

		var trimmedString = string.substring(0, spaceBeforeTrimPoint);

		return trimmedString.concat("...");
	} else {
		return string;
	}
}

// YahooSmData constructor.
function YahooSmData(title, description, siteHost, clickUrl) {
	this.title = title;
	this.description = description;
	this.siteHost = siteHost;
 	this.clickUrl = clickUrl;

	if (this.siteHost.indexOf('www.') == 0) {
		this.trimmedHost = this.siteHost.substring(4);
	} else {
		this.trimmedHost = this.siteHost;
	}

	// Trim overly long descriptions
	this.description = trimAndEllipses(this.description, 60);

	return 0;
}

// Convert the raw YahooSm data which is a single long list into
// a more understandable list of YahooSmData objects.
function parseYahooSmArray(yahooSmData) {
	var newArray = new Array();
	var counter = 0;
	var title = '';
	var description = '';
	var siteHost = '';
	var clickUrl = '';

	// Skip over the first five elements which are the first ad
	// because this is an YahooSm house ad which we do not want to display.
	for (var i=6; i < yahooSmData.length; i++) {

		switch (counter) {
			case 0:
				description = yahooSmData[i];
				break;
			case 1:
				break;
			case 2:
				clickUrl = yahooSmData[i];
				break;
			case 3:
				title = yahooSmData[i];
				break;
			case 4:
				siteHost = yahooSmData[i];
				break;
			case 5:
				var yahooSmObj = new YahooSmData(title, description, siteHost, clickUrl);
				newArray[newArray.length] = yahooSmObj;
				counter = -1;
		}

		counter++;
	}
	return newArray;
}

// Takes parsed data and returns an HTML string representation.
function renderYahooSmHtml(parsedYahooSmData, hotspotsData) {
	var html = '<div class="capsule">';
	if (parsedYahooSmData.length > 0 || hotspotsData.length > 0) {
		html += '<h3>Advertiser links</h3>';

		html += '<ul class="results">';
		for (var i=0; i < parsedYahooSmData.length; i++)
		{
			var data = parsedYahooSmData[i];
			html += '<li><h4><a href="' + data.clickUrl + '">' + data.title + '</h4>';
			html += '<p>' + data.description + '</p>';
			html += '<p><a href="' + data.clickUrl + '">' + data.trimmedHost + '</a></p></li>';
		}
		html += '</ul>';

		// Now do hotspots.
		html += '<ul class="links">';
		for (var i=0; i < hotspotsData.length; i++) {
			var keywords = hotspotsData[i].keywords.split(", ");
			for (var j = 0; j < keywords.length; j++) {
				html += '<li>';
				if (i > 0 || j > 0) html += ' |';
				html += ' <a href="http://uk.search.yahoo.com/search?fr=cb-guardian&amp;p=' + escape(keywords[j]) + '">';
				html +=keywords[j];
				html += '</a></li>';
			}
		}
		html += '</ul></div>';
	}
	return html;
}

// Main entry point - handles the data using above functions, and does the actual insert into the page.
function renderYahooSm(yahooSmData, hotspotsData) {
	// Check we've got somewhere to render into.
	var yahooSmDiv = document.getElementById("yahoosm");
	var yahooSmLoaderDiv = document.getElementById("yahoosm_loader");
	if (! yahooSmDiv || ! yahooSmLoaderDiv) {
		return;
	}

	// Parse data.
	var parsedYahooSmData = parseYahooSmArray(yahooSmData);

	// Display.
	var html = renderYahooSmHtml(parsedYahooSmData, hotspotsData);
	yahooSmLoaderDiv.innerHTML = renderYahooSmHtml(parsedYahooSmData, hotspotsData);
	moveContents(yahooSmLoaderDiv, yahooSmDiv);
}
