// JavaScript Document

var map;
var geocoder;
		
function createMarker(point, address, categoria) {
	// Creo il marker personalizzato
	var prodIcon = new GIcon(G_DEFAULT_ICON);
	switch(categoria){
		case '1': prodIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/yellow.png";
				break;
		case '2': prodIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/red.png";
				break;
		case '3': prodIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/pink.png";
				break;
		case '4': prodIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/green.png";
				break;
		case '5': prodIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/orange.png";
				break;
		case '6': prodIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/purple.png";
				break;
		case '7': prodIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/blue.png";
				break;
		default: prodIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/lightblue.png";
				break;
	}
	
	prodIcon.iconSize = new GSize(30, 32);
	prodIcon.shadowSize = new GSize(55, 32);
                
	// Aggiungo l'opzione del marker personalizzato ai marker
	markerOptions = { icon:prodIcon };

	var marker = new GMarker(point, markerOptions); 
	GEvent.addListener(marker, "click", 
	function() {    
		marker.openInfoWindowHtml(address);  
	});  
	return marker;
}

function initialize() {
	if (GBrowserIsCompatible() && (stato_mappa=="aperta" || stato_mappa=="")) {      	
		
    	map = new GMap2(document.getElementById("map_canvas"));
		map.setMapType(G_HYBRID_MAP);
		           
		map.addControl(new GLargeMapControl3D());
    	map.addControl(new GMapTypeControl());
		
		if(typeof(ids_array) == "undefined"){
			//geocoder = new GClientGeocoder();
			//geocoder.getLocations("it", set_center);
			map.setCenter(new GLatLng(45.6998297,10.2229573), 8);
			if (typeof(is_empty_user) != "undefined"){}
			else{
				/*for(i=0; i < data.count; i++){
					coords = data.prodotti[i].coords.split(",");
					point = new GLatLng(coords[0],coords[1]);
					
					if(!data.prodotti[i].testo)
						nome = '<div style="width:300px;"><br/ ><a style=" position:relative; top:-20px;" href="'+base_url+'index.php/home/vedi_prodotto/'+ data.prodotti[i].prodotto_id +'">' + data.prodotti[i].nome + '</a><br></div>';
					else{ 
						img = base_url+'prodotti_img/'+ data.prodotti[i].img;
							
						nome = '<div style="width:300px;"><br/ ><a href="'+base_url+'index.php/home/vedi_prodotto/'+ data.prodotti[i].prodotto_id +'"><img style="width:60px; float:right; border:1px solid gray;" src="'+ img +'" /></a><a style=" position:relative; top:-20px;" href="'+base_url+'index.php/home/vedi_prodotto/'+ data.prodotti[i].prodotto_id +'">' + data.prodotti[i].nome + '</a><br><em>' + data.prodotti[i].testo + '</em></div>';
					}
					map.addOverlay(createMarker(point, nome, data.prodotti[i].id_categoria));
				}*/
			}
		}
		else if(ids_array == '')
			map.setCenter(new GLatLng(45.6998297,10.2229573), 8);
    	else{
			var minLat = 90;
			var minLon = 181;
			var maxLat = -1;
			var maxLon = -181;
			var contatore_punti = 0;
			
			for(i=0; i < ids_array.length; i++){
				for(j=0; j < data.count; j++){
					if(data.prodotti[j].prodotto_id == ids_array[i]){
						coords = data.prodotti[j].coords.split(",");
						point = new GLatLng(coords[0],coords[1]);
						contatore_punti++;
						if(coords[0] < minLat) minLat = parseFloat(coords[0]);
						if(coords[0] > maxLat) maxLat = parseFloat(coords[0]);
						if(coords[1] < minLon) minLon = parseFloat(coords[1]);
						if(coords[1] > maxLon) maxLon = parseFloat(coords[1]);
	
						if(!data.prodotti[j].testo)
							nome = '<div style="width:300px;"><br/ ><a style=" position:relative; top:-20px;" href="'+base_url+'index.php/home/vedi_prodotto/'+ data.prodotti[j].prodotto_id +'.0">' + data.prodotti[j].nome + '</a><br></div>';
						else{ 
							img = base_url+'prodotti_img/'+ data.prodotti[j].img;
							
							nome = '<div style="width:300px;"><br/ ><a href="'+base_url+'index.php/home/vedi_prodotto/'+ data.prodotti[j].prodotto_id +'"><img style="width:60px; float:right; border:1px solid gray;" src="'+ img +'" /></a><a style=" position:relative; top:-20px;" href="'+base_url+'index.php/home/vedi_prodotto/'+ data.prodotti[j].prodotto_id +'">' + data.prodotti[j].nome + '</a><br><em>' + data.prodotti[j].testo + '</em></div>';
						}
						map.addOverlay(createMarker(point, nome, data.prodotti[j].id_categoria));
					}
				}
			}
			if(contatore_punti >= 1){
				/*minLat = minLat - 0.005;
				maxLat = maxLat + 0.005;
				minLon = minLon - 0.005;
				maxLon = maxLon + 0.005;*/
				var boundaries = new GLatLngBounds(new GLatLng(minLat,minLon), new GLatLng(maxLat,maxLon));
							
				centerLat = (maxLat + minLat)/2;
				centerLon = (maxLon + minLon)/2;
							
				var center = new GLatLng(centerLat,centerLon);
				
				if(contatore_punti == 1)
					var minZoom = 16;
				else
					var minZoom = map.getBoundsZoomLevel(boundaries);

				map.setCenter(center, minZoom);
			}
			else{
				map.setCenter(new GLatLng(45.6998297,10.2229573), 8);
			}
		}
	}
}

/*function setMaxZoomCenter(map, latlng) {
  G_SATELLITE_MAP.getMaxZoomAtLatLng(latlng, function(response) {
    if (response && response['status'] == G_GEO_SUCCESS) {
	  if(response['zoom'] < 16)
        map.setCenter(latlng, response['zoom']);
	  else 
	  	map.setCenter(latlng, 16);
    }
  });
}

function set_center(response){
	place = response.Placemark[0];
    point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
	map.setCenter(point, 5);
}*/
