/**
 * Renderiza o mapa google para o direct�rio e oficinas
 */
function renderMap( cLatitude, cLongitude, url, zoom, iconImagePath, address ) {
	if( GBrowserIsCompatible() ) {
		var map = new GMap2( document.getElementById('mapRender') );
		map.setUIToDefault();
		var baseIcon = new GIcon();
		baseIcon.image = iconImagePath;
		baseIcon.iconSize = new GSize( 48, 48 );
		baseIcon.iconAnchor = new GPoint( 0, 0 );
		baseIcon.infoWindowAnchor = new GPoint( 24, 24 );		
		if( address != null && address.length >= 3) {
			var geocoder = new GClientGeocoder();
			geocoder.getLatLng(	address, function( point ) {
				if( !point ) {
					alert( address + " não encontrado!");
				} else {
					cLatitude = point.y;
					cLongitude = point.x;
					mapLocations( cLatitude, cLongitude, url+'&q='+address, map, baseIcon, iconImagePath, zoom );
				}
			});
		} else {
			mapLocations( cLatitude, cLongitude, url, map, baseIcon, iconImagePath, zoom );
		}
	}
}

function mapLocations( cLatitude, cLongitude, url, map, baseIcon, iconImagePath, zoom ) {
	
	new Request({
		url: url,
		method: 'get',
		onSuccess: function( response ) {
			response = JSON.decode( response );
			for( i = 0; i < response.length; i++ ) {
				var latlng = new GLatLng( response[i].latitude, response[i].longitude );
				if( $chk( response[i].logoMapa ) && response[i].logoMapa != '' ) {
					iconPath = response[i].logoMapa;
				} else {
					iconPath = iconImagePath;
				}
				map.addOverlay( createMarker( latlng, response[i].text, iconPath, baseIcon ) );
			}
			map.setCenter( new GLatLng( cLatitude, cLongitude ), zoom );
		}.bindWithEvent( this )
	}).send( 'lat='+cLatitude+'&lon='+cLongitude );
	
	
}


/**
 * Cria um marcador no mapa google
 */
function createMarker( point, text, iconImagePath, baseIcon ) {
	var iconMarker = new GIcon( baseIcon );
	iconMarker.image = iconImagePath;
	markerOptions = { icon:iconMarker };
	var marker = new GMarker( point, markerOptions );
	//click
	GEvent.addListener( marker, "click", function() {
		marker.openInfoWindowHtml( text );
	});
	return marker;
}

