//  GigPressExtensions
//
//  Created by Cristobal Dabed on 2009-10-05.
//  Copyright (c) 2009 Creuna AS http://creuna.no
var GigPressExtensions = function($){
	
	// == Private Helper Variables
	
	// 1. Our Map Reference
	var map = null;
	var markers = {};
		
	var current_gmarker = null;
	var current_marker = null;
	var marker_event = false;
	

	// == Private Methods
	
	/**
	 * Add GMarker listener
	 *	Using this way of binding, so that IE does bind all gmarkers events to the last assigned events.
	 *
	 * @param gmarker The Google Map Marker
	 * @param marker	Internal Marker Object Representation.
	 */
	var adGMarkerListener = function(gmarker, marker){
		GEvent.addListener(gmarker, "click", function(){
				pushMarkersForEvent(gmarker, marker);
				map.setCenter(marker['point'], marker['zoom']);
		});
	};
	
	/**
	 * Push Markers For Event
	 */
	var pushMarkersForEvent = function(gmarker, marker){
		current_gmarker = null; // reset then store.
		current_gmarker = gmarker;
		current_marker = null; // reset then store.
		current_marker = marker;
		marker_event = true;
	};

	/**
	 * Pop Markers From Event
	 */	
	var popMarkersFromEvent = function(){
		marker_event = false;
		// current_gmarker = null; // reset then store.
		current_marker = null;
	};

	/**
	 * Listen on Map Move End
	 *	if there is any marker event initialized.
	 *	Show the Info Window for this object.
	 */		
	var mapMoveEnd = function(){
		if(marker_event == true){
			current_gmarker.openInfoWindowHtml(createHTMLForInfoWindow(current_marker['showdata']));			
			popMarkersFromEvent();
		}
	};
	
	/**
	 * Create HTML for info window
	 * 	
	 * 	Creates the html for display in the info window based on the showdata given.
	 *
	 *	@param showdata The showdata to create the html for.
	 */
	var createHTMLForInfoWindow = function(showdata){
		var html = '<h3>' + showdata['venue']  + (showdata['city'] != '' ?  ' <span class="city">' + showdata['city'] + '</span>' : '') + (showdata['country'] != '' ? ' <span class="country">[' + showdata['country'] + ']</span>' : '') +'</h3>';
		html += '<p><span class="gigpress-street">' + showdata['title'] +'</span></p>';
		html += '<p><span class="gigpress-date">' + showdata['date'] +'</span></p>';
		html += '<p><span class="gigpress-tickets-link">' + showdata['ticket_link'] + '</span></p>';
		html += '<p class="google-link"><a onclick="window.open(this.href); return false;" href="http://maps.google.com/maps?&amp;q=' + showdata['address'] + '">Open in Google &raquo;</a></p>';
		return html;
	};
	
	// == Public Methods
	return {
		
		/**
		 * Initialize Google Map
		 * 	Called from the google.setOnLoadCallback();
		 */ 
		initializeGoogleMap: function(){
			var container = $("#content #gigpress-map").get(0);
		  var size = new GSize(488, 488);
		  var latLng = new GLatLng(52.1873, 5.36126);
			try {
				map = new GMap2(container, {'size': size});
			}catch(e){
				alert("couldn't load map");
				return;
			}
			map.setCenter(latLng, 3);
			map.enableDoubleClickZoom(); 
			
			// 1. Add Map Control
			// 2. Add Map Type Control, switch between Map|Sattelite|Hybrid.
			// 3. Enable Scroll Wheel Zooming
			map.addControl(new GLargeMapControl3D());
			map.addControl(new GMapTypeControl());
			//  map.removeMapType(G_HYBRID_MAP);
			
			map.enableScrollWheelZoom();
			GEvent.addListener(map, 'moveend', mapMoveEnd);
			
			var gmarker;
			for(address in markers){
				marker = markers[address];
				marker['point'] = new GLatLng(marker['coordinates']['lat'], marker['coordinates']['lng']);
				marker['zoom'] = zoom	 =  13; // (marker['coordinates']['accuracy'] < 8 ? 9 : 13);
				
				gmarker = new GMarker(marker['point'], {'title': marker['venue']});
				adGMarkerListener(gmarker, marker);
				map.addOverlay( gmarker );
				
				marker['gmarker'] = gmarker;
				markers[address] = marker;
			}
			$(window).unload( function (){
				GUnload(); // Prevent Memory Leaks Especially IE, @url http://code.google.com/apis/maps/documentation/index.html#Memory_Leaks.
			});
		},
		
		/**
		 * Add a Marker to the markers list.
		 *
		 * @param address The address to add for.
		 * @param coordinates The coordinates object{lat: number, lng: number, altittude: number,  accuracy: number}
		 * @param showdata		The showdata from the gigpress plugin as javascript object plusss, other params set in the gigpress_extenstions 
		 */ 
		addMarker: function(address, coordinates, showdata){
			markers[address] = {'address': address, 'coordinates': coordinates, 'showdata': showdata};
		},
		
		/**
		 * Activate Marker
		 *
		 *	If the marker with given address is present in the list and available in the map set it as the current active object.
		 *
		 *	@param address The address in the map to activate. 
		 */
		activateMarker: function(address){
			if(current_marker != null){
				try {
					current_marker.closeInfoWindow();
				}catch(e){
					//
				}
			}
			try {
				marker = markers[address];
				pushMarkersForEvent(marker['gmarker'], marker);
				map.setCenter(marker['point'], marker['zoom']);
			}catch(e){
				//
			}
		}
	};
}(jQuery); // Module Pattern @url http://yuiblog.com/blog/2007/06/12/module-pattern/