var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2288187-18']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();


$(function () 
{   
	var stepvalidazione = 1;
	var err = false;
	
	if (typeof(fullDays) != "undefined") {
		$(".user").before("<p style=\"font-size: 12px\">Nelle date indicate in rosso non abbiamo disponibilità di camere.</p>");
	}
	
    $('#startdate, #enddate').datepicker({
        beforeShow: customRange,
        dateFormat: "dd/mm/yy",
        firstDay: 1, 
		beforeShowDay: giornipieni,
        changeFirstDay: false,
		showAnim: 'fadeIn',
    });
	
	function giornipieni(date) {
		var mese = date.getMonth();
		var giorno = date.getDate();
		if (typeof(fullDays) != "undefined") {
			var i = 0;
			var $return = new Array();
			$return[0] = true;
			while(i<fullDays.length) {
				var arraydata = fullDays[i].split("/");
				var full_giorno = arraydata[0];
				var full_mese = arraydata[1];
				if(mese == full_mese - 1 && giorno == full_giorno) {
					$return[0] = false;
					$return[1] = "full";
				}
				i++;
			}
			return $return;
		}
	}

	$( "#dialog:ui-dialog" ).dialog( "destroy" );
	$( "#dialog-message" ).dialog({
		autoOpen: false,
		modal: true,
		buttons: {
			Ok: function() {
				$( this ).dialog( "close" );
			}
		}
	});
	$( "#dialog-confirm" ).dialog({
		autoOpen: false,
		modal: true,
		buttons: {
			Ok: function() {
				$( this ).dialog( "close" );
			}
		}
	});

	$( "#prenota" ).click(function validate() {
		var startdate = $("#startdate").val();
		var enddate = $("#enddate").val();
		if (!startdate || !enddate) {
			$( "#dialog-message" ).dialog( "open" );
		}
		else {
			/*$( "#prenotazioni" ).attr('method', 'post');
			$( "#prenotazioni" ).attr('action', '#');
			$( "#prenotazioni" ).submit();*/
			if (stepvalidazione == 1) {
				$( "#prenotazioni > .user" ).animate({
					height: '370px',
					easing: 'EaseInOut'
				  }, 2000, function() {
					// Animation complete.
				  });
				stepvalidazione = 2;
			}
			else {
				err = false;
				if (!$("#nome").val()) {
					$("#nome").addClass("err");
					$("#nome").click(function (){ $(this).removeClass("err"); });
					err = true;
				}
				if (!$("#cognome").val()) {
					$("#cognome").addClass("err");
					$("#cognome").click(function (){ $(this).removeClass("err"); });
					err = true;
				}
				if (!$("#persone").val()) {
					$("#persone").addClass("err");
					$("#persone").click(function (){ $(this).removeClass("err"); });
					err = true;
				}
				if (!$("#telefono").val()) {
					$("#telefono").addClass("err");
					$("#telefono").click(function (){ $(this).removeClass("err"); });
					err = true;
				}
				if (!$("#email").val()) {
					$("#email").addClass("err");
					$("#email").click(function (){ $(this).removeClass("err"); });
					err = true;
				}
				if (!err) {
					$.post("mail.php", {
						startdate: $("#startdate").val(),
						enddate: $("#enddate").val(),
						nome: $("#nome").val(),
						cognome: $("#cognome").val(),
						persone: $("#persone").val(),
						telefono: $("#telefono").val(),
						email: $("#email").val()
					}, function(data) {
						$("#dialog-confirm").dialog( "open" );
						$("#dialog-text").html(data);	
					});
				}
			}
		}
	});
	
	$('ul#header-img').innerfade({
		speed: 2000,
		timeout: 5000,
		type: 'sequence',
		containerheight: '300px'
	});
	
	$('#menu-bar > .menu > ul > li').hover(function() {
  	  x = $(this).position().left + 25;
	  w = $(this).width();
	  
	  if(!$('#menu-highlight').height()) {
		$('#menu-highlight').css('left', x);
		$('#menu-highlight').width(w);
		$('#menu-highlight').animate({
		  height: '4'
		  }, 500);
	  }
	  else {
	    $('#menu-highlight').stop();
	    $('#menu-highlight').animate({
		  left: [x, 'easeOutCubic'],
		  width: w,
		  height: '4'
	    }, 1000);
	  }
	});

	$('#menu-bar > .menu').mouseleave(function() {
	  $('#menu-highlight').stop();
	  $('#menu-highlight').animate({
	    height: '0'
	  }, 400);
	});


});

function customRange(input) { 
    var min = new Date(),
        dateMin = min,
        dateMax = null,
        dayRange = 6;

    if (input.id === "startdate") {
        if ($("#enddate").datepicker("getDate") != null) {
            dateMax = $("#enddate").datepicker("getDate");
            /*dateMin = $("#enddate").datepicker("getDate");
            dateMin.setDate(dateMin.getDate() - dayRange);
            if (dateMin < min) {
                dateMin = min;
            }*/
        }
        else {
            //dateMax = new Date;
        }                      
    }
    else if (input.id === "enddate") {
        //dateMax = new Date;
        if ($("#startdate").datepicker("getDate") != null) {
            dateMin = $("#startdate").datepicker("getDate");
            /*var rangeMax = new Date(dateMin.getFullYear(), dateMin.getMonth(),dateMin.getDate() + dayRange);

            if(rangeMax < dateMax) {
                dateMax = rangeMax; 
            }*/
        }
    }
    return {
        minDate: dateMin, 
        maxDate: dateMax
    };  
}
