	var map = null;
    var geocoder = null;
    var scale = 
    {
    	min: 1, 
    	max: 20,
    	convertScale: function(value) {
    		return Math.round((value/((this.max-this.min)+1))*100);
    	},
    	backConvertScale: function(value) {
    		return Math.round((value*((this.max-this.min)+1))/100);
    	}
    };

    $(document).ready(function() 
	{
		if (GBrowserIsCompatible() && document.getElementById("maps_canvas")) {
		    map = new GMap2(document.getElementById("maps_canvas"));
		    geocoder = new GClientGeocoder();
		    if (map_coords_1 != 0 && map_coords_2 != 0) {
		    	point = new GLatLng(map_coords_1, map_coords_2);
		    	placeMarker(point, map_address);
		    } else {
		    	map.setCenter(new GLatLng(34, 0), 1);
		    }
		    map.addControl(new GSmallMapControl());
    		map.addControl(new GMapTypeControl());
    		
    		// Check map zoom and save it into hidden field
    		GEvent.addListener(map, 'zoomend', 
    		function() 
    		{
                $("#map_zoom").val(scale.convertScale(map.getZoom()));
                map_zoom = scale.convertScale(map.getZoom());
        	}); 
		}
	});

	function generateMap(getLocationArrayById_url, selected_location_id, address_line_1, server_path, title, logo, latitude, longitude) {
		$.post(getLocationArrayById_url, {location_id: selected_location_id}, 
		function(data) {
			var address = '';
			for (i=0; i<data.length; i++) {
				address += data[i].name;
				if (i != data.length) {
					address += ', ';
				}
			}
			address += address_line_1;

			if (manual_coords_block_opened) {
				// Place existed Lat/Lng
				map.clearOverlays();
				$("#map_address").val(address);
				placeMarker(new GLatLng(latitude, longitude), address);
			} else {
				// Geocode by address
				geocoder.getLocations(address, addAddressToMap);
			}
		}, "json");
	}

	function addAddressToMap(response) {
		map.clearOverlays();
		if (!response || response.Status.code != 200) {
			alert("Sorry, we were unable to geocode that address");
		} else {
			place = response.Placemark[0];
			point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
			$("#map_coords_1").val(place.Point.coordinates[1]);
			$("#map_coords_2").val(place.Point.coordinates[0]);
			$("#map_address").val(place.address);
			placeMarker(point, place.address);
		}
	}
	
	function placeMarker(point, address)
	{
		marker = new GMarker(point);
		map.setCenter(point, scale.backConvertScale(parseInt(map_zoom)));
		map.addOverlay(marker);
		var windowHtml = '';
		if (global_logo != '')
			windowHtml = '<img width="70px" align="left" style="float:left; padding-right:10px;" src="' + global_server_path + '/users_images/logos/' + global_logo + '">';
		windowHtml += '<b>' + global_title + '</b><br />' + address + '<div class="clear_float"></div>';
		marker.openInfoWindowHtml(windowHtml, {maxWidth:300});
	}
