// JavaScript Document

/**
* Gestisce l'apertura del menu principale
**/
function open_menu(menu_item){
	
//	var getsetlab = "/comunicazioni_sociali/pigotta";
	var getsetlab = "";
	
	switch(menu_item){
		case 'il-mondo-della-pigotta':
			document.getElementById('item_gruppo_1').style.display='block';
			document.getElementById('freccia_arancio').style.background='url('+getsetlab+'/images/arrow_dw_arancio.gif) no-repeat center right';
			break;
		case 'adotta-la-tua-pigotta':
			document.getElementById('item_gruppo_2').style.display='block';
			document.getElementById('freccia_rosa').style.background='url('+getsetlab+'/images/arrow_dw_rosa.gif) no-repeat center right';
			break;
		case '1-pigotta-1-kit-salvavita':
			document.getElementById('item_gruppo_3').style.display='block';
			document.getElementById('freccia_verde').style.background='url('+getsetlab+'/images/arrow_dw_verde.gif) no-repeat center right';
			break;
		case 'diventa-volontario':
			document.getElementById('item_gruppo_4').style.display='block';
			document.getElementById('freccia_azzurra').style.background='url('+getsetlab+'/images/arrow_dw_azzurra.gif) no-repeat center right';
			break;
		case 'il-magazine':
			document.getElementById('item_gruppo_5').style.display='block';
			document.getElementById('freccia_viola').style.background='url('+getsetlab+'/images/arrow_dw_viola.gif) no-repeat center right';
			break;
	}
}

/**
* Evidenzia gli errori di invio nelle form
**/
function errore(error_item){
	//document.getElementById(error_item).style.backgroundColor='#FF2D2D';
	document.getElementById(error_item).style.background = "url(/images/tab_input_bg_ko.gif) no-repeat 0px 26px";
}

function errore_ricevi_magazine(error_item){
	document.getElementById(error_item).className = 'errore';
	//document.getElementById(error_item).style.background = "url(/images/" + + "_ko.gif) no-repeat 0px 0px";
}

/**
* Apre il box in stile lightbox per la funzione dillo a un amico
**/
function openBox(){
	document.getElementById('opacizzato').className='lovedo';
	document.getElementById('container').className='lovedo'; 
	//var body_ = document.getElementsByTagName('body')[0];
	// Su IE6 non funziona, inoltre spacca la grafica
	if (typeof document.body.style.maxHeight != "undefined") {
		// IE 7, mozilla, safari, opera 9
		var body_ = document.getElementsByTagName('body')[0];
		//body_.style.overflow='hidden';
	} 
	//body_.style.overflow='hidden'; 
	return false;	
}

/**
* Chiude il box in stile lightbox per la funzione dillo a un amico
**/
function closeBox(){
	document.getElementById('div_tell_mail').style.background = "url(/images/tab_input_bg_ok.gif) no-repeat 0px 26px";
	document.getElementById('div_tell_tuamail').style.background = "url(/images/tab_input_bg_ok.gif) no-repeat 0px 26px";
	document.getElementById('div_tell_nome').style.background = "url(/images/tab_input_bg_ok.gif) no-repeat 0px 26px";
	document.getElementById('div_tell_tuonome').style.background = "url(/images/tab_input_bg_ok.gif) no-repeat 0px 26px";
	
	document.getElementById('tell_mail').value='';
	document.getElementById('tell_nome').value='';	
	document.getElementById('tell_tuamail').value='';	
	document.getElementById('tell_tuonome').value='';

	document.getElementById('send_status').innerHTML='';

	document.getElementById('opacizzato').className = 'nascosto';
    document.getElementById('container').className = 'nascosto';
	//var body_ = document.getElementsByTagName('body')[0];
	
	// Su IE6 non funziona, inoltre spacca la grafica
	if (typeof document.body.style.maxHeight != "undefined") {
		// IE 7, mozilla, safari, opera 9
		var body_ = document.getElementsByTagName('body')[0];
		body_.style.overflow='auto'; 
	} 
	
	
    return false;	
}

/**
* Inizializza la google map all'apertura della pagina e posiziona i marker dall'xml
**/
function loadGmap() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl3D());			
		map.enableScrollWheelZoom();
		map.setCenter(new GLatLng(42,13),5);
		GDownloadUrl("location.xml", function(data) {
			var xml = GXml.parse(data);
			var markers = xml.documentElement.getElementsByTagName("marker");
			for (var i = 0; i < markers.length; i++) {
				var nome_tmp  = (markers[i].getAttribute("nome")).toLowerCase();
				var nome = nome_tmp.substr(0,1).toUpperCase() + nome_tmp.substr(1,nome_tmp.length);
				var indirizzo = markers[i].getAttribute("indirizzo");
				var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),	parseFloat(markers[i].getAttribute("lng")));
				var marker = createMarker(point, nome, indirizzo);
				map.addOverlay(marker);
			}
		});
	} else {
		alert("Sorry, the Google Maps API is not compatible with this browser");
	}
}

/**
* Fa uno zoom proporzionale sulla google map a seconda del livello di dettaglio del punto cercato
* e propone indirizzi alternativi in caso di corrispondenze multiple con il punto cercato
**/
function showAddress() {
	if (GBrowserIsCompatible()) {
		// Create a Client Geocoder
		var geo = new GClientGeocoder();
		
		// Array for decoding the failure codes
		var reasons=[];
		reasons[G_GEO_SUCCESS]            = "Success";
		reasons[G_GEO_MISSING_ADDRESS]    = "Missing Address: The address was either missing or had no value.";
		reasons[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
		reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
		reasons[G_GEO_BAD_KEY]            = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
		reasons[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
		reasons[G_GEO_SERVER_ERROR]       = "Server error: The geocoding request could not be successfully processed.";
		
		var search = document.getElementById("search").value;
		// Perform the Geocoding    
		geo.getLocations(search, function (result) {
			// If that was successful
			if (result.Status.code == G_GEO_SUCCESS) {
				// How many resuts were found
				document.getElementById("message").innerHTML = "Trovati " +result.Placemark.length +" risultati.";
				// Loop through the results, placing markers
				for (var i=0; i<result.Placemark.length; i++) {
					var p = result.Placemark[i].Point.coordinates;
					var marker = new GMarker(new GLatLng(p[1],p[0]));
					document.getElementById("message").innerHTML += "<br />"+(i+1)+": "+ result.Placemark[i].address;
					//document.getElementById("message").innerHTML += "<br />"+(i+1)+": "+ result.Placemark[i].address + marker.getPoint();
					//map.addOverlay(marker);
				}
				// centre the map on the first result
				var p = result.Placemark[0].Point.coordinates;
				// Look for the bounding box of the first result
				var N = result.Placemark[0].ExtendedData.LatLonBox.north;
				var S = result.Placemark[0].ExtendedData.LatLonBox.south;
				var E = result.Placemark[0].ExtendedData.LatLonBox.east;
				var W = result.Placemark[0].ExtendedData.LatLonBox.west;
				var bounds = new GLatLngBounds(new GLatLng(S,W), new GLatLng(N,E));
				// Choose a zoom level that fits
				var zoom = map.getBoundsZoomLevel(bounds);
				map.setCenter(bounds.getCenter(),zoom);
				// var points=[new GLatLng(N,W),new GLatLng(N,E),new GLatLng(S,E),new GLatLng(S,W),new GLatLng(N,W)];
				// map.addOverlay(new GPolyline(points));
			}
			// Decode the error status
			else {
				var reason="Code "+result.Status.code;
				if (reasons[result.Status.code])
					reason = reasons[result.Status.code]
				alert('Could not find "'+search+ '" ' + reason);
			}
		});
	}
	// display a warning if the browser was not compatible
	else {
		alert("Sorry, the Google Maps API is not compatible with this browser");
	}
}

/**
* Crea l'icona personalizzata per la google map
**/
function createIcon(){
	icon = new GIcon();
	icon.image = '../images/icona_bicchiere_zero_map.png';
	icon.iconAnchor = new GPoint(10, 20);
	icon.infoWindowAnchor = new GPoint(5, 1);
	//icon.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
	//icon.iconSize = new GSize(12, 20);
	//icon.shadowSize = new GSize(22, 20);
}

/**
* Crea il marker sulla google map
**/
function createMarker(point, nome, indirizzo) {
	//createIcon();
	//var marker = new GMarker(point, icon);
	var marker = new GMarker(point);
	var html = "<div style='width:300px'>" + nome + "<br /><b>Indirizzo:</b><br />" + indirizzo + "</div>";
	GEvent.addListener(marker, 'click', function() {
		marker.openInfoWindowHtml(html);
	});
	return marker;
}

/**
* Funzione per la creazione del link a twitter per la condivisione dei contenuti tramite Social Bookmarking
**/
function twitter(){
	var linkArr=document.getElementsByTagName('link');
	var a=false;
	var u='';
	for(var i=0;i<linkArr.length&&!a;i++){
		var l=linkArr[i];
		for(var x=0;x<l.attributes.length;x++){
			if(l.attributes[x].nodeName.toLowerCase()=='rel'){
				p=l.attributes[x].nodeValue.split('%20');
				for (y=0;y<p.length;y++){
					if(p[y]=='short_url'||p[y]=='shortlink')a=true;
				}
			}
		}
		if(a)u=l.href;
	}
	if(a){
		l='http://twitter.com/home/?status='+encodeURIComponent(u+'%20'+document.title);
	}else{
		l='http://bernhardhaeussner.de/tools/tweetthis.php?link='+encodeURIComponent(window.location.href)+'&title='+encodeURIComponent(document.title);
	}
	window.open(l,'tweetthisbklmt');
}
