/**
 * 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-container').cycle({
		fx: 'scrollHorz',
		speed: 2000,
		timeout: 6500,
		prev: '#offertaPrev',
		next: '#offertaNext'
	});

}
catch(e) {toDebug(e.toString());}
}

function slideBoxTop()
{
try
{
	$('#images_top').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());}
}

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

	$("#checkin").datepicker({option:' it', dateFormat: 'dd-mm-yy', numberOfMonths: 3});
	$("#checkout").datepicker({option:' it', dateFormat: 'dd-mm-yy',  numberOfMonths: 3});


	//$('a.highslide').lightBox();
    //$('#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);
		}
	);

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

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

		var iName = $("#nsup_nome");
		var iEmail = $("#nsup_email");
		var iPrivacy = $("#nsup_privacy");

		if( iName.val() == "" || iName.val() == "nome*" ) {
			alert("Inserire il proprio nome");
			iName.focus();
			return false;
		}
		else 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;}
	});

});

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


	/*
$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 MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function overtd(id){
 if(document.getElementsByTagName){
   var table = document.getElementById("tabella");
   var celle = table.getElementsByTagName("td");
   celle[id].className = "sopra";
	}
 }
 function outtd(id){
 if(document.getElementsByTagName){
   var table = document.getElementById("tabella");
   var celle = table.getElementsByTagName("td");
   celle[id].className = "fuori";
	}
 }

 function overtd2(id){
 if(document.getElementsByTagName){
   var table = document.getElementById("tabella2");
   var celle = table.getElementsByTagName("td");
   celle[id].className = "sopra";
	}
 }
 function outtd2(id){
 if(document.getElementsByTagName){
   var table = document.getElementById("tabella2");
   var celle = table.getElementsByTagName("td");
   celle[id].className = "fuori";
	}
 }

function controllo() {
	if (document.getElementById("nome").value == "") {
		alert("Inserire il proprio nome");
		document.getElementById("nome").focus()
		return;
	}
	if (document.getElementById("cognome").value == "") {
		alert("Inserire il proprio cognome");
		document.getElementById("cognome").focus()
		return;
	}
	if (document.getElementById("telefono").value == "") {
		alert("Inserire il proprio telefono");
		document.getElementById("telefono").focus()
		return;
	}
	if (document.getElementById("submit_by").value == "") {
		alert("Inserire il proprio indirizzo email");
		document.getElementById("submit_by").focus()
		return;
	}
	if (document.getElementById("submit_by").value == "") {
		alert("Inserire il proprio indirizzo email");
		document.getElementById("submit_by").focus()
		return;
	}
	if (document.getElementById("consenso").checked == false) {
		alert("E' necessario accettare il trattamento dei dati");
		document.getElementById("consenso").focus()
		return;
	}
	dest = "cont";
	document.getElementById("form2").action = dest + "atti" + ".php";
	document.getElementById("form2").submit();
}
