// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var startZoom = 9;
var map;
var markerHash={};

// Create a base icon for all of our markers that specifies the
// shadow, icon dimensions, etc.


function init() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
	}
	
	for(i=0; i<markers.length; i++) { 
		point = new GLatLng(markers[i].latitude, markers[i].longitude)
		marker = createMarker( point, markers[i].id, i); 
		var contactHtml='<div class=\"map_quote\">'
					+'<strong>'+ markers[i].company+'</strong><br/>'
					+ markers[i].address1 +'<br/>'
					+ markers[i].city+',' + markers[i].state +' '+ markers[i].postal_code +'<br/>'
					+'<strong>Contact: </strong>'+ markers[i].first_name+" "+[markers[i].last_name]+'<br/>'
					+'<strong>Telephone: </strong>'+ markers[i].telephone +'<br />'
					+'<a href=\"/users/' + markers[i].id + '/contacts/new\">Contact Installer</a>'
					+'</div>';
							 	
		markerHash[markers[i].id]={marker:marker,html:contactHtml,visible:true}; 
		
	} 
	
}

function addMarker(latitude, longitude, description) { 
	var marker = new GMarker(new GLatLng(latitude, longitude)); 
	GEvent.addListener(marker, 'click', 
	function() { 
		marker.openInfoWindowHtml(description); 
	}); 
	
	map.addOverlay(marker); 
} 

function focusPoint(id){ 
markerHash[id].marker.openInfoWindowHtml(markerHash[id].html); 
} 

    

function createMarker(point,id,index) {
          // Create a lettered icon for this point using our icon class
          var letter = String.fromCharCode("A".charCodeAt(0) + index);
          var letteredIcon = new GIcon(baseIcon);
          letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";

          // Set up our GMarkerOptions object
          markerOptions = { icon:letteredIcon };
          var marker = new GMarker(point, markerOptions);

          GEvent.addListener(marker, "click", function() {
			focusPoint(id)
          });
          map.addOverlay(marker);
		  return marker;
}

function updateLocation(point) {
	document.getElementById('photo_geo_lat').value = point.y; 
	document.getElementById('photo_geo_long').value = point.x;
	map.clearOverlays();
	map.addOverlay(new GMarker(new GLatLng(point.y, point.x)));
}

