// JScript File
/*
 * Google MAP jQuery interface
 * Copyright (c) 2009  Oliver Astrologo
 * Dual licensed under MIT and GPL.
 * @author  Oliver Astrologo
 * @version 1.1
 */

var side_bar_html;
var map;
var counter = 0;
var gmarkers = [];
var htmls = [];
var geocoder = null;
var selmarker = false;
var responseLat = 41.895465;
var responseLng = 12.482324;
var defaultMapZoom = 5;
var mapIsInitialized = false;


function showRequest(XMLHttpRequest) {
	//jQuery("#loading-indicator").show();
	//jQuery('#form-via').addClass("ac_loading");
}

function showError(XMLHttpRequest, textStatus, errorThrown) {
	//console.log( textStatus);
	//jQuery('#form-via').removeClass("ac_loading");
	alert('Spiacenti ma al momento non è possibile visualizzare i risultati, riprova fra poco.');
}		

function processData(){			
	jQuery.ajax({		
		type: "GET",
		dataType: 'json',   
		cache: false,
		beforeSend: showRequest,   
		error: showError,
		url: '/data-sources/territorio.cgi',
		data: {tipo:GeoData},
		success: function(data){addMarkers(data);}
		}); 
	} 		


///
// Aggiungo i markers
///
function addMarkers (data,key) { 
	if (!mapIsInitialized) {
		initializeMap();
		}
	else {		
		map.getInfoWindow().hide();
		map.clearOverlays();
		gmarkers = [];	
		htmls = [];
		counter = 0;
		}		
	var bounds = new GLatLngBounds();  	   
	jQuery.each(data.markers, function (i, item) {		
		var tipo = parseFloat(item.tipo);			
		var lat = parseFloat(item.lat);
		var lng = parseFloat(item.lng);
		var point = new GLatLng(lat, lng);
		var title = item.name;					
		var html = '<h4>'+item.name+'</h4>' + (item.address ? '<strong>'+item.address+'</strong><br/>':'') + item.html;
		htmls[counter] = '<div class="baloon">'+html+'</div>';						
		var marker = createMarker(point, tipo,counter);
		map.addOverlay(marker);							
		bounds.extend(point);			
		counter++;
		});
	if (counter == 0) {
		map.setCenter(new GLatLng(responseLat,responseLng), defaultMapZoom);
		}
	else {
		map.setZoom(map.getBoundsZoomLevel(bounds) );
		map.panTo(bounds.getCenter());  
		}
	}


///
// Creo il marker
///
function createMarker(point, icontype,index,defaultOpen) {
	var marker = new GMarker(point, icons[icontype]);
	if (jQuery('#map_canvas_add').length == 0) {
		GEvent.addListener(marker, "click", function () {
			marker.openInfoWindowHtml(htmls[index]);
			});
		if (defaultOpen){
			marker.openInfoWindowHtml(htmls[index]);
			}
		}
	gmarkers[index] = marker;  
	return marker;
	}

function initializeMap() {
	if (jQuery('#map_canvas').length > 0) {		 
		if (GBrowserIsCompatible()) {
			// this variable will collect the html which will eventualkly be placed in the side_bar		 
			// create the map
			map = new GMap2(document.getElementById("map_canvas"));				
			map.enableDoubleClickZoom();			
			map.setUIToDefault();
			map.setCenter(new GLatLng(responseLat,responseLng),defaultMapZoom);	
			map.disableScrollWheelZoom();
			mapIsInitialized = true;
			}
		else {
			alert("Sorry, the Google Maps API is not compatible with this browser");
			}
		};	
	}
	
jQuery(function(){	
	processData();	
	});
