///////////////////////////////////////////////////////////
// Class GoogleMap
///////////////////////////////////////////////////////////


var GoogleMap = Class.create({

	///////////////////////////////////////////////////////////
	// initialize
	///////////////////////////////////////////////////////////

	initialize:function(parent, element){
		
		this.parent = parent;
		this.element = element;
		
		this.markers = [];
		this.markersDict = [];
		this.markerInfo = [];
		this.currentZoom = null;
		
		this.infoWindowTemplate = new Template('<div class="infoWindow"><h3>#{title}</h3><p>#{country}<br/>#{date1}#{date2}#{time}<br/>#{contact}#{phone}</p></div>');
  
		this.setupMap();
	},


	///////////////////////////////////////////////////////////
	// setup map
	///////////////////////////////////////////////////////////	
	
	setupMap:function(){
		
		
		var l = this.parent.locations.length;
		var obj, marker;
		
		this.markerInfo= this.parent.locations.clone();
		
		if(GBrowserIsCompatible()){
			this.map = new GMap2(this.element);
			this.map.setCenter(new GLatLng(51.50393, 3.886506), 6);
			this.map.setUIToDefault();
			//this.map.setMapType(G_PHYSICAL_MAP);
			
			this.map.enableDoubleClickZoom();
	        
			this.mgr = new MarkerManager(this.map);
			this.setupMarkers.bind(this).delay(0);
	
			Event.observe(window, 'unload', GUnload);
			
		}
	},


	///////////////////////////////////////////////////////////
	// set the markers
	///////////////////////////////////////////////////////////	

	setupMarkers:function(){
	    
		var l = this.markerInfo.length;
		var n = l;
		var p, m, icon, html;
		
		while(l--){
			
			p = this.markerInfo[l];
			
			icon = new GIcon();
			
			icon.image = '/static/images/marker.png';
			icon.shadow = "/static/images/marker_shadow.png";
			icon.iconSize = new GSize(12, 20);
			icon.shadowSize = new GSize(22, 20);
			icon.iconAnchor = new GPoint(6, 6);
			icon.infoWindowAnchor = new GPoint(2, 26);
			
			// make new marker
			m = new GMarker(new GLatLng(p.lat, p.lng), icon);
			m.params = p;
			m.params.parent = this;
			
			if(m.params.date1){ var date1 = parseDate(m.params.date1);  }
			if(m.params.date2){ var date2 = parseDate(m.params.date2);  }
			
		    m.params.country = (m.params.country !== '') ? '<strong>Country:</strong> '+ m.params.country : '';
		    m.params.date1 = (m.params.date1 !== '') ? '<br /><strong>From:</strong> '+ date1 : '';
		    m.params.date2 = (m.params.date2 !== '') ? '<br /><strong>To:</strong> '+ date2 : '';
	        m.params.time = (m.params.time !== '') ? '<br /><strong>Time:</strong> '+ m.params.time : '';
	        m.params.contact_email = (m.params.contact_email !== '') ? '<br /><strong>E-mail:</strong> <a href="mailto:'+ m.params.contact_email +'" title="E-mail: '+ m.params.contact_email +'">'+ m.params.contact_email +'</a>' : '';
	        m.params.phone = (m.params.phone !== '') ? '<br /><strong>Phone:</strong> '+ m.params.phone : '';
			
			// keep a reference
			this.markersDict[p.id] = this.markers.length;
			this.markers.push(m);
			
			this.mgr.addMarker(m, 0);
			
			// listen for click
			GEvent.addListener(m, "click", function(){
			    html = this.params.parent.infoWindowTemplate.evaluate(this.params);
				this.openInfoWindowHtml(html);
			});
		}
		
		// let the world know the map is ready
		this.element.fire('map:ready');

	},
	

	///////////////////////////////////////////////////////////
	// update the map
	///////////////////////////////////////////////////////////	
	
	update:function(){
		
		// show all markers
		if(!this.currentZoom){
			this.map.setCenter(new GLatLng(0, 0), 0);
			var latlngbounds = new GLatLngBounds();
			var l = this.markers.length;
			while(l--){
			    latlngbounds.extend(this.markers[l].getLatLng());
			}
			this.map.setCenter( latlngbounds.getCenter( ), this.map.getBoundsZoomLevel( latlngbounds ) );
			
			this.setTitle('Showing all events on the map')
			
		// zoom in on country	
		}else{
			this.map.setZoom(this.currentZoom.zoom);
			this.map.zoomIn(new GLatLng(this.currentZoom.lat, this.currentZoom.lng), true, true);
			this.setTitle('Zoomed in on '+ this.currentZoom.country +' <img src="/static/images/map_quit_results.gif" style="vertical-align:middle;" alt="" onclick="locations.map.resetZoom();"/>')
		}
	},


	///////////////////////////////////////////////////////////
	// zoomCountry
	///////////////////////////////////////////////////////////	

	zoomCountry:function(obj){
		this.currentZoom = obj;
		this.update();
	},
	

	///////////////////////////////////////////////////////////
	// setTitle
	///////////////////////////////////////////////////////////	

	setTitle:function(msg){
		this.element.previous().update(msg);
	},


	///////////////////////////////////////////////////////////
	// zoomCountry
	///////////////////////////////////////////////////////////	

	resetZoom:function(){
		this.currentZoom = null;
		this.update();
	}
});
