/* 
 * Google Maps generator
 */
var gMapGenerator = {

	startZoom: 11,
	centreLat: null,
	centreLong: null,
	mapWidth: 400,
	mapHeight: 400,
	gMaps: new Array(),
	gMarkers: new Array(),
	polygon: new GPolygon([
		new GLatLng(54.610951,-2.817993),
		new GLatLng(54.590168,-2.839108),
		new GLatLng(54.583404,-2.815247),
		new GLatLng(54.567882,-2.831039),
		new GLatLng(54.500452,-2.866917),
		new GLatLng(54.492277,-2.862625),
		new GLatLng(54.48081,-2.919445),
		new GLatLng(54.497761,-2.973862),
		new GLatLng(54.514107,-3.010941),
		new GLatLng(54.556336,-3.017635),
		new GLatLng(54.570669,-3.032227),
		new GLatLng(54.592754,-3.001328),
		new GLatLng(54.633412,-2.999783),
		new GLatLng(54.63679,-2.980556),
		new GLatLng(54.634207,-2.954636),
		new GLatLng(54.641459,-2.922192),
		new GLatLng(54.637585,-2.896442),
		new GLatLng(54.639572,-2.87035),
		new GLatLng(54.615698,-2.835031),
		new GLatLng(54.610951,-2.817993)
	],'#999999',3,0.8,'#cccccc',0.6),

	init: function(gMapWidth, gMapHeight, zoom, gMapType){
		var self = this;
		if(!GBrowserIsCompatible()) return false;
		// Looks for DOM nodes of class "googlemap" to create Google Maps in
		$(".googlemap").each(function(n){
			var gMapId = n;
			// Remove any images that may be there for progressive enhancment
			$(this).children('img').remove();
			// Look for nodes carrying the Centre Map Lat Long data
			var centreLat = $(this).find('.latitude').text();
			var centreLng = $(this).find('.longitude').text();
			// Create the map
			var gMap = self.buildGMap(this,centreLat, centreLng, G_PHYSICAL_MAP);
			// Add an polygon overlay if the DOM node has class "area"
			if($(this).hasClass('area')) self.addArea(gMap);
			// Look for DOM nodes with class "gmarker" and add markers
			// to the gMarkers array from the data they contain
			$('.gmarker').each(function(n){
				self.setMarker(this, gMapId);
			});
			// Add the new Googlemap to the gMaps array
			self.gMaps[gMapId] = gMap;
		})
		// Add the markers in the gMarkers array to each of the maps in the gMaps array
		for(mapId in this.gMaps){
			for(mrkId in this.gMarkers[mapId]){
				this.addMarker(mapId,mrkId);
			}
		}
	},

	buildGMap: function(domNode, ctrLat, ctrLng, type){
		var gMap = new GMap2(domNode);
		var location = new GLatLng(ctrLat,ctrLng);
		gMap.setMapType(type);
		gMap.addControl(new GSmallZoomControl());
		gMap.setCenter(location,this.startZoom);
		return gMap;
	},

	addArea: function(gMap){
		gMap.addOverlay(this.polygon);
	},

	setMarker: function(node, index){
		var self = this;
		if(typeof this.gMarkers[index] == 'undefined') this.gMarkers[index] = new Array();
		var marker = {
			Lat: $(node).children('span.latitude').text(),
			Lng: $(node).children('span.longitude').text(),
			Txt: $(node).find('.gtext a').get(0),
			Node: node
		};
		this.gMarkers[index].push(marker);
	},

	addMarker: function(gMapId,gMarkerId){
		var self = this;
		var marker = this.gMarkers[gMapId][gMarkerId];
		var gMarker = new GMarker(new GLatLng(marker.Lat,marker.Lng));
		var blueIcon = new GIcon(G_DEFAULT_ICON);
		blueIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png";

		GEvent.addListener(gMarker,'click',
			function(){
				gMarker.openInfoWindow(marker.Txt);
			});
		$(marker.Node).children('.gtext').hover(
			function(){
				self.gMaps[gMapId].panTo(new GLatLng(marker.Lat,marker.Lng));
				gMarker.setImage("/workspace/css_images/marker-highlight.png");
			},
			function(){
				gMarker.setImage(gMarker.getIcon().image);
			});
		this.gMaps[gMapId].addOverlay(gMarker);
	}

}
$(function() {
		if($('body').hasClass('archive')) $("#archive").tabs();
	});
$(function(){
	$(window).unload(GUnload);
	gMapGenerator.init();
});

