/*
	Heimatkult JavaScript functions
	Code: Niels Heidenreich 2008-2011
	Requires jQuery
*/

function load_google_maps_api() {
	var key;
	switch (location.host) {
		case "www.heimatkult.org":
			key = "SIh08HlWisEGhxUJLXUDNDULNJkxSWQ6cjo6tN_zOlqBGJlZ_GcpxDTg";
			break;
		case "heimatkult.org":
			key = "RDjkZEKaM2gvJkcMHPdyeKssHmthRan55EmPLr97ctCbFRFLSh3y7yKQ";
			break;
		case "www.nordstadtleben.org":
			key = "SqkrNqzqN2hYJwWjr57v0aosbD6RTHRacbXV2_s0Wnmzq2SlRYDwP6LQ";
			break;
		case "nordstadtleben.org":
			key = "QImcka8wFl4nlTb9F7uKP8uGdOPBQxQ8QVJCAeHE1LcsEbm3uX2wK_aw";
			break;
		case "editor.heimatkult.org":
			key = "SUluxhPkGOqATGl6C7WG28BnIA6xS4MetKsyLU7CKoqhuJzFAZdn0i-A";
			break;
		case "editor.nordstadtleben.org":
			key = "SLyS5o8H85_V6TY8SX02AlMjsDtxTa8Ogf0Fenn04RVgCAoe2rdoZ2Gg";
			break;
		case "test.heimatkult.org":
			key = "TQWSjde0RwDFzYj3HV_bu2YBIruRQAynSlDMd7Euyo6mOmBE8c-uv6CQ";
			break;
		case "test.nordstadtleben.org":
			key = "SP89c1T6E_nGNL2V31crZZrkHrTBToAv4sUaWYsjuHSOSVlYjBOUSL1Q";
			break;
		case "192.168.2.104":	// silly but no way around it right now
			key = "RiN9bou4Z7JnBKp1jdtVBct2eSehSV951XaWyqWK1nlA27odmnq_Shug";
			break;
		default:	// ngin.de and local
			key = "RO7o21DYoqoVhlHladcWj7KHm3wBTCXFP2-UK2iKy74YC4bsldTrf0fg";
	}
	document.write('<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAZVK7wsaUAzqNSEbcs81CmB'
		+ key + '" type="text/javascript"></script>');
}


// Create the Google Map for the ad pages
function gm(lat, lng, info) {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map"));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
		var latlng = new GLatLng(lat, lng);
		map.setCenter(latlng, 16);
		var marker = new GMarker(latlng);
		GEvent.addListener(marker, "click", function() {
			map.openInfoWindowHtml(latlng, info);
		});
		map.addOverlay(marker);
	}
}

// Do all this once the DOM is ready (page fully loaded)
$(document).ready(function() {

	// Placeholder for search field
	$('#search-query').focus(function() {
		if ($(this).attr("value") == "Finden")
			$(this).attr("value", "");
	});
	$('#search-query').blur(function() {
		if ($(this).attr("value") == "")
			$(this).attr("value", "Finden");
	});

	// Make external links open in a new window while using valid XHTML
	// http://dererumnatura.us/archives/2008/02/external-linkin.html
	$("a[rel=external]").each(function() {
		this.target = "_blank";
	});


	// Fade the opener images
	$(".imagefader").innerfade({
		speed: 2000,
		timeout: 5000,
		containerheight: "255px"
	});

	// Make GoogleMap (ad pages only!)
	var title = $('#map').attr("title");
	if (title != undefined) {
		var gmdata = title.split("|");
		var info = gmdata[0].bold() + "<br />" + gmdata[1].replace(/,/g, "<br />");
		$("#map").css("display", "block");
		$("#map").attr("title", "Google-Map");
		gm(parseFloat(gmdata[3]), parseFloat(gmdata[2]), info);
	}

	// Does this event actually fire?
	$("body").unload(function() {
		GUnload();
	});
});

