var map = {};
var global_markers = [];
var global_ambients = [];
var markerClusterer = null;
var mapSpace;

window.addEvent('domready',function(){
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("gmap"));
		/* Set GMap Center*/
		map.setCenter(new GLatLng(41.442726,12.392578), 5);
		
		var mapTypes = {
			'roadmap': G_NORMAL_MAP,
			'satellite': G_SATELLITE_MAP,
			'hybrid': G_HYBRID_MAP,
			'terrain': G_PHYSICAL_MAP
		};
		map.setMapType(G_HYBRID_MAP);

		/* Setup Controls */
		map.addControl(new rmTopBarControl());
		map.addControl(new rmBottomBarControl());
		map.addControl(
			new GMenuMapTypeControl(),
			new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(8, 110))
		);
		map.addControl(
			new GLargeMapControl3D() ,
			new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(8, 110))
		);
			
		map.addControl(
			new rmFadeMessage('Premere F11 per visualizzare Realtour Maps a pieno schermo.')
		);

		/* Setup GMap behaviors */
		map.enableContinuousZoom();
        map.enableScrollWheelZoom(); 


		mapSpace = new GLatLngBounds(); 

		markerClusterer = new MarkerClusterer(
			map,
			null,
			{
				maxZoom: 12,
				gridSize: 15
			}
		);

		new Request.JSON({
			url: "/categories.json",
			onSuccess: function(responseJSON, responseText){
				responseJSON.each(function(category){
					rmMarkerIcons[category['Category']['id']] = {
						'id': category['Category']['id'],
						'name': category['Category']['name'],
						'gicon': MapIconMaker.createMarkerIcon({
							width: category['Category']['width'],
							height: category['Category']['height'],
							primaryColor: category['Category']['primaryColor'],
							strokeColor: category['Category']['strokeColor'],
							cornerColor: category['Category']['cornerColor']
						})
					};
				});
				new Request.JSON({
					url: "/structures.json",
					onSuccess: function(responseJSON, responseText){
						$H(responseJSON).each(function(structures,category_id){
							structures.each(function(structure){
								var latlng = new GLatLng(structure['lat'], structure['lng']);
								
								//global_markers.push(rmCreateMarker(latlng,structure));
								structure['url'] = '/structures/view/'+structure['id']+'.json';
								global_markers.push(rmCreateStructureMarker(structure));
								mapSpace.extend(latlng); 
							});
						});
						
						markerClusterer.addMarkers(global_markers);
						map.setCenter(mapSpace.getCenter(),map.getBoundsZoomLevel(mapSpace) ); 
					}
				}).get();
				
				new Request.JSON({
					url: "/ambients.json",
					onSuccess: function(responseJSON, responseText){
						
						responseJSON.each(function(ambient){
							global_ambients.push(rmCreateAmbientMarker(ambient));
							var latlng = new GLatLng(ambient['Ambient']['lat'], ambient['Ambient']['lng']);
							mapSpace.extend(latlng); 
						});
												
						markerClusterer.addMarkers(global_ambients);
						map.setCenter(mapSpace.getCenter(),map.getBoundsZoomLevel(mapSpace) ); 
					}
				}).get();
				
			}
		}).get();

	}
});
