
function isDebugMode() {
	return (location.protocol == "file:" || location.host == "127.0.0.1");
}

function showDebugGoogleAd() {
	var tag = '<div style=\"border:1px solid gray;font-size:x-small;width:'+google_ad_width+'px;height:'+google_ad_height+'px\">Google ad</div>';
	document.write(tag);
}

function showFirefoxButton() {
	document.write('<div class=\"sidebar navlinks\">');
	document.write('<div style=\"position:relative; left:10px;\">');

		google_ad_client = "pub-0359998685168726";
		google_ad_width = 125;
		google_ad_height = 125;
		google_ad_format = "125x125_as_rimg";
		google_cpa_choice = "CAAQweaZzgEaCA2ZyYC_NXeAKK2293M";
		if (isDebugMode())
			showDebugGoogleAd();
		else
			document.write('<script type=\"text/javascript\"  src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\"></script>');

	document.write('</div>');
	document.write('</div>');
}


function setGoogleColors() {
	if (gCurrentContinent == INDEX_AFRICA) {
		google_color_border = "DDBB66";
		google_color_bg = "FFFCCC";
		google_color_link = "895B0F";
		google_color_url = "FF6600";
		google_color_text = "895B0F";
	}
	else
	if (gCurrentContinent == INDEX_SOUTHAMERICA) {
		google_color_border = "A7FFB1";
		google_color_bg = "EEFFF0";
		google_color_link = "006666";
		google_color_url = "0066FF";
		google_color_text = "006666";
	}
	else {
		google_color_border = "CCCCCC";
		google_color_bg = "EEFFF8";
		google_color_link = "0000FF";
		google_color_url = "666666";
		google_color_text = "000000";
	}
}


function showGoogleAd() {
	setGoogleColors();
	if (isDebugMode())
		showDebugGoogleAd();
	else
		document.write('<script type=\"text/javascript\"  src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\"></script>');
}


function showGoogleSquare() {
	google_ad_client = "pub-0359998685168726";
	google_ad_width = 336;
	google_ad_height = 280;
	google_ad_format = "336x280_as";
	google_ad_type = "text_image";
	google_ad_channel ="";

	showGoogleAd();
}


function showGoogleBanner(wide) {
	google_ad_client = "pub-0359998685168726";
	google_ad_width = (wide ? 728 : 468);
	google_ad_height = (wide ? 90 : 60);
	google_ad_format = (wide ? "728x90_as" : "468x60_as");
	google_ad_type = "text_image";
	google_ad_channel ="";

	showGoogleAd();
}


function showGoogleSkyscraper(wide) {
	google_ad_client = "pub-0359998685168726";
	google_ad_width = (wide ? 160 : 120);
	google_ad_height = 600;
	google_ad_format = (wide ? "160x600_as" : "120x600_as");
	google_ad_type = "text_image";
	google_ad_channel ="";

	showGoogleAd();
}


function showGoogleLeftMarginSkyscraper(wide) {
	document.write('<div style=\"position:relative; left:-5px;\">');
	showGoogleSkyscraper(wide);
	document.write('</div>');
}


function supportsMaxWidth() {
	// MacIE and WinIE6 don't support max-width
	var doesSupport = true;
	var ieVersion = /msie (\d\.\d*)/

	if (navigator.appName.toLowerCase().indexOf('microsoft') != -1) {
		if (navigator.appVersion.toLowerCase().indexOf('macintosh') == -1) {
			var findVersion = ieVersion.exec(navigator.appVersion.toLowerCase())
			if (findVersion) {
				if (parseFloat(findVersion[1]) < 7.0)  // there are claims that IE7 will support max-width and max-height
					doesSupport = false;
			}
		}
		else
			doesSupport = false;
	}
	return doesSupport;
}


var counter = 0;
function showGoogleSkyscraperAtLocation(loc) {
//	if ((++counter) > 3) return;		// Google doesn't want more than 3 ads per page

	if (isDebugMode()) {
		var tag = '<div style=\"border:1px solid grey;font-size:x-small;position:absolute;width:120px;height:600px;'+loc+'\">Google ad</div>';
		document.write(tag);
		return;
	}


	// Love these hacks...
	// WinIE doesn't position correctly the IFRAME generated by Google when it is inside
	// an absolutely positioned _generated_ DIV. Apparently, the only generated element
	// that will cause the IFRAME to display at the right place is another IFRAME. Great.
	// We pass as parameters the continent and the location so that Google displays the
	// right ads in the right colors.
	var file = getLangBaseUrl() + 'googleframe.php?continent=' + gCurrentContinent + '&caller=' + document.location;

	document.write('<iframe marginwidth=\"0\" marginheight=\"0\" frameborder=\"0\" scrolling=\"no\" src=\"'+file+'\" style=\"position:absolute;width:120px;height:600px;'+loc+'\">');
	document.write('</iframe>');

}

function hasSmallScreen() {
	return (screen.availWidth <= 800);
}

function showGoogleTopRightSkyscraper() {
	if (hasSmallScreen()) return;

	var loc = "";
	if (supportsMaxWidth()) {
		loc = "top:81px;left:830px;";
	}
	else {
		// We have to do some weird gymnastics for the slideshow index page with WinIE.
		// The BODY position becomes absolute otherwise the Google ad can cover the table
		// (but then, we have to exchange margins/paddings for the sidebarContainer).
		// The H1 width becomes 100% to avoid a funny bug when the mouse cursor is over a slideshow and the map is displayed.
		var elt = (document.getElementsByTagName("h1"))[0];
		if (elt) {
//			elt.style.border 		= "1px solid blue";
			elt.style.width 		= "100%";
		}
		var elt = (document.getElementsByTagName("body"))[0];
		if (elt) {
//			elt.style.border 		= "2px solid blue";
			elt.style.position		= "absolute";

			elt.style.marginLeft	= "0";
			elt.style.marginTop		= "0";

			elt.style.paddingLeft	= "20px";
			elt.style.paddingTop	= "15px";;
			elt.style.paddingRight	= "140px";

		}
		loc = "top:81px;right:10px;";
	}
	showGoogleSkyscraperAtLocation(loc);
}


function toPrint() {
	return (paramVal("print") == "1");
}

function showGoogleJournalEntrySkyscraper() {
	if (hasSmallScreen()) return;
	if (toPrint()) return;

	var loc = "";
	if (supportsMaxWidth()) {
		loc = "margin-top:20px;left:830px;";
	}
	else {
		var elt = (document.getElementsByTagName("body"))[0];
		if (elt) { elt.style.marginRight = "140px"; }
		loc = "margin-top:0px;right:10px;";
	}
	showGoogleSkyscraperAtLocation(loc);
}
