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


function slideBox()
{
try
{		
	$('#slider-wrapper').cycle({
		fx: 'fade',
		speed: 1000,
		timeout: 6500, 
		next:   '#nextPhoto', 
		prev:   '#prevPhoto' 
	});
}
catch(e) {toDebug(e.toString());}
}


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());}
}

function formPrevBambini()
{
try
{	
	$('select.prev_children option').click(function(){
		var $this = $(this);
		var nchild = parseInt($this.val());
		if (nchild>3) nchild=3;
		if (nchild==0){
			$('td.age_children').empty()();
		}
		if (nchild>0){
			var contenuto = "";
			var ncol = (nchild*2)-1;
			if(ncol == 1) ncol = 2;
			contenuto+= "<table><tr><td colspan='"+ncol+"'>Et&agrave; bambini</td></tr><tr>";
			for (i=1; i<=nchild; i++){
				contenuto+= "<td><div><select><option selected='' value='-1' label='-1'>-</option><option value='0' label='0'><1</option><option value='1' label='1'>1</option><option value='2' label='2'>2</option><option value='3' label='3'>3</option><option value='4' label='4'>4</option><option value='5' label='5'>5</option><option value='6' label='6'>6</option><option value='7' label='7'>7</option><option value='8' label='8'>8</option><option value='9' label='9'>9</option><option value='10' label='10'>10</option><option value='11' label='11'>11</option><option value='12' label='12'>12</option></select></div></td>";
				if(i!=nchild || ncol==2){
					contenuto += "<td width='10'></td>"
				}
			}
			contenuto+= "</tr></table>";
			$('.age_children').html(contenuto);
		}
	});
}
catch(e) {toDebug(e.toString());}
}



/*
creazione del cookie
se c'e' 'days' setta il cookie per il numero di giorni passato
altrimenti in base alla data
*/
function setCookie(name, value, days)
{
try
{
	// se passato il numero dei giorni setto la validità
	if(days)
	{
	    $.cookie(name, value, { expires: days, path: '/'});
	}
	//	altrimenti rimane valido per la sessione
	else 
	{
		$.cookie(name,value);
		/*
	    var date = new Date();
	    date.setTime(date.getTime() + (3 * 24 * 60 * 60 * 1000));
	    $.cookie(name,value, { expires: date, path: '/'});
	    */
	}
}    
catch(e) {toDebug(e.toString());}
}

function closeBlox()
{
try
{
	var closebutton = $('#close');
	closebutton.toggle(function(event){
		event.preventDefault(); 
		$('#preventivo').children('div').fadeOut('fast',function(){
			$('#preventivo').animate({"height" : "32px", "padding-bottom" : "0", "top" : "1px"}, function(){
				$('#preventivo').css("background-image", "url(images/background/preventivo-background-close.png)");
				$('#preventivo').addClass("prev_close");
				setCookie('box_close', true);
			});
		});
	}, function(event){
		event.preventDefault(); 
		$('#preventivo').css("background-image", "url(images/background/preventivo-background.png)");
		$('#preventivo').animate({"height" : "140px", "padding-bottom" : "15px", "top" : "50px"}, function(){
			$('#preventivo').children('div').fadeIn('fast');
			$('#preventivo').removeClass("prev_close");
			setCookie('box_close', null);
		});
	});
}
catch(e) {toDebug(e.toString());}
}


/**
 * Applicato sul menu con id '#menu'.
 * Effetto di apertura e chiusura sulle voci che presentano un sottomenu. La classe '.active' è applicata sia sul 'ul' che sul 'li'. 
*/

function subMenuAnimation()
{
try
{
	var menu = $('#menu').children('ul').children('li');
	var sottomenu = $('.sottomenu');
	
	sottomenu.each(function(){
		var $this = $(this);
 		$this.children('li').each(function(){
			var $this = $(this);
			if($this.is('.active')){
				$this.parent('ul').addClass('active');
				$this.parent('ul').parent('li').addClass('active');
				$this.parent('ul').slideDown("normal");
			}
		});
		
		$this.siblings('a').bind('click',function(event){
			var $this = $(this);
			if($this.siblings().is('ul')){
				event.preventDefault();
				menu.children('ul').slideUp("normal");
				if($this.siblings('ul').is(':visible')){
					$this.siblings('ul').slideUp("normal");
				}else{
					$this.siblings('ul').slideDown("normal");
					$this.siblings('ul').hover(function(){ $this.css({'color':'#FFF'}) },function(){ $this.css({'color':'#824776'}) });
				}
			}else{
				menu.children('ul').children('li').removeClass('active');
				$this.parent('li').addClass('active');
				$this.siblings('ul').addClass('active');
			}
		});
	});
}
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()
{ 

	allClick();
	slideBox();
	cancellaForm();
	
	//$(".date").datepicker({option:' it', dateFormat: 'dd/mm/yy',showOn: 'both', buttonImage: 'images/calendarietto.png', buttonText: '', buttonImageOnly: true});
	$("#checkin").datepicker({option:' it', dateFormat: 'dd-mm-yy',showOn: 'both', buttonText: '', buttonImageOnly: true, numberOfMonths: 3});
	$("#checkout").datepicker({option:' it', dateFormat: 'dd-mm-yy',showOn: 'both', buttonText: '', buttonImageOnly: true, numberOfMonths: 3});
	
	$("#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);
		}
	);
	
	$("a[rel^='prettyPhoto']").prettyPhoto({theme: 'facebook'});
	
	closeBlox();
});

$j(window).load(function()
{
	formPrevBambini();
	subMenuAnimation();
	
	if($.cookie('box_close')) $('#close').trigger('click');
});

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

});

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

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></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";
	}

}

function sendCallBack() {
		if ((document.getElementById("frm_nome").value == "") || (document.getElementById("frm_nome").value == "nome*")) {
			alert('Inserisci il tuo nome');
			document.getElementById("frm_nome").focus();
			return false;
		}
		if ((document.getElementById("frm_telefono").value == "") || (document.getElementById("frm_telefono").value == "telefono*")) {
			alert('Inserisci il tuo numero di telefono');
			document.getElementById("frm_telefono").focus();
			return false;
		}
		if ((document.getElementById("frm_orario").value == "") || (document.getElementById("frm_orario").value == "orario*")) {
			alert('A che ora vuoi essere richiamato?');
			document.getElementById("frm_orario").focus();
			return false;
		}
		if (document.getElementById("frm_privacy").checked == false) {
			alert("E' necessario prendere visione delle normative della privacy");
			return false;
		}
		var dest = "back";
		document.getElementById("wrapper_form_callback").action = "call" + dest + ".php";
    	document.getElementById("wrapper_form_callback").submit();
	}
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 sendForm(){
	if (document.getElementById("form_nome").value === ''){
		alert ('Il Nome e\' un campo obbligatorio');
		document.getElementById("form_nome").focus();
		return false;
	}
	if (document.getElementById("form_cognome").value === ''){
		alert ('Il Cognome e\' un campo obbligatorio');
		document.getElementById("form_cognome").focus();
		return false;
	}
    
	if (document.getElementById("form_email").value === ''){
		alert ('L\'E-mail e\' un campo obbligatorio');
		document.getElementById("form_email").focus();
		return false;
	} else {
		var nm,p='';
		val=document.getElementById("form_email").value;
		p=val.indexOf('@');
		if (p<1 || p==(val.length-1)){
			alert ('L\'E-mail non e\' valida');
			document.getElementById("form_email").focus();
			return false;
		}
	}

	if (document.getElementById("form_privacy").checked === false) {
		alert("Accettare il trattamento della Privacy");
		return;
	}
	var dest = "info";
	document.getElementById("form_contatti").action = dest + "rmazioni" + ".php";
	document.getElementById("form_contatti").submit();
}

