/**
 * Trova i box con classe "allclick".
 * All'interno cerca dei link, ed estende l'href sul click di tutto il box
 * @return null
 */
function checkAllclick()
{
try
{
	var clickbox = $('.allclick');
	clickbox.each(function()
	{
		var $this = $(this);
		//	ad ogni box allclick assegno il cursore pointer
		$this.css('cursor','pointer');
		//	sul click mando la pagine all'href del link sottostante
		$this.bind('click', function(){window.location.href=$('a',this).attr('href');});
	});
}
catch(e) {toDebug(e.toString());}
}


function slideBox()
{
	try
	{
		$('#slider-offerte').cycle({
			fx: 'fade',
			speed: 2000,
			timeout: 6500,
			next:   '#nextPhoto',
			prev:   '#prevPhoto'
		});

		$('#offerte-bike').cycle({
			fx: 'fade',
			speed: 2000,
			timeout: 6500,
			next:   '#nextBike',
			prev:   '#prevBike'
		});

		/*$('#slider-video').cycle({
			fx: 'scrollHorz',
			speed: 2000,
			timeout: 0,
			next:   '#nextVideo',
			prev:   '#prevVideo'
		});*/
	}
	catch(e) {toDebug(e.toString());}
}

/**
 * Trova i gli input text con classe "input_form".
 * Quando si clicca all'interno del form la scritta originale scopare e ricompare se non si digita nulla.
 * @return null
*/
function cancellaForm()
{
try
{
	$(".input_form:text").bind('focus blur',function(event)
	{
		var value = $(this).attr("originalValue");
		if (event.type == "focus") {
			if ($(this).val() == "" || $(this).val() == value) {
				$(this).val("");
			}
		}else {
			if ($(this).val() == "") {
				$(this).val(value);
			}
		}
	});
	$("textarea.input_form").bind('focus blur',function(event)
	{
		var value = $(this).attr("originalValue");
		if (event.type == "focus") {
			if ($(this).text() == "" || $(this).text() == value) {
				$(this).text("");
			}
		}else {
			if ($(this).text() == "") {
				$(this).text(value);
			}
		}
	});
}
catch(e) {toDebug(e.toString());}
}

/*****************************************************************************************/
//	inizio funzioni di debug
debug_msg = false;
function toDebug(string)
{
	if (debug_msg)
	{
		if((typeof window.console=="undefined")) {alert(string);}
		else {
			console.error();
			console.group(string);
			console.trace();
			console.groupEnd();
		}
	}
}
function toConsole(string)
{
	if((typeof window.console=="undefined")) {alert(string);}
	else console.log(string);
}
//fine funzioni di debug

function $j(argument)
{
	var result = jQuery(argument);
	if (result.length == 0) return null;
	else return result;
}

$j(document).ready(function()
{

	checkAllclick();
	slideBox();
	cancellaForm();

	$('#slide_header').cycle();

	/*$('#checkin, #checkout').datepicker({
		numberOfMonths: 3,
		showButtonPanel: true,
		minDate: 0
	});*/

	var openDate = new Date( new Date().getFullYear(), 3, 5, 0, 0, 0 );
	var closeDate = new Date( new Date().getFullYear(), 10, 2, 0, 0, 0 );

	$("#checkin").datepicker({option:'en', dateFormat: 'dd-mm-yy',showOn: 'both', buttonImage: 'images/calendarietto.png', buttonText: '', buttonImageOnly: true, numberOfMonths: 3});
	$("#checkout").datepicker({option:'en', dateFormat: 'dd-mm-yy',showOn: 'both', buttonImage: 'images/calendarietto.png', buttonText: '', buttonImageOnly: true, numberOfMonths: 3});

	jQuery( "#checkin, #checkout" ).datepicker( "option", "minDate", new Date(openDate.getFullYear(), openDate.getMonth(), openDate.getDate()) );
	jQuery( "#checkin, #checkout" ).datepicker( "option", "maxDate", new Date(closeDate.getFullYear(), closeDate.getMonth(), closeDate.getDate()) );

	/*$.datepicker.regional['it'] = {
		closeText: 'Chiudi',
		prevText: '&#x3c;',
		nextText: '&#x3e;',
		currentText: 'Oggi',
		monthNames: ['Gennaio','Febbraio','Marzo','Aprile','Maggio','Giugno',
			'Luglio','Agosto','Settembre','Ottobre','Novembre','Dicembre'],
		monthNamesShort: ['Gen','Feb','Mar','Apr','Mag','Giu',
			'Lug','Ago','Set','Ott','Nov','Dic'],
		dayNames: ['Domenica','Luned&#236','Marted&#236','Mercoled&#236','Gioved&#236','Venerd&#236','Sabato'],
		dayNamesShort: ['Dom','Lun','Mar','Mer','Gio','Ven','Sab'],
		dayNamesMin: ['Do','Lu','Ma','Me','Gi','Ve','Sa'],
		weekHeader: 'Sm',
		dateFormat: 'dd-mm-yy',
		buttonImage: 'images/calendarietto.png',
		buttonImageOnly: true,
		firstDay: 1,
		isRTL: false,
		showMonthAfterYear: false,
		yearSuffix: ''};
	$.datepicker.setDefaults($.datepicker.regional['it']);*/

	$("#checkin").change(
		function(){
			date1 = $("#checkin").datepicker( 'getDate' );
			// devo controllare che la data sia valida
			check = false;
			dateCheck = date1;
			date2 = new Date(dateCheck);
			$("#checkout").datepicker('option', 'minDate',date2);
		}
	);

	// Newsletter signup
	$("#frmSS33, #frmSS34").submit(function(e) {
		var mail=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;

		var iEmail = $("#email");
		var iPrivacy = $("#privacy");

		if( iEmail.val() == "" || !mail.test( iEmail.val())) {
			alert("La preghiamo di inserire un indirizzo email valido");
			iEmail.focus();
			return false;
		}
		else if(!iPrivacy.attr("checked")){
			alert("La preghiamo di autorizzare l'uso dei suoi dati secondo la legge sulla Privacy.(art. 13 L. 675/03)");
			iPrivacy.focus();
			return false;
		}
		else{return true;}
	});

	// start_Booking
	var maxAdults = 7;
	var maxChild = 4;
	var maxChildAge = 17;
	var labelRoom = "Camera";
	var labelAdults = "Adulti";
	var labelChildren = "Bambini";
	var labelChildAge = "eta' bambini";

	jQuery("#numRooms").change( function(){
		var nRooms = jQuery(this).val();
		var roomsOptions = jQuery("#contOption table:first");
		var roomRow = "";

		roomsOptions.find("tr:gt(0)").remove();

		for( var i=1; i<=nRooms; i++ )
		{
			// Per la 1a camera non stampo l'etichetta "Camera 1"
			if( i > 1 ) roomRow = roomRow + "<tr><td><strong>" + labelRoom+" "+i+"</strong></td></tr>";

			// Adulti
			roomRow = roomRow + "<tr><td><select name='bform[reqRooms]["+i+"][adults]'>";

			for( var x=1; x<=maxAdults; x++ )
			{
				roomRow = roomRow + "<option value='"+x+"'>"+x+"</option>";
			}

			// Bambini
			roomRow = roomRow + "</select>";
			if( i > 1 ) roomRow = roomRow +labelAdults;
			roomRow = roomRow + "</td><td><select name='bform[reqRooms]["+i+"][child]' class='absChildren' data-roomnumber='"+i+"'>";

			for( var x=0; x<=maxChild; x++ )
			{
				roomRow = roomRow + "<option value='"+x+"'>"+x+"</option>";
			}

			// Età Bambini (creo la colonna)
			roomRow = roomRow + "</select> ";
			if( i > 1 ) roomRow = roomRow +labelChildren;
			roomRow = roomRow + "</td><td></td></tr>";
		}

		roomsOptions.append( roomRow );
	});

	// Cambio numero bambini
	jQuery(".absChildren").live( "change", function(){
		var tdAgeChildren = jQuery(this).parent().next("td");
		var roomNumber = jQuery(this).data("roomnumber");
		var nChildren = jQuery(this).val();
		var childAgeRow = "";

		if( nChildren > 0 ){
			//jQuery("#contOption table:first tr:first td:last").html("Eta bambini");
		}
		else{
			jQuery("#contOption table:first tr:first td:last").html("");
		}

		tdAgeChildren.html("");

		for( var x=1; x<=nChildren; x++ )
		{
			childAgeRow = childAgeRow + '<select name="bform[reqRooms]['+roomNumber+'][childAge]['+x+']">';
			for( var y=0; y<=maxChildAge; y++ )
			{
				if( y === 0 ){
					childAgeRow = childAgeRow + '<option value="'+y+'">&lsaquo; 1</option>';
				}
				else{
					childAgeRow = childAgeRow + '<option value="'+y+'">'+y+'</option>';
				}
			}

			childAgeRow = childAgeRow + "</select>";
		}

		childAgeRow = childAgeRow + " " + labelChildAge;

		tdAgeChildren.append( childAgeRow );
	});

	// ABS Submit
	jQuery("#abs_form_invia").click( function(){

		if( jQuery("#checkin").val() != "" && jQuery("#checkout").val() != "" ){
			jQuery("#abs_form").submit();
		}
	});
	// end_Booking

});

function ctrform(){
	if (document.getElementById("checkin").value==''){
		document.getElementById("checkin").focus();
		alert("Data di Arrivo obbligatoria");
		return false;
	}
	if (document.getElementById("checkout").value=='') {
		document.getElementById("checkout").focus();
		alert("Data di Partenza obbligatoria");
		return false;
	}
	document.getElementById("form_ins").submit();
}
function cambia_bambini(n) {
	frm = document.getElementById("form_ins");
	camere = parseInt(document.getElementById("numRooms").value);
	if (n>4) n=4;
	if ((n>0)&&(camere==1))
	{
		var contenuto = "";
		var name = "";
		for (i=1; i<=n; i++){
			name="bform[reqRooms][1][childAge]["+i+"]";
			contenuto += "<select name='"+name+"' id='"+name+"'><option value='-1'>-?-</option><option value='0'>&lt;1</option><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option><option>6</option><option>7</option><option>8</option><option>9</option><option>10</option><option>11</option><option>12</option><option>13</option><option>14</option><option>15</option><option>16</option><option>17</option><option>18</option></select>";
		}
		document.getElementById("bam_div").innerHTML=contenuto+"&nbsp;";
		document.getElementById("bam_div").style.visibility = "visible";
		document.getElementById("bam_div").style.display = "block";
		document.getElementById("bam_div1").style.visibility = "visible";
		document.getElementById("bam_div1").style.display = "block";
	} else {
		document.getElementById("bam_div").style.visibility = "hidden";
		document.getElementById("bam_div").style.display = "none";
		document.getElementById("bam_div1").style.visibility = "hidden";
		document.getElementById("bam_div1").style.display = "none";
	}

}

$j(window).load(function()
{


});

$j(window).resize(function()
{

});

/*****************************************************************************************/


  function inviamail(){
    document.fmail.submit();
  }
  function validateformesperto(){
    //alert();
		referente = document.generale.referente.value;
		if ((referente == "") || (referente == "undefined")) {
			alert('Il referente inserito non è valido');
		   document.generale.referente.focus();
			return false;
		}
		telefono = document.generale.telefono.value;
		if ((telefono == "") || (telefono == "undefined")) {
			alert('Il telefono inserito non è valido');
		   document.generale.telefono.focus();
			return false;
		}
		email = document.generale.email.value;
		if ((email == "") || (email == "undefined")) {
			alert('L'+chr(44)+'email inserita non è valida');
		   document.generale.email.focus();
			return false;
		}
		messaggio = document.generale.messaggio.value;
		if ((messaggio == "") || (messaggio == "undefined")) {
			alert('Il messaggio inserito non è valido');
		   document.generale.messaggio.focus();
			return false;
		}
		document.generale.action = document.getElementById("pagina").value;
    	document.generale.submit();
  }

	function sendCallBack() {
		if ((document.getElementById("cb_nome").value == "") || (document.getElementById("cb_nome").value == "nome*")) {
			alert('Inserisci il tuo nome');
			document.getElementById("cb_nome").focus();
			return false;
		}
		if ((document.getElementById("cb_telefono").value == "") || (document.getElementById("cb_telefono").value == "telefono*")) {
			alert('Inserisci il tuo numero di telefono');
			document.getElementById("cb_telefono").focus();
			return false;
		}
		if ((document.getElementById("cb_orario").value == "") || (document.getElementById("cb_orario").value == "orario*")) {
			alert('A che ora vuoi essere richiamato?');
			document.getElementById("cb_orario").focus();
			return false;
		}
		if (document.getElementById("cb_privacy").checked == false) {
			alert("E' necessario prendere visione delle normative della privacy");
			return false;
		}
		var dest = "back";
		document.getElementById("f_callback").action = "call" + dest + ".php";
    	document.getElementById("f_callback").submit();
	}
