var map;

$(document).ready(function(){
	$.show_directions_map();
});

$(window).load(function(){
	$('#callout').even_heights();
});

jQuery.show_directions_map = function() {
	if(document.getElementById('map')) {
		var latlng = new google.maps.LatLng(0, 0);
		map = new google.maps.Map(document.getElementById("map_canvas"), {
			zoom: 12,
			center: latlng,
			disableDefaultUI: true,
			mapTypeId: google.maps.MapTypeId.ROADMAP      
		});
		$.create_map_controls();

		var icon = image = "/wordpress/wp-content/themes/findingflow/images/maps/marker.png";
		var geocoder = new google.maps.Geocoder();
		var bounds = new google.maps.LatLngBounds();
		
		//var addresses = [{ 'address': "300 Preston St, Ottawa, ON, Canada", 'notes': "(located inside Moksha Yoga Ottawa on the 2nd floor)"}, { 'address': "2589 Traverse Drive, Ottawa, ON", 'notes': "" }];
		var addresses = [{ 'address': "300 Preston St, Ottawa, ON, Canada", 'notes': "(located inside Moksha Yoga Ottawa on the 2nd floor)"}];
		
		$.each(addresses, function(){
			var location = this;
			geocoder.geocode({'address': location.address.toString()}, function(results, status) {
				if (status == google.maps.GeocoderStatus.OK) {
					var marker = new google.maps.Marker({
						position: results[0].geometry.location,
						map: map,
						icon: icon,
						title: location.address.toString()
					});
					bounds.extend(marker.position);
					
					var content = '<div class="window">' + 
						'<h3>Finding Flow Wellness Center</h3>' + 
						'<p>'+location.address.toString()+'</p>' + 
						'<p>'+location.notes.toString()+'</p>' + 
						'</div>';
					var infowindow = new google.maps.InfoWindow({ content: content });
					
					google.maps.event.addListener(marker, 'click', function() {
						infowindow.open(map,marker);
					});
				} else {
					alert("Geocoder failed due to: " + status);
				}
				map.setCenter(bounds.getCenter());
			});
		});
	}
}

jQuery.create_map_controls = function() {
	$('#mapcontrols button').click(function() { return false; });				
	$('#centerButton').click(function() { map.panTo(bounds.getCenter()); });
	$('#upButton').click(function() { map.panBy(0,-20); });
	$('#downButton').click(function() { map.panBy(0,20); });
	$('#rightButton').click(function() { map.panBy(20,0); });
	$('#leftButton').click(function() { map.panBy(-20,0); });
	$('#zoomInButton').click(function() { map.setZoom(map.zoom + 1); });
	$('#zoomOutButton').click(function() { map.setZoom(map.zoom - 1); });
}

jQuery.fn.even_heights = function() {
	$(this).find('> div')
		.height($(this).innerHeight())
		.css('position', 'relative')
		.find('a.moreinfo')
			.css({
				'position': 'absolute',
				'bottom': 0,
				'width': '100%'
			});
}