var Pollen = {
	studio: null,
	map: null,
	icon: null,
	marker: null,
	directionsPanel: null,
	directions: null,

	debug: function(str) {
		var console = window.console || false;
		if(console) {
			if(console.debug) {
				console.debug(str);
			} else if(console.log) {
				console.log(str);
			}
		}
	},

	init: function() {
		Pollen.studio = new GLatLng(53.98633287286662, -0.7842577993869781);
	},


	load: function() {
		// Initialise the map data
		Pollen.map = new GMap2(
			document.getElementById("map"),
			{mapTypes:[G_HYBRID_MAP, G_NORMAL_MAP]}
		);
		with(Pollen.map) {
			addControl(new GSmallZoomControl());
			// addControl(new GMapTypeControl());
			enableContinuousZoom();
			enableScrollWheelZoom();
		}
		Pollen.icon = new GIcon(G_DEFAULT_ICON, '../images/marker.png');
		with(Pollen.icon) {
			shadow = '../images/marker_shadow.png';
			iconSize = new GSize(38,26);
			shadowSize = new GSize(52,27);
			iconAnchor = new GPoint(5, 26);
			transparent = null;
			mozPrintImage = null;
			printImage = null;
			printShadow = null;
		}
		Pollen.marker = new GMarker(Pollen.studio, {icon: Pollen.icon})
		// Set the map to its initial state
		Pollen.resetMap(Pollen.map);

		Pollen.directionsPanel = document.getElementById("map_directions_text");
		Pollen.directions = new GDirections(Pollen.map, Pollen.directionsPanel);
		with(GEvent) {
			addListener(Pollen.directions, 'error', Pollen.directionsUnsuccessful);
			addListener(Pollen.directions, 'load', Pollen.directionsSuccessful);
		}
	},

	Google: {
		// Geocodes a UK location by performing a Google local search and taking the
		//     location of the first result.
		// 
		// The search is asynchronous and the result will be passed as a GLatLng to
		//     the supplied callback function. null will be passed if no matching result
		//     could be found.
		geocode: function(place, callback) {
			Pollen.debug("Geocoding");
			var localSearch = new GlocalSearch();
			localSearch.setSearchCompleteCallback(null,
				function() {
					if (localSearch.results[0]) {
						var resultLat = localSearch.results[0].lat;
						var resultLng = localSearch.results[0].lng;
						var point = new GLatLng(resultLat,resultLng);
						callback(point);
					} else {
						callback(null);
					}
				}
			);

			localSearch.execute(place + ", UK");
		}
	},

	// Recentre the map on Pollen Studio's location
	resetMap: function() {
		Pollen.debug("Resetting map");
		with(Pollen.map) {
			if(Pollen.directions) {
				Pollen.directions.clear();
			}
			checkResize();
			setCenter(Pollen.studio, 16);
			setMapType(G_HYBRID_MAP);
			removeOverlay(Pollen.marker);
			addOverlay(Pollen.marker);
			// checkResize();
		}
	},
	
	// Expand the small map to fill a bigger space. Also moves other elements out of
	// the way. Calls the callback function when finished
	expandMap: function(callback) {
		Pollen.debug("Expanding map");
		// Need a bigger container to fit the expanded map and directions panel
		$("#map_container").css({width: "870px", marginLeft: "-590px"});

		// a) Grow the map to a bigger size
		$("#map").animate(
			{ width: "500px", height: "440px"},
			800,
			null,
			function() { Pollen.map.checkResize(); if(callback){callback()} }
			// Also make the directions panel the same height (although it should still
			//       be 0-width)
		).siblings('#map_directions').animate({ height: "450px" }, 800);

		// b) Push the left-hand side down so there's no overlap
		// 
		// This selector deals with the case when we've got errors from a form
		//     submission on screen
		$(".errorExplanation,#nature").eq(0).animate({ marginTop: "500px" }, 800);
	},

	// Shrink the map back into its small spot, and hide the directions panel if
	// required
	contractMap: function(callbackFunc) {
		Pollen.debug("Contracting map");
	 // If someone clicks on a directions waypoint the infowindow opens and
	 // doesn't close again by itself even when directions.clear is called
		Pollen.map.closeInfoWindow();
		Pollen.hideDirectionsPanel(function() {
			$("#map_directions").animate({ height: "200px" }, 500)
			$("#map").animate({ width: "270px", height: "190px" }, 500, null, function() {
				Pollen.resetMap();
				$("#map_container").css({width: "280px", marginLeft: "0px"});
				if(callbackFunc){callbackFunc();}
			});

			// This selector deals with the case when we've got errors from a form
			//     submission on screen
			$(".errorExplanation,#nature").eq(0).animate({ marginTop: "0px" }, 500, null, function() {
			});
		});
	},
};

Pollen.init();
$(function() {
	$("#directions_form").submit(function() {
		Pollen.getDirectionsFrom(this.elements['saddr'].value);
		return false
	})
})