//############ GOOGLE ADSENSE ########################
function google_afs_request_done(google_ads) {
	google_ads_request_done(google_ads);
}

function google_ad_request_done(google_ads) {
	google_ads_request_done(google_ads);
}

function google_ads_request_done(google_ads) {
	if (google_ads.length == 0) {
		return;
	}
	
	var feedback_link = typeof(google_info) == "undefined" ? "http://www.google.com/adsense" : google_info.feedback_url;
	
	$("div.sponsoredLinks p").each(function(){
		$(this).append('<a rel="external" href=\"' + feedback_link  + '\">Ads by Google</a>');
	});
	
	$(google_ads).each(function(){
		if(this.type=="text/wide" || this.type=="text"){
			insertAnAd("wideAds",buildAnAd(this));
		}
		else {
			insertAnAd("narrowAds",buildAnAd(this));
		}
	});
	
}

function insertAnAd(cssClass, string){
	var targetDiv = $("div."+cssClass)[0];
	if (targetDiv != null){
		$(targetDiv).parent().show();
		$(targetDiv).replaceWith(string);
	}
}

function buildAnAd(googleAd)
{
	var s = '';
	s += '<div><p class="title"><a rel="external" href="' +
	googleAd.url + '" onmouseout="window.status=\'\'" onmouseover="window.status=\'go to ' +
	googleAd.visible_url + '\';return true"> ' +
	googleAd.line1 + '</a></p> <p>' +
	googleAd.line2 + ' ' +
	(typeof(googleAd.line3) != "undefined" ? googleAd.line3 : "") +
	'</p> <p><a rel="external"  href="' +
	googleAd.url + '" onmouseout="window.status=\'\'" onmouseover="window.status=\'go to ' +
	googleAd.visible_url + '\';return true">' +
	googleAd.visible_url + '</a></p></div>';
	return s;
}

var lp_test_channel = null;
function setupGoogleAdTest(percentageTrafficTested) {
	// Make sure test is only applied to a certain percentage of visitor
	var isApplyTest = Math.round(Math.random() * 100) <= percentageTrafficTested;
	if (!isApplyTest) {
		lp_test_channel = null;
		return;
	}
	
	// Test channel is defined as style1, style2, style3, and style4
	var random = Math.round(Math.random() * 3) + 1;
	var testChannel = 'style' + random;
	lp_test_channel = testChannel;
	
	if (google_ad_channel) {
		google_ad_channel = google_ad_channel + ' ' + testChannel;
	}
	
	$('#mainContent div.sponsoredLinks, #contentBody div.sponsoredLinks').addClass(testChannel);		
}
