// 
//  location-mini.js
//  Ochsner Location Mini Map
//  
//  Created by Sean Gates on Tuesday September 23th 2008 (15:11PM)
//  Copyright 2008 Paramore | Redd Online Marketing. All rights reserved.
// 

$('document').ready(function() {
	/********* DEFAULTS ********************************************************/
	var def_zoomLevel 		= 8;
	var def_jsonFile 		= '/locations/locations_json';
	var def_LatLng			= new GLatLng(30.4, -90.5);
	/********* END DEFAULTS ****************************************************/

	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map"));
		map.setCenter(def_LatLng, def_zoomLevel);
		
		geocoder = new GClientGeocoder();

		map.addControl(new GSmallMapControl());
		// map.addControl(new GMapTypeControl());
		
		function add_marker(latlng, myloc){
			
			var baseIcon = new GIcon();
			// switch the type of marker used
			var location_type = "";
			var secondary_type = "";
			
			if (loc.type.indexOf("Medical Center") != -1) {
				location_type = "hospital";
			} else if (loc.type.indexOf("Health Care") != -1) {
				location_type = "outpatient";
			} else if (loc.type.indexOf("Specialty Center") != -1) {
				location_type = "specialty";
			}	else if (loc.type.indexOf("Emergency Care") != -1) {
				location_type = "emergency";
			} else if (loc.type.indexOf("Urgent Care Center") != -1) {
				location_type = "urgent";
			} else if (loc.type.indexOf("Elmwood Fitness Center") != -1) {
				location_type = "fitness";
			} 

			baseIcon.image = "/images/ico/flag_" + location_type + "_map.png";
			
			baseIcon.shadow = "/images/ico/flag_shadow.png";
			baseIcon.iconSize = new GSize(19, 25);
			baseIcon.shadowSize = new GSize(33, 25);
			baseIcon.iconAnchor = new GPoint(9, 20);
			baseIcon.infoWindowAnchor = new GPoint(9, 2);
			
			var marker = new GMarker(latlng,{ icon:baseIcon });
			map.addOverlay(marker);
			/*GEvent.addListener(marker, "click", function() {
				// marker.openInfoWindowHtml("<div class=\"bubble\"><p><strong>" + myloc.name + "</strong><br /><a href=" + myloc.address + "<br />" + myloc.city + ", " + myloc.state + " " + myloc.zip + "<br />" + myloc.phone + "</p></div>");
				marker.openInfoWindowHtml('<div class="bubble"><form action="http://maps.google.com/maps" method="get" target="_blank"><fieldset><legend>Driving Directions</legend><label for="saddr">Starting Address</label><input type="text" name="saddr" id="saddr"></fieldset><fieldset><legend>Submit</legend><input type="hidden" value="' + myloc.address + ', ' + myloc.city + ', ' + myloc.state + ' ' + myloc.zip + '" name="daddr"><input type="submit" value="Get Directions &raquo;" class="map-directions" /></fieldset></form></div>');
			});*/
			
			return marker;
		} // end function add_marker
		
		function sortByKey(arr,key,direction) {
			function sortNumeric(a,b) {
				var c=a[key],d=b[key];
				if (direction=="ASC") {
					return (c==="")-(d==="") || c-d;
				} else {
					return (c==="")-(d==="") || d-c;   
				}
			}       
			return arr.sort(sortNumeric);
	    }
		
		function convertToMiles(meters){
			miles = meters / 1609.344;
			return parseFloat(miles).toFixed(1);
		}
		
		var bounds = new GLatLngBounds();
		
		if(loc.lat != '' && loc.lng != ''){
			var point = new GLatLng(loc.lat,loc.lng);
			bounds.extend(point);
			add_marker(point,loc);
			map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds) - 6);
		}
		
		$('#map').click(function(event) {
			var $target = $(event.target);
			if( $target.is('.map-directions')) {
				$target.parent('form').sumbit();
				return false;
			}
		});

	} // end browser compatibility test
}); // end ready()
