var total_markers = 0;
var total_addresses = 0;

$(document).ready(function(){
	if(GBrowserIsCompatible()) {
	
		/************************************************************************************************
		// contants, vars, etc.
		//
		************************************************************************************************/	   
	
		var zoomed = 14;
		var bounds = new GLatLngBounds();		// this is the bounds of the entire map			
	
		var map = new GMap2(document.getElementById("google_map"));
		map.addControl(new GSmallMapControl());
		// map.addControl(new GMenuMapTypeControl());
		map.setCenter(new GLatLng(0, 0), zoomed);
		
		var myIcon = new GIcon(G_DEFAULT_ICON);
		myIcon.image = "/about-us/locations/pen-blue.png";
		myIcon.iconSize = new GSize(14, 22);
		myIcon.shadow = "/about-us/locations/pen-shadow.png";
		myIcon.shadowSize = new GSize(24, 22);
		myIcon.infoWindowAnchor = new GPoint(6, 9);
		
		var markerOptions = { icon:myIcon };
		
		
		
		// ====== Array for decoding the failure codes ======
		var reasons=[];
		reasons[G_GEO_SUCCESS]				= "Success";
		reasons[G_GEO_MISSING_ADDRESS]		= "Missing Address: The address was either missing or had no value.";
		reasons[G_GEO_UNKNOWN_ADDRESS]		= "Unknown Address:	 No corresponding geographic location could be found for the specified address.";
		reasons[G_GEO_UNAVAILABLE_ADDRESS]	= "Unavailable Address:	 The geocode for the given address cannot be returned due to legal or contractual reasons.";
		reasons[G_GEO_BAD_KEY]				= "Bad Key: The API key is either invalid or does not match the domain for which it was given";
		reasons[G_GEO_TOO_MANY_QUERIES]		= "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
		reasons[G_GEO_SERVER_ERROR]			= "Server error: The geocoding request could not be successfully processed.";

		

		/************************************************************************************************
		//	fmt_info(obj, marker) - extracts the store info from the object, returns html string with info
		//		obj: the jQuery object representing the store
		//		marker: the current marker, for directions purposes
		************************************************************************************************/	   
		function fmt_info(obj, marker){
			var to_return = "";
			if(obj.attr("title")){
				to_return += "<strong>" + obj.attr("title") + "</strong>";
			}
			if(obj.attr("rel")){
				to_return += "<br />" + obj.attr("rel");
			}
			if(obj.attr("phone")){
				to_return += "<br />" + obj.attr("phone");
			}
			if(obj.attr("hours")){
				to_return += "<br />" + obj.attr("hours");
			}
			
			return to_return;
		}
	
		/************************************************************************************************
		//	do_zoom(latlng, marker) - zooms to the marker, and pops open the info for that store
		//		latlng: the GLatLng for the store
		//		marker: the marker on the map for the store 
		//		obj: the jQuery obj holding the store info
		//	
		************************************************************************************************/	   
		function do_zoom(latlng, marker, obj){
			map.panTo(latlng);
			map.setCenter(latlng, zoomed);
			marker.openInfoWindowHtml(fmt_info(obj, marker));				
		}
	
		/************************************************************************************************
		//	locate_store(search) - drops a marker on the map for every possible address match
		//		search: is a string containing the city, state and zip of a store
		//		obj: the jQuery object to attach the click handler for panning
		//	
		************************************************************************************************/	   
		function locate_store(lat, lng, obj, idx){
			// console.log(lat);
			// console.log(lng);
			
			var latlng = new GLatLng(lat,lng);
			var marker = new GMarker(latlng, markerOptions);

			// console.log(marker);

			map.addOverlay(marker);
			total_markers++;

			// extend the bounds of the map
			// used when we zoomShowAll() later
			bounds.extend(latlng);							
			
			GEvent.addListener(marker, 'click', function() {
				map.setCenter(latlng, zoomed);
				marker.openInfoWindowHtml(fmt_info(obj, marker));
				// console.log(marker);
			});


			obj.click(function(){
				do_zoom(latlng, marker, $(this));
			});
			
			zoomShowAll();

		}

		/************************************************************************************************
		//	zoomShowAll() - using the global map and bounds, zooms out to accommodate all markers
		//	
		************************************************************************************************/	   
		function zoomShowAll() {		
			// console.log("zooming out");	  
			// console.log(bounds);	
			map.setCenter(new GLatLng(0,0), 0);
			map.setZoom(map.getBoundsZoomLevel(bounds) - 1);
			var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;
			var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;
			map.setCenter(new GLatLng(clat, clng));
		}	
		

		/************************************************************************************************
		// process the locations.
		************************************************************************************************/ 
		$(".store_link[latitude]").each(function(obj, idx){
			locate_store($(this).attr("latitude"), $(this).attr("longitude"), $(this), idx);
			total_addresses++;		
			// console.log("going..." + total_addresses);
		});
		
		setTimeout(
			function(){
				// console.log(total_markers);
				if(total_markers > 0){
					
					$("#zoomout").html("Click here to zoom out and view all locations<br /><br />");
					$("#zoomout").click(function(){
						zoomShowAll();
					});			
				};
			},
			50
		);
		
	}				
}); 
   
$(document.body).unload(function() {
	if(GBrowserIsCompatible()) {
		GUnload();
	}
});


