
function showsubmenu(id){
	submenu=document.getElementById('s'+id);
	for(i=1;i<=4;i++){
		if(i==id){
			submenu.style.display="block";
		} else{
			document.getElementById('s'+i).style.display="none";
		}
	}
}





var oldLink = null;
function setActiveStyleSheet(link, title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
  if (oldLink) oldLink.style.fontWeight = 'normal';
  oldLink = link;
  link.style.fontWeight = 'bold';
  return false;
}

// This function gets called when the end-user clicks on some date.
function selected(cal, date) {
  cal.sel.value = date; // just update the date in the input field.
  if (cal.dateClicked && (cal.sel.id == "sel1" || cal.sel.id == "sel3"))
    cal.callCloseHandler();
}

function closeHandler(cal) {
  cal.hide();                        // hide the calendar
//  cal.destroy();
  _dynarch_popupCalendar = null;
}

function showCalendar(id, format, showsTime, showsOtherMonths) {
  var el = document.getElementById(id);
  if (_dynarch_popupCalendar != null) {
    // we already have some calendar created
    _dynarch_popupCalendar.hide();                 // so we hide it first.
  } else {
    // first-time call, create the calendar.
    var cal = new Calendar(1, null, selected, closeHandler);
    // uncomment the following line to hide the week numbers
    // cal.weekNumbers = false;
    if (typeof showsTime == "string") {
      cal.showsTime = true;
      cal.time24 = (showsTime == "24");
    }
    if (showsOtherMonths) {
      cal.showsOtherMonths = true;
    }
    _dynarch_popupCalendar = cal;                  // remember it in the global var
    cal.setRange(1900, 2070);        // min/max year allowed.
    cal.create();
  }
  _dynarch_popupCalendar.setDateFormat(format);    // set the specified date format
  _dynarch_popupCalendar.parseDate(el.value);      // try to parse the text in field
  _dynarch_popupCalendar.sel = el;                 // inform it what input field we use

  _dynarch_popupCalendar.showAtElement(el.nextSibling, "Br");        // show the calendar

  return false;
}

var MINUTE = 60 * 1000;
var HOUR = 60 * MINUTE;
var DAY = 24 * HOUR;
var WEEK = 7 * DAY;

function isDisabled(date) {
  var today = new Date();
  return (Math.abs(date.getTime() - today.getTime()) / DAY) > 10;
}

function flatSelected(cal, date) {
  var el = document.getElementById("preview");
  el.innerHTML = date;
}

function showFlatCalendar() {
  var parent = document.getElementById("display");

  var cal = new Calendar(0, null, flatSelected);

  cal.weekNumbers = false;

  cal.setDisabledHandler(isDisabled);
  cal.setDateFormat("%A, %B %e");

  cal.create(parent);

  cal.show();
}



/**************************************************************************/
function allClick()
{
try
{
	var clickbox = $('.offerta');
	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: 'scrollLeft',
		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 slideBoxTop()
{
try
{
	$('#images_top').cycle({
		fx: 'fade',
		speed: 1000,
		timeout: 4500
	});
}
catch(e) {toDebug(e.toString());}
}



function cancellaForm()
{
try
{
	$(".short: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 slideBoxTop()
{
try
{
	$('#gallery').cycle({
		fx: 'fade',
		speed: 1000,
		timeout: 3000
	});
}
catch(e) {toDebug(e.toString());}
}



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

// start_Funzioni per versione mobile del sito
function moveAbs(){
	var tmp = jQuery('#preventivo').clone();
	jQuery("#preventivo").remove();
	tmp.prependTo("#cont");
	//tmp.insertAfter("#top-navigation");
}

function copyFooter(){
	var fTel = jQuery('#footer .footerTel').clone();
	var fMail = jQuery('#footer .footerMail').clone();
	jQuery("#footer").remove();
	fTel.prependTo("#container");
	//fMail.prependTo("#container");
}
// end

$j(document).ready(function()
{
	// start_Codice per versione mobile del sito
	if( jQuery.browser.mobile ){
		//alert( "versione mobile" );

		// Rimpiazzio il normale foglio di stile per desktop con quello per dispositivi mobile
		//jQuery("link:first").attr("href","css/mobile.css");
		jQuery("link[href='css/core.css']").attr("href","css/mobile.css");

		moveAbs();
		copyFooter();

		// non visualizzo il numero delle camere nella versione mobile
		jQuery("#numRooms").parent().prev().html("");
		jQuery("#numRooms").css({'display': 'none'});

		jQuery("#checkin, #checkout").live('click', function() {
			jQuery(this).datepicker({option:'it', dateFormat: 'dd-mm-yy', buttonText: '', numberOfMonths: 1,beforeShow: function (textbox, instance) { instance.dpDiv.css({ marginTop: (-textbox.offsetHeight+20) + 'px', marginLeft: (textbox.offsetWidth-20) + 'px' }); }}).focus();
		});
	}
	else
	{
		if( jQuery.browser.msie )
		{
			jQuery("#checkin, #checkout").datepicker({option:'it', minDate: 0, dateFormat: 'dd-mm-yy', showOn: 'both', buttonImage: 'images/calendarietto.png', buttonText: '', buttonImageOnly: true, numberOfMonths: 2,beforeShow: function (textbox, instance) { instance.dpDiv.css({ marginTop: (-textbox.offsetHeight-150) + 'px', marginLeft: (textbox.offsetWidth) + 'px' }); }});
			/*
			jQuery("#checkin, #checkout").live('click', function() {
				jQuery(this).datepicker({option:'it', dateFormat: 'dd-mm-yy', showOn: 'both', buttonImage: 'images/calendarietto.png', buttonText: '', buttonImageOnly: true, numberOfMonths: 2,beforeShow: function (textbox, instance) { instance.dpDiv.css({ marginTop: (-textbox.offsetHeight-150) + 'px', marginLeft: (textbox.offsetWidth) + 'px' }); }}).focus();
			});*/
		}
		else
		{
			jQuery("#checkin, #checkout").datepicker({option:'it', minDate: 0, dateFormat: 'dd-mm-yy', showOn: 'both', buttonImage: 'images/calendarietto.png', buttonText: '', buttonImageOnly: true, numberOfMonths: 2,beforeShow: function (textbox, instance) { instance.dpDiv.css({ marginTop: (-textbox.offsetHeight-150) + 'px', marginLeft: (textbox.offsetWidth) + 'px' }); }});

			/*
			jQuery("#checkin, #checkout").live('click', function() {
				jQuery(this).datepicker({option:'it', dateFormat: 'dd-mm-yy', showOn: 'both', buttonImage: 'images/calendarietto.png', buttonText: '', buttonImageOnly: true, numberOfMonths: 2,beforeShow: function (textbox, instance) { instance.dpDiv.css({ marginTop: (-textbox.offsetHeight-150) + 'px', marginLeft: (textbox.offsetWidth) + 'px' }); }}).focus();
			});*/
		}
	}
	// end

	//allClick();
	slideBox();
	slideBoxTop();
	cancellaForm();
	//slideShow();

	//$("a[rel^='prettyPhoto']").prettyPhoto({theme: 'facebook'});
	//$('*[rel^="lightbox"], *[rel^="prettyPhoto"]').prettyPhoto({theme:'facebook'});

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

    //$('#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);
		}
	);*/

	// controllo su data di arrivo e di partenza
   	date1 = jQuery("#checkin").datepicker( 'getDate' );
   	date1 = new Date(date1);
	date1.setDate(date1.getDate() + 1);

	jQuery("#checkout").datepicker('option', 'minDate',date1);

	jQuery("#checkin").live('change', function() {
		date1 = jQuery("#checkin").datepicker( 'getDate' );
		date2 = new Date(date1);
		date2.setDate(date1.getDate() + 1);
		jQuery("#checkout").datepicker('option', 'minDate',date2);
	});
	// end_Fix per versione mobile del sito

	$('*[rel^="lightbox"], *[rel^="prettyPhoto"], .cont-txt2 a').prettyPhoto({theme:'facebook'});

	// Cartina homepage
	jQuery(".popMap").click( function(){
		mapPopUp();
	});

	jQuery(".dark-panel").click( function(){
		mapPopClose();
	});

	jQuery(".cartina p").bind({

		mouseenter: function(){
			var mapPoint = jQuery(this);
			var pW = mapPoint.width();
			var pH = mapPoint.height();
			var pLeft = parseInt( mapPoint.css('left'));
			var pBottom = parseInt( mapPoint.css('bottom'));

			var mapImage = mapPoint.find("span");
			var mapImageW = mapPoint.find("span").width();
			var mapImageH = mapPoint.find("span").height();
			var coef = 5;

			var xScreen = pLeft + mapImageW;
			var yScreen = pBottom + mapImageH;
			var yPopup = 0;

			if( yScreen > 600 ){ yPopup = (-mapImageH); }else{ yPopup = pH; }

			if( xScreen > 900 ){
				mapImage.css({'position': 'absolute', 'left': -(mapImageW-pW), 'bottom': yPopup});
			}
			else{
				mapImage.css({'position': 'absolute', 'left': 0, 'bottom': yPopup});
			}
			mapImage.fadeIn();

		},
		mouseleave: function(){
			var mapPoint = jQuery(this);
			var pW = mapPoint.width();
			var pH = mapPoint.height();
			var mapImage = mapPoint.find("span");

			mapImage.fadeOut();
		}
	});

	// Banner evento (Fedelity card)
	jQuery("#bannerClose").click( function(){
		jQuery(this).parent("div").fadeOut();
	});
});

function mapPopUp()
{
	var screenW = jQuery(window).width();
	var center = (screenW-900)/2;

	// apro il popup
	jQuery(".dark-panel, .cartina").fadeIn(300);
	jQuery(".cartina").css({'left': center});
	jQuery(".dark-panel").height( jQuery(document).height());
}

function mapPopClose()
{
	// chiudo il popup
	jQuery(".dark-panel, .cartina").fadeOut(300);
}

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

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


});

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

});

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 slideShow() {
	$('#gallery img').css({opacity: 0.0});
	$('#gallery img:first').css({opacity: 1.0});
	$('#gallery .caption').css({opacity: 0.7});
	$('#gallery .caption').css({width: $('#gallery').find('img').css('width')});
	setInterval('gallery()',3000);
}

function gallery() {
	var current = ($('#gallery img.show')?  $('#gallery img.show') : $('#gallery img:first'));
	var next = ((current.next().length) ? ((current.next().hasClass('caption'))? $('#gallery img:first') :current.next()) : $('#gallery gallery:first'));
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
	$('#gallery .caption').animate({opacity: 0.0}, { queue:false, duration:50 }).animate({height: '1px'}, { queue:true, duration:300 });
	$('#gallery .caption').animate({opacity: 0.7},100 ).animate({height: '100px'},500 );
}

/* ABS */

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){
			if (i != 1) {
				myResults=myResults+' <li class="absRoom"><strong>'+label_room+' '+i+'</strong></li>\n';
			} else {
				myResults=myResults+' <li class="absRoom"><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(3,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(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');
	}
}

