/**
 * 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
{
 	$('#offerte-wrapper').cycle({
		fx: 'scrollRight',
		speed: 2000,
		timeout: 6500
	}).mouseenter(function(){
		$('#offerte-wrapper').cycle('pause');
	}).mouseleave(function(){
		$('#offerte-wrapper').cycle('resume');
	});

	$('#slider-wrapper').cycle({
		fx: 'fade',
		speed: 1000,
		timeout: 4500,
		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());}
}

/*****************************************************************************************/
//	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();

	// start_Datepicker
	var openDate = new Date( new Date().getFullYear(), 2, 30, 0, 0, 0 );
	//var closeDate = new Date( new Date().getFullYear(), 10, 2, 0, 0, 0 );

	//$(".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: '', numberOfMonths: 3});
	$("#checkout").datepicker({option:' it', dateFormat: 'dd-mm-yy',showOn: 'both', buttonText: '', 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()) );

    //$('#gallery-slider a').lightBox();

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


});

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

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


});

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

});

function inviaCallBack() {
	if ( (document.getElementById("call_nome").value == "") || (document.getElementById("call_nome").value == "nome") ) {
		alert("Inserire il proprio nome");
		return;
	}
	if ( (document.getElementById("call_telefono").value == "") || (document.getElementById("call_telefono").value == "telefono") ) {
		alert("Inserire il proprio telefono");
		return;
	}
	if ( (document.getElementById("call_orario").value == "") || (document.getElementById("call_orario").value == "orario") ) {
		alert("Inserire l'orario nel quale si desidera essere richiamati");
		return;
	}
	document.getElementById("form_callback").submit();
}

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

function setRoom(nrooms,label_adults,label_children,label_children_age,label_room,myBlockNow){
	suffix="off_";
    if(myBlockNow==undefined){
    	myBlockNow='contOption';
		suffix="";
	}
	var myResults="";
    myResults=myResults+'<ul>\n';
    for(var i=1;i<=nrooms;i++){
    	if(nrooms>1){
        	myResults=myResults+' <li><strong>'+label_room+' '+i+'</strong></li>\n';
		}
		myResults=myResults+'  <li>\n'+'   <label>'+label_adults+'</label>\n'+'   <select name="bform[reqRooms]['+i+'][adults]" class="select_small">\n'+'   '+createSelect(6,1,'',2)+'   </select>\n'+'  </li>\n'+'  <li>\n'+'   <label>'+label_children+'</label>\n'+'   <select name="bform[reqRooms]['+i+'][child]" class="select_small" onchange="setChildAge(this.value,\''+suffix+'chdAge_'+i+'\', '+i+',\''+label_children_age+'\');">\n'+'   '+createSelect(4,0,'',0)+'   </select>\n'+'  </li>\n'+'  <li class="childAge" id="'+suffix+'chdAge_'+i+'"></li>\n'+' </li>\n';
	}
	myResults=myResults+'</ul>\n';
    if(document.getElementById(myBlockNow)){
    	document.getElementById(myBlockNow).innerHTML=myResults;
	} else {
    	alert("blocco non valido");
	}
}

function createSelect(num,init,label,selected){
	if(label!=""){label=" "+label;}
	var mySel="";
	for(var k=init;k<=num;k++){
		if(selected!=0&&selected==k){
			mySel=mySel+"\n"+'<option value="'+k+'" selected="selected">'+k+label+'</option>';
		}else{
			mySel=mySel+"\n"+'<option value="'+k+'">'+k+label+'</option>';
		}
	}
	return mySel;
}

function setChildAge(nchild,chdid,roomn,label_children_age,suffix){
	if(suffix==undefined)suffix="";
    var selectHtml="";
    if(nchild>0){
    	selectHtml='<label>'+label_children_age+'</label>';
        for(var cc=1;cc<=nchild;cc++){
        	selectHtml=selectHtml+'<select name="bform[reqRooms]['+roomn+'][childAge]['+cc+']">'+'<option value="0">&lsaquo; 1</option>';
	        for(var k=1;k<=18;k++){
    	    	k_opt=k<10?"&nbsp;"+k:k;selectHtml=selectHtml+"\n"+'<option value="'+k+'">'+k_opt+'</option>';
			}
		selectHtml=selectHtml+'</select>';
    	}
	}
	if(document.getElementById(suffix+chdid)){
    	document.getElementById(suffix+chdid).innerHTML=selectHtml;
	}
}

function setRoom2_ORG(nrooms,label_adults,label_children,label_children_age,label_room){
	for(cc=2;cc<=10;cc=cc+1) {
		jQuery('.r'+cc).remove();jQuery('#childlabel'+cc).remove();
	}
	var roomsHtml='';
	if(nrooms>1) {
		for(cc=2;cc<=nrooms;cc=cc+1) {
			var roomsHtml=roomsHtml+'<tr class="r'+cc+'" >'+'<td colspan="2"><div class="roomlabel">'+label_room+' '+cc+'</div></td>'+'</tr>'+'<tr class="r'+cc+'" >'+'<td scope="row">'+label_adults+'</td>'+'<td><select name="bform[reqRooms]['+cc+'][adults]" >'+'<option value="1">1</option>'+'<option value="2" selected="selected" >2</option>'+'<option value="3">3</option>'+'<option value="4">4</option>'+'</select>'+'</td>'+' </tr>'+'<tr class="r'+cc+'" >'+'<td scope="row">'+label_children+''+'</td>'+'<td><select name="bform[reqRooms]['+cc+'][child]" onchange="setChildAge2(this.value,'+cc+')" >'+' <option value="0">0</option>'+' <option value="1">1</option>'+' <option value="2">2</option>'+' <option value="3">3</option>'+'</select>'+'</td>'+' </tr>'+'<tr id="childlabel'+cc+'">'+'<td scope="row">'+label_children_age+'</td>'+'<td><div id="childage'+cc+'"></div></td>'+'</tr>';
		}
		jQuery('#lastroom').after(roomsHtml).show()
		for(cx=2;cx<=nrooms;cx=cx+1) {
			jQuery('#childlabel'+cx).css('display','none');
			jQuery('#childage'+cx).html('');
		}
		jQuery('div.roomlabel').css('display','block');
		jQuery('.spazio').css('height','2px');
	} else {
		jQuery('div.roomlabel').css('display','none');
		jQuery('.spazio').css('height','13px');
	}
}

function setRoom2(nrooms,label_adults,label_children,label_children_age,label_room){
	for(cc=2;cc<=10;cc=cc+1) {
		jQuery('.r'+cc).remove();jQuery('#childlabel'+cc).remove();
	}
	var roomsHtml='';
	if(nrooms>1) {
		for(cc=2;cc<=nrooms;cc=cc+1) {
			code = '';
			//code = code + '<span class="r'+cc+'" >'+'<td colspan="2"><div class="roomlabel">'+label_room+' '+cc+'</div></td>'+'</span>';
			jQuery('div.roomlabel2').css('display','block');
			code = code + '<span class="rowABS r'+cc+'" >'+''+label_adults+'<br />'+'<select name="bform[reqRooms]['+cc+'][adults]" >'+'<option value="1">1</option>'+'<option value="2" selected="selected" >2</option>'+'<option value="3">3</option>'+'<option value="4">4</option>'+'</select>'+''+' </span>';
			code = code + '<span class="rowABS r'+cc+'" >'+''+label_children+'<br />'+''+'<select name="bform[reqRooms]['+cc+'][child]" onchange="setChildAge2(this.value,'+cc+')" >'+' <option value="0">0</option>'+' <option value="1">1</option>'+' <option value="2">2</option>'+' <option value="3">3</option>'+'</select>'+''+' </span>';
			code = code +'<span class="rowABS" id="childlabel'+cc+'">'+''+label_children_age+'<br />'+'<div id="childage'+cc+'"></div>'+'</span>'
			var roomsHtml=roomsHtml+code;
		}
		//jQuery('#lastroom').after(roomsHtml).show()
		jQuery('#lastroom').html(roomsHtml).show()
		for(cx=2;cx<=nrooms;cx=cx+1) {
			jQuery('#childlabel'+cx).css('display','none');
			jQuery('#childage'+cx).html('');
		}
		jQuery('div.roomlabel').css('display','block');
		jQuery('.spazio').css('height','2px');
	} else {
		jQuery('div.roomlabel').css('display','none');
		jQuery('.spazio').css('height','13px');
	}
}

function setChildAge2(nchild,roomn) {
	if(nchild>0) {
		var selectHtml='';
		var cc=1;
		for(cc=1;cc<=nchild;cc=cc+1) {
			var selectHtml=selectHtml+'<select name="bform[reqRooms]['+roomn+'][childAge]['+cc+']"  class="childage {validate:{required:true, messages:{required:\'\'}}}" >'+'<option value="0">&lt;1</option>'+'<option value="1">1</option>'+'<option value="2">2</option>'+'<option value="3">3</option>'+'<option value="4">4</option>'+'<option value="5">5</option>'+'<option value="6">6</option>'+'<option value="7">7</option>'+'<option value="8">8</option>'+'<option value="9">9</option>'+'<option value="10">10</option>'+'<option value="11">11</option>'+'<option value="12">12</option>'+'<option value="13">13</option>'+'<option value="14">14</option>'+'<option value="15">15</option>'+'<option value="16">16</option>'+'<option value="17">17</option>'+'<option value="18">18</option>'+'</select>';
		}
		jQuery('#childage'+roomn).html(selectHtml).show();
		jQuery('#childlabel'+roomn).removeAttr('style');
	} else {
		jQuery('#childage'+roomn).html('');
		jQuery('#childlabel'+roomn).css('display','none');
	}
}

