//<![CDATA[
var map;
var markers = [];
var infoWindow;
var locationSelect;
var l_lat = 41.8719400;
var l_lng = 12.5673800;
var l_zoom;
var l_type;
var map_id;




function load() {
	map = new google.maps.Map(document.getElementById("map"), {
		center: new google.maps.LatLng(l_lat, l_lng),
		zoom: 5,
		mapTypeId: 'roadmap',
		mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU}
	});
	infoWindow = new google.maps.InfoWindow();

	/*locationSelect = document.getElementById("locationSelect");
	locationSelect.onchange = function() {
	var markerNum = locationSelect.options[locationSelect.selectedIndex].value;
	if (markerNum != "none"){
	google.maps.event.trigger(markers[markerNum], 'click');
	}
	};*/
}

function loadSingle(l_lat, l_lng, l_zoom, l_type,map_id, nome, indirizzo, email, telefono, fax) {
	geocoder = new google.maps.Geocoder();
	var myLatlng = new google.maps.LatLng(l_lat,l_lng);
	var myOptions ={
		center: myLatlng,
		zoom: l_zoom,
		mapTypeId: google.maps.MapTypeId.ROADMAP,/*  ROADMAP / SATELLITE / HYBRID / TERRAIN */
		mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU}
	};
	map = new google.maps.Map(document.getElementById(map_id), myOptions);
	infoWindow = new google.maps.InfoWindow();

	switch(l_type){
		case 'SATELLITE':
		map.setMapTypeId(google.maps.MapTypeId.SATELLITE);
		break;
		case 'HYBRID':
		map.setMapTypeId(google.maps.MapTypeId.HYBRID);
		break;
		case 'TERRAIN':
		map.setMapTypeId(google.maps.MapTypeId.TERRAIN);
		break;
	}

	marker = new google.maps.Marker({
		position: myLatlng,
		map: map,
		title: nome,
		draggable: true
	});

	var html = "<b>" + nome + "</b><br/>" + indirizzo;
	if(email != '') {
		html = html + "<br/>e-mail: <a href=\"mailto:" + email + "\">" + email + "</a>";
	}
	if(telefono != '') {
		html = html + "<br/>Telefono: " + telefono;
	}
	if(fax != '') {
		html = html + "<br/>Fax: " + fax;
	}
	infoWindow.setContent(html);
	infoWindow.open(map, marker);
}

/* cambio provincia */
function changeLocationsNear(tipo_select){
		var comune;
		comune='';

		if(document.forms['form_filiali'].filiali_provincia.value==''){
			alert("Selezionare una provincia!");
			return;
		}//end if

		if(!(eval('document.forms["form_filiali"].filiali_comuni') == undefined) && tipo_select=='C'){
			comune=document.forms['form_filiali'].filiali_comuni.value;
		}//end if

		searchLocationsNear(document.forms['form_filiali'].filiali_provincia.value, comune);
}//end if

/* caricamento con markers da xml */
function searchLocationsNear(provincia, comune) {
	clearLocations();

	//	var radius = document.getElementById('radiusSelect').value;
	if((provincia!='') && (provincia!='tutte')){
		estensione_get='?id_prov='+provincia;
		if((comune!='') && (comune!='tutte')){
			estensione_get=estensione_get + '&id_comune='+comune;
		}//end if
	}else{
		estensione_get='';
	}//end if


	var searchUrl = 'gmaps_xml.php' + estensione_get;

	downloadUrl(searchUrl, function(data) {
		var xml = parseXml(data);
		var markerNodes = xml.documentElement.getElementsByTagName("marker");
		var bounds = new google.maps.LatLngBounds();
		if(markerNodes.length == 0) {
			alert('Nessuna filiale in zona.\nProva a modificare la tua ricerca.');
			return;
		}

		//alert(markerNodes[0].getAttribute("name"));

		for (var i = 0; i < markerNodes.length; i++) {
			var name = markerNodes[i].getAttribute("name");
			var email = markerNodes[i].getAttribute("email");
			var fax = markerNodes[i].getAttribute("fax");

			var indirizzo = markerNodes[i].getAttribute("indirizzo");
			var provincia = markerNodes[i].getAttribute("provincia");
			var zip = markerNodes[i].getAttribute("zip");
			var comune = markerNodes[i].getAttribute("comune");

			var telefono = markerNodes[i].getAttribute("telefono");
			var immagine = markerNodes[i].getAttribute("immagine");
			var seriale = markerNodes[i].getAttribute("seriale");

			var distance = parseFloat(markerNodes[i].getAttribute("distance"));
			var latlng = new google.maps.LatLng(

			parseFloat(markerNodes[i].getAttribute("lat")),
			parseFloat(markerNodes[i].getAttribute("lng")));

			switch(markerNodes[i].getAttribute("categoria")){
				case 3: /* NEB*/
				case "3": /* NEB*/
					/* immagine icona puntatore */
					var image = new google.maps.MarkerImage('http://www.vodu.it/icone_googlemaps/civibank_neb.png',
		 										new google.maps.Size(32, 34),
		 										new google.maps.Point(0,0),
		 										new google.maps.Point(0, 32));
					/* ombreggiatura bandiera*/
					 var shadow = new google.maps.MarkerImage('http://www.vodu.it/icone_googlemaps/marker_end_shadow.png',
		 									       new google.maps.Size(38, 34),
		 									       new google.maps.Point(0,0),
		 									       new google.maps.Point(-7, 36));

				break;
				case 2: /* BANCOMAT*/
				case "2": /* <b></b>*/
					/* immagine icona puntatore */
					var image = new google.maps.MarkerImage('http://www.vodu.it/icone_googlemaps/civibank_bancomat.png',
		 										new google.maps.Size(32, 34),
		 										new google.maps.Point(0,0),
		 										new google.maps.Point(0, 32));
					/* ombreggiatura bandiera*/
					 var shadow = new google.maps.MarkerImage('http://www.vodu.it/icone_googlemaps/marker_end_shadow.png',
		 									       new google.maps.Size(38, 34),
		 									       new google.maps.Point(0,0),
		 									       new google.maps.Point(-7, 36));

				break;
				default:/* CIVIBANK */
					 var image = new google.maps.MarkerImage('http://www.vodu.it/icone_googlemaps/civibank.png',
		 										new google.maps.Size(32, 37),
		 										new google.maps.Point(0,0),
		 										new google.maps.Point(0, 32));
					/* ombreggiatura bandiera*/
					 var shadow = new google.maps.MarkerImage('http://www.vodu.it/icone_googlemaps/marker_end_shadow.png',
		 									       new google.maps.Size(38, 34),
		 									       new google.maps.Point(0,0),
		 									       new google.maps.Point(-10, 30));

				break;
			}



		/* zona cliccabile [x1,y1,x2,y2]  */
		 var shape = {
					coord: [0, 0, 100, 100],
					type: 'rect'   };

			//createOption(name, distance, i);
			createMarker(seriale, latlng, image, shape, shadow, name, indirizzo, provincia, zip, comune, telefono, email, fax, immagine);
			bounds.extend(latlng);
		}

		map.fitBounds(bounds);

		if(markerNodes.length==1) //in caso di unico marker setto lo zoom a 16
			map.setZoom(16)

		/*var zoom = map.getZoom();
		zoom = 9;
		alert(zoom);
		/*map.setZoom(zoom);*/
		/*locationSelect.style.visibility = "visible";
		locationSelect.onchange = function() {
		var markerNum = locationSelect.options[locationSelect.selectedIndex].value;
		google.maps.event.trigger(markers[markerNum], 'click');
		};*/
	});
}


function clearLocations() {
	infoWindow.close();
	for (var i = 0; i < markers.length; i++) {
		markers[i].setMap(null);
	}
	markers.length = 0;

//	locationSelect.innerHTML = "";
//	var option = document.createElement("option");
//	option.value = "none";
//	option.innerHTML = "Tutti i risultati:";
//	locationSelect.appendChild(option);
}// end function clearLocations()


function doNothing() {}


function downloadUrl(url, callback) {
	var request = window.ActiveXObject ?
	new ActiveXObject('Microsoft.XMLHTTP') :
	new XMLHttpRequest;

	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			request.onreadystatechange = doNothing;
			callback(request.responseText, request.status);
		}
	};

	request.open('GET', url, true);
	request.send(null);
}

function parseXml(str) {
	if (window.ActiveXObject) {
		var doc = new ActiveXObject('Microsoft.XMLDOM');
		doc.loadXML(str);
		return doc;
	} else if (window.DOMParser) {
		return (new DOMParser).parseFromString(str, 'text/xml');
	}
}




function createMarker(seriale, latlng, image, shape, shadow, name, indirizzo, provincia, zip, comune, telefono, email, fax, immagine) {
	var html = "<table width='300px' cellpadding='0' cellspacing='0' border='0'>";
	html = html + "<tr>";
	html = html + "<td class='loc_testo' width='150px'>";

	html = html +  "<strong>" + name + "</strong><br/>";
	html = html +  "" + indirizzo + "<br/>";
	html = html +  "<strong>Cap:</strong> " + zip + "<br/>";
	html = html +  "<strong>Comune:</strong> " + comune + "<br/>";
	html = html +  "<strong>Provincia:</strong> " + provincia + "<br/>";
	if(email != '') html = html + "<strong>e-mail:</strong> <a href=\"mailto:" + email + "\">" + email + "</a><br/>";
	if(telefono != '') html = html + "<strong>Tel:</strong> " + telefono + "<br/>";
	if(fax != '') html = html + "<strong>Fax:</strong> " + fax + "<br/>";
	html = html + "</td>";

	if(immagine != ''){
		$path='./FILES/'+immagine;
	}else{
		$path='./imm_struttura/foto_non_disponibile.gif';
	}// end if
	html = html + "<td class='loc_testo'>";
	html = html + "<img src='"+$path+"' width='150' height='120'>";
	html = html + "</td>";
	html = html + "</tr>";

	var iframe="./scheda_locator/index.php?ID=" + seriale + "&amp;iframe=true&amp;width=700&amp;height=600";
	html = html + "<tr><td class='loc_dettaglio gallery' colspan='2'><a href='#' onclick='jQuery.prettyPhoto.open(\"" + iframe + "\",\""+name+"\",\"\");return false' rel=\"prettyPhoto[iframe]\" border=\"0\">Maggiori Dettagli</a></td></tr>";
	html = html + "</table>";

//	var html = "<div class='loc_container'>";
//		html = html + "<div class='loc_container_sx'>";
//		html = html +  "<div class='loc_titolo'>" + name + "</div>";
//		html = html +  "<div class='loc_testo'>" + indirizzo + "</div>";
//		html = html +  "<div class='loc_testo'>Cap: " + zip + "</div>";
//		html = html +  "<div class='loc_testo'>Comune: " + comune + "</div>";
//		html = html +  "<div class='loc_testo'>Provincia: " + provincia + "</div>";
//		if(email != '') html = html + "<div class='loc_testo'>e-mail: <a href=\"mailto:" + email + "\">" + email + "</a></div>";
//		if(telefono != '') html = html + "<div class='loc_testo'>Tel: " + telefono + "</div>";
//		if(fax != '') html = html + "<div class='loc_testo'>Fax: " + fax + "</div>";
//		html = html +  "</div>"; //fine container sx
//
//	if(immagine != ''){
//		$path='http://www2.vodu.it/civibank_banca_di_cividale/FILES/'+immagine;
//	}else{
//		$path='http://www2.vodu.it/civibank_banca_di_cividale/imm_struttura/foto_non_disponibile.gif';
//	}// end if
//
//	html = html +  "<div class='loc_container_dx'>";
//	html = html +  "<div class='loc_immagine'><img src='"+$path+"' width='150' height='120'></div>";
//	html = html +  "</div>"; //container_dx
//
//	var iframe="./scheda_locator/index.php?ID=" + seriale + "&amp;iframe=true&amp;width=700&amp;height=600";
// 	html = html + "<div class='loc_dettaglio gallery'><a href='#' onclick='jQuery.prettyPhoto.open(\"" + iframe + "\",\""+name+"\",\"\");return false' rel=\"prettyPhoto[iframe]\" border=\"0\">Maggiori Dettagli</a></div>";
//
//	html = html +  "</div>"; //fine container


	var marker = new google.maps.Marker({
		map: map,
		position: latlng,
		icon: image,
		shape: shape,
		shadow: shadow
	});
	google.maps.event.addListener(marker, 'click', function() {
		infoWindow.setContent(html);
		infoWindow.open(map, marker);
	});
	markers.push(marker);
}

/*function createOption(name, distance, num) {
var option = document.createElement("option");
option.value = num;
option.innerHTML = name + "(" + distance.toFixed(1) + ")";
locationSelect.appendChild(option);
}*/



//]]>

