/* convenience functions */
var log = function(msg){
	if(window.console) {
		console.log(msg);
	} else {
		var ft = document.getElementById("ft");
		ft.innerHTML=msg;
	}
}

function initialize() {
	var latlng = new google.maps.LatLng(10, -20);
	var myOptions = {
		zoom: 2,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.TERRAIN
	};
	
	var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	
	var set_pins = function(pin, coords, city) {
		var image = 'http://www.jimmybyrum.com/images/pin.png';
		var pinlatlon = new google.maps.LatLng(coords[1], coords[0]);
		var marker = new google.maps.Marker({
			position: pinlatlon,
			map: map,
			icon: image,
			title: city
		});
		var infolatlon = new google.maps.LatLng(coords[1], coords[0]+10);
		var infowindow = new google.maps.InfoWindow({
			content: "<strong>"+city+"</strong>"
		});
		google.maps.event.addListener(marker, 'click', function() {
			infowindow.open(map, marker);
		});
		$(pin).click(function() {
			infowindow.open(map, marker);
		});
	}

	$("#locations li").each(function(i) {
		var city = $(this).find("em").html();
		var coords = $(this).find("span").html();
		var pin = $(this).find("em:first");
		coords = eval(coords);
		set_pins(pin, coords, city);
	});
}

// hide all locations
$("#locations h2").next().hide();
// loop through location titles and attach and event to show/hide the locations for that region on click
$("#locations h2").click(function() {
	$(this).next().is(":hidden") ? $(this).next().slideDown("fast") : $(this).next().slideUp("fast");
});

// this page would be a bit boring without a map
initialize();

