var Map = {
	map: null,
	icon: new Hash(),
	markers: new Hash(),
	info_windows: new Hash(),
	current_latlng: new google.maps.LatLng(53.0, -3),
	current_zoom: 6,
	
	initialiseMap: function () {
		IE6 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 6;
	  if (IE6){
			$("map").insert("<img src=\"/images/ie6_locations.jpg\" />");
			$('map_elements').show();
			$('loading_notice').remove();
		} else {
		  var myOptions = {
		    zoom: Map.current_zoom,
		    center: Map.current_latlng,
		    mapTypeId: google.maps.MapTypeId.HYBRID,
				mapTypeControl: false,
				navigationControl: true,
				navigationControlOptions: {
					style: google.maps.NavigationControlStyle.SMALL
				}
		  }
		  Map.map = new google.maps.Map($("map"), myOptions);
			Map.icon.set('wind_farm', new google.maps.MarkerImage('/images/google_icons_95.png', new google.maps.Size(10, 10), new google.maps.Point(32,0), new google.maps.Point(5,5)));
			Map.icon.set('bio_power', new google.maps.MarkerImage('/images/google_icons_95.png', new google.maps.Size(10, 10), new google.maps.Point(48,0), new google.maps.Point(5,5)));
			Map.icon.set('wco_collection_point', new google.maps.MarkerImage('/images/google_icons_95.png', new google.maps.Size(10, 10), new google.maps.Point(0,0), new google.maps.Point(5,5)));
			Map.icon.set('genset', new google.maps.MarkerImage('/images/google_icons_95.png', new google.maps.Size(10, 10), new google.maps.Point(16,0), new google.maps.Point(5,5)));
		
			Map.initialiseIcons();
		}
	},
	
	initialiseIcons: function() {
		Map.requestIcons();
		$('map_elements').show();
		$('loading_notice').remove();
	},

	requestIcons: function(e, d) {
		var url = '/locations/find';
		new Ajax.Request(url, {
			method: 'get',
			onSuccess: function(transport) {
				Map.loadIcons(transport.responseJSON);
			}
		});
	},

	loadIcons: function(d) {
		d.each(function(l){
			split = l.location.latlng.split(':');
			latlng = new google.maps.LatLng(split.first(), split.last());
			if (l.location.location_type == 'wco_collection_point') {
				infoContent = '<h4>' + l.location.name + '</h4>' + '<p>' + l.location.address + '<br />' + l.location.city + '<br />' + l.location.county + '<br />' + l.location.postal_code + '</p>';
			} else {
				infoContent = '<h4>' + l.location.name + '</h4>' + '<p><strong>Number of units:</strong> ' + l.location.no_of_units + '</p>' + '<p><strong>Capacity:</strong> ' + l.location.power_output + '</p>';
			}
			infoContent += '<h5><a href="#" onclick="Map.show_location()" params="' + l.location.latlng + '">Take me to this location</a></h5>';
			
			Map.info_windows[l.location.id] = new google.maps.InfoWindow({
				content: infoContent
			});

			Map.markers[l.location.id] = new google.maps.Marker({
				map: Map.map,
				icon: Map.icon.get(l.location.location_type),
				position: latlng,
				title: l.location.name
			});

			google.maps.event.addListener(Map.markers[l.location.id], 'click', function() {
				Map.info_windows[l.location.id].open(Map.map, Map.markers[l.location.id]);
			});
		});
	},
	
	show_location: function() {
		Map.current_zoom = Map.map.getZoom();
		Map.current_latlng = Map.map.getCenter();
		
		e = Event.findElement(event, 'a');
		split = e.readAttribute('params').split(':');
		latlng = new google.maps.LatLng(split.first(), split.last());
		Map.map.setZoom(16);
		Map.map.panTo(latlng);
		
		e.replace('<a href="#" onclick="Map.reset()" params="' + e.readAttribute('params') + '">Back to previous view</a>');
	},
	
	reset: function() {
		e = Event.findElement(event, 'a');
		Map.map.setZoom(Map.current_zoom);
		Map.map.panTo(Map.current_latlng);
		
		e.replace('<a href="#" onclick="Map.show_location()" params="' + e.readAttribute('params') + '">Take me to this location</a>');
	}
}

Event.observe(window, 'load', function() {
	Map.initialiseMap();
});