 $(document).ready(function(){
 
 Shadowbox.init({
    // a darker overlay looks better on this particular site
    overlayOpacity: 0.8
    // setupDemos is defined in assets/demo.js
});

 
	$("#sendmail").click(function(){
		var valid = '';
		var isr = ' is required.';
		var name = $("#nome").val();
		var language = $("#language").val();
		var mail = $("#mail").val();
		var subject = $("#cognome").val();
		var text = escape($("#messaggio").val());
		text = text.replace(/\n/g, "<br />")
        .replace(/\n\n+/g, '<br /><br />');
		
		if (name.length<1) {
			if(language == 'EN'){
				valid += '<br />Field First name is required.';
			} 
			else if(language == 'ES'){
				valid += '<br />El campo Nombre es obligatorio.';
			}
			 else{
				valid += '<br />Il campo Nome &egrave richiesto.';
				}
				
		}
		if (subject.length<1) {
			if(language == "EN"){
				valid += '<br />Field Last name is required.';			
			}
			else if(language == "ES"){
				valid += '<br />El campo Apellidos es obligatorio.';			
			}			
			 else{
				valid += '<br />Il campo Cognome &egrave richiesto.';
				}

		}
		
		if (!mail.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
			if(language == "EN"){
				valid += '<br />A valid e-mail is required.';			
			}
			else if(language == "ES"){
				valid += '<br />Es necesario introducir un email v&aacute;lido.';			
			}
			 else{
				valid += '<br />&Egrave; richiesta una e-mail valida.';
				}
		}

		if (text.length<1) {
			if(language == "EN"){
				valid += '<br />Field Message is required.';			
			}
			else if(language == "ES"){
				valid += '<br />El campo mensaje es obligatorio.';			
			}
			 else{
				valid += '<br />Il campo Messaggio &egrave richiesto.';
				}
		}
		if (valid!='') {
			$("#response").fadeIn("slow");
			if(language == "EN"){
				$("#response").html("Errors:"+valid);			
			}
			else if(language == "ES"){
				$("#response").html("Errores:"+valid);			
			}
			 else{
				$("#response").html("Errori:"+valid);
				}

		}
		else {
			if(language == 'EN'){
				var datastr ='name=' + name + '&mail=' + mail + '&subject=' + subject + '&text=' + text + '&language=' + language;
			}
			else if(language == 'ES'){
				var datastr ='name=' + name + '&mail=' + mail + '&subject=' + subject + '&text=' + text + '&language=' + language;
			}
			 else {
				var datastr ='name=' + name + '&mail=' + mail + '&subject=' + subject + '&text=' + text;
			}
			$("#response").css("display", "block");
			
			if(language == 'EN'){
				$("#response").html("Sending mail .... ");			
			}
			else if(language == 'ES'){
				$("#response").html("Envio en curso .... ");			
			}
			 else{
				$("#response").html("Invio in corso .... ");
				}		
			document.getElementById("sendmail").disabled = true;
			$("#response").fadeIn("slow");
			setTimeout("send('"+datastr+"')",2000);
		}
		return false;
	});

$("#galleria").hover(function(){
				$("#wrap_menu").stop().fadeTo("slow", 0.5); // This should set the opacity to 100% on hover
				$("#menu").stop().fadeTo("slow", 0.5); // This should set the opacity to 100% on hover
				},function(){
				$("#wrap_menu").stop().fadeTo("fast", 0.9); // This should set the opacity back to 60% on mouseout
				$("#menu").stop().fadeTo("fast", 1.0); // This should set the opacity back to 60% on mouseout
			});

});

function send(datastr){
	var sender_mail;
	if (($("#language").val() == 'EN') || $("#language").val() == 'ES'){
		sender_mail = "../bin/mail.php";
	} else {
		sender_mail = "bin/mail.php";
	}
	$.ajax({	
		type: "POST",
		url: sender_mail,
		data: datastr,
		cache: false,
		success: function(html){
		$("#response").fadeIn("slow");
		$("#response").html(html);
		setTimeout('$("#response").fadeOut("slow")',2000);
	}
	});
}

function initialize() {

	var latlng = new google.maps.LatLng(43.094345,  11.78225);
	var myLatlng = new google.maps.LatLng(43.092631, 11.782289);
	var contentString = '<b style="color:#000">Palazzo Carletti</b><br />Via dell Opio nel Corso, 3<br />53045 Montepulciano (SI)<br />Phone +39 0578 756080<br /><a href="mailto:info@palazzocarletti.com">info@palazzocarletti.com</a>';
	
	var myOptions = {
		zoom: 17,
		center: latlng,
		scrollwheel:true,
		navigationControl: true,
		mapTypeControl: true,
		mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,position: google.maps.ControlPosition.TOP_RIGHT},
		navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL,position: google.maps.ControlPosition.LEFT_CENTER},
		mapTypeId: google.maps.MapTypeId.SATELLITE
	};
	
	//Ceo la mappa e la aggiungo al div galleria
	var map = new google.maps.Map(document.getElementById("galleria"), myOptions);
	
	var image = '../img/marker.png';
	
	//Creo il Marker
	var marker = new google.maps.Marker({
		position: myLatlng,
		map : map,
		//icon: image,
		title:"Palazzo Carletti"
	});
	
	//Creo L'infoWindow
	var infowindow = new google.maps.InfoWindow({
		content: contentString
	});
	
	//Aggiungo il listener al marker per riaprire l'infowindow se chiusa
	google.maps.event.addListener(marker, 'click', function() {
		infowindow.open(map,marker);
		//locateFF(); //funzione per geolocalizzare la posizione del browser
	});
	
	//Apro l'info window al caricamento
	infowindow.open(map,marker);
	
	
//--------------------funzioni per la Geolocalizzazione del browser-------------------------
	
	function sprite(id){
		var origin = new google.maps.Point(0, id * 34);
		var icon = new google.maps.MarkerImage(
			"http://maps.google.com/intl/fi_ALL/mapfiles/all_markers.png",
			new google.maps.Size(32, 34), 
			origin
		);
		return icon;
	}
	
	/**
	* markers 
	*/
	function makeMarker(point, icon){
		var spriteMarker = new google.maps.Marker({
			map: map,
			position: point,
			icon: sprite(icon)
		});
	map.panTo(point);
	
	/*
	google.maps.event.addListener(map, 'idle', function(){
	infowindow.open(map,spriteMarker); LISTENER PER UN EVENTO
	map.setZoom(15);
	});
	*/
	var infowindow = new google.maps.InfoWindow({
		content: "Ti trovi qui!"
	});
	
	google.maps.event.addListener(spriteMarker, 'click', function(){
		infowindow.open(map,spriteMarker);
		});
	infowindow.open(map,spriteMarker);
	}
	
	/**
	* test amd functions for FF3.5+
	*/
	if(typeof navigator.geolocation != "undefined"){
		function showPosition(position) {
			var location = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
			makeMarker(location, 0);
		} 
		function report(error){
			alert(error.message);
		}
		window.locateFF = function(){
			navigator.geolocation.getCurrentPosition(showPosition, report);
		}
	}

	/**
	* test and functions for Google Gears
	*/
	if((typeof google != "undefined") && (typeof google.gears != "undefined")){
		var geo = google.gears.factory.create('beta.geolocation');
		function updatePosition(position) {
			var location = new google.maps.LatLng(position.latitude, position.longitude);
			makeMarker(location, 1);
		}
		function handleError(error) {
			alert(error.message);
		}
		window.locateGears = function(){
			geo.getCurrentPosition(updatePosition, handleError);
		}
	}
}
