google.load("maps", "2.x");

$(document).unload( function () {
	google.maps.Unload();
});

function initSalonMap() {

	if(typeof geoLocate != 'undefined' && geoLocate) {
		if (google.loader.ClientLocation) {
			var city = google.loader.ClientLocation.address.city;
			$('#location').val(city);
		}
	}

	if (GBrowserIsCompatible()) {
		var pmIcon = new google.maps.Icon();
		pmIcon.image = "img/map-icon.png";
		pmIcon.iconSize = new google.maps.Size(40, 40);
		pmIcon.iconAnchor = new google.maps.Point(5, 34);
		pmIcon.infoWindowAnchor = new google.maps.Point(30, 2);

		var map    = new google.maps.Map2(document.getElementById("map"));
		var bounds = new google.maps.LatLngBounds();
		var point  = null;
		var marker = null;

		if(typeof mapData != 'undefined') {
			for(var i = 0; i < mapData.length; i++) {
				point = new google.maps.LatLng(mapData[i][0], mapData[i][1]);
				bounds.extend(point);
				marker = new google.maps.Marker(point, pmIcon);
				marker.bindInfoWindowHtml(mapData[i][2]);
				$('#salon_' +  mapData[i][3]).bind("click", {marker: marker}, function(e) {
					GEvent.trigger(e.data.marker, "click");
				});
				
				map.addOverlay(marker);
			}

			if(typeof myLat != 'undefined') {
				point = new google.maps.LatLng(myLat, myLng);
				marker = new google.maps.Marker(point);
				bounds.extend(point);
				map.addOverlay(marker);
			}
			map.setCenter(bounds.getCenter());
			map.setZoom(map.getBoundsZoomLevel(bounds));
		}
		else {
			map.setCenter(new google.maps.LatLng(lat, lng));
			map.setZoom(zoom);
		}
		map.addControl(new google.maps.LargeMapControl());
		map.enableDoubleClickZoom();
		map.enableScrollWheelZoom();
	}
}

google.setOnLoadCallback(initSalonMap);
