/**
 * 
 *//*
function jsToDbDate(dt)
{
	var temp_mese=parseInt(dt.getMonth());
	var mese=temp_mese+1<10?"0"+(temp_mese+1):temp_mese+1;
	var dtstring = dt.getFullYear()
    + '-' + mese
    + '-' + dt.getDate();
	return dtstring;
}
*/
var cal;
function validate()
{
	var result={error:0};
	result.values=[];
	if(!$("#checkbox span").is(":visible"))
	{
		result.error=1;
		result.msg="Accetta i termini sulla privacy per proseguire";
	}
	else
	{
		result.values.push({id:"checkbox",val:true});
	}
	$("#nome,#cognome,#partecipanti,#email").each(function()
	{
		var myVal=$(this).val();
		var myId=$(this).attr("id");
		if (myId=="email" && !result.error)
		{
			var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			if (!filter.test(myVal))
			{
				result.error=1;
				result.msg="Inserisci una e-mail valida per proseguire";
			}
		}
		if(myVal==$(this).attr("default_value")  && !result.error)
		{
			result.error=1;
			switch (myId)
			{
				case "nome":
							result.msg="Inserisci il nome per proseguire";
							break;
				case "cognome":
							result.msg="Inserisci il cognome per proseguire";
							break;
				case "partecipanti":
							result.msg="Scegli il numero dei partecipanti";
							break;
				case "email":
							result.msg="Inserisci l'e-mail per proseguire";
				 			break;
			}
		}
		else
		if (!result.error)
		{
			result.values.push({id:$(this).attr('id'),val:$(this).val()});;
		}
	});
	return result;
}

function setHours(cal,data_prenotazione,capsula)
{
	if (!capsula) capsula="A";
	cal.datepicker( "disable" );
	$.post("ajax.php", {
		dir : "design_camparitivo/prenotazione",
		page : "get_hours",
		data : data_prenotazione,
		capsula: capsula
	},function(data)
	{
		$("td.selected").removeClass("selected");
		$(".prenotazione_color").addClass("green");
		$(".prenotazione_color").removeClass("red");
		
		var gg=	parseInt(data_prenotazione.substring(0,1))*10
				+parseInt(data_prenotazione.substring(1,2));
		
		var mm=	parseInt(data_prenotazione.substring(3,4))*10
				+parseInt(data_prenotazione.substring(4,5))-1;
		
		var aa=parseInt(data_prenotazione.substring(6));
		var selected_day=new Date(aa,mm,gg);
		
		/*********MARTEDI(2) E DOMENICA(0) CHIUDE ALLE 22:30******/
		if (selected_day.getDay()==0 || selected_day.getDay()==2)
		{
			var ultimoTd=$(".prenotazione_color:last").parents("td");
			var penultimoTd=ultimoTd.prev();
			ultimoTd.find(".prenotazione_color").addClass("red");
			penultimoTd.find(".prenotazione_color").addClass("red");
		}
		
		
		$.each(data,function(i,item)
		{
			$("#o"+item+" .prenotazione_color").addClass("red");
			$("#o"+item+" .prenotazione_color").removeClass("green");
		});
		cal.datepicker( "enable" );
	},'json');
}

function initCal(id)
{
	/*loading?*/
	selected=[];
	$.datepicker.setDefaults({
		dayNamesMin : $.datepicker._defaults.dayNamesShort,
		onSelect: function(dateText, inst)
		{
			selected=[];
			day=dateText;
			var capsula= $("#buttons_capsule").find(".selected").attr("id");
			setHours(cal,dateText,capsula);
		}
	});
		cal=$("#" + id).datepicker({
		altField : "#data_selezionata",
		altFormat : "dd/mm/yy",
		minDate : 0
	});
	day=$.datepicker.formatDate('dd/mm/yy', new Date());
	setHours(cal,day);
	return cal;
}

function switchContent(visible, clicked)
{
	var clickedId=clicked.attr('id');
	var visibleId=visible.attr('id');
	if (clickedId != visibleId)
	{
		$('#'+visibleId).hide();
		visible = null;
		$('#'+clickedId).show();
		if (!$('#'+clickedId).hasClass("jspScrollable"))
		{
			$('#'+clickedId).jScrollPane();
		}
	}
}
$(document).ready(function()
{
	$("#introduzione").jScrollPane();
});
$("#button_prenotazione").live("click", function()
{
	return false;
	$.post("ajax.php", {
		dir : "design_camparitivo/prenotazione",
		page : "splash"
	}, function(page)
	{
		$("#design_camparitivo_inner").addClass("prenotazione");
		$("#design_camparitivo_menu").addClass("prenotazione");
		$("#design_camparitivo_inner").html(page);
		$("#design_camparitivo_menu .current").removeClass('current');
		$("#button_prenota").addClass('current');
		$("#button_prenota").show();
		_gaq.push(['_trackEvent','Camparitivo','Click','Prenotazione']);
	});
});

$("#buttons_capsule > div").live("click",function()
{
	var capsula= $(this).attr("id");
	setHours(cal, $.datepicker.formatDate('dd/mm/yy',cal.datepicker("getDate")), capsula);
	$("#buttons_capsule").find(".selected").removeClass("selected");
	$(this).addClass("selected");
});

$("#back_button_prenotazione").live(
		"click",
		function()
		{
			var step = parseInt($("#main_prenotazione").attr("class").match(/\d+/,''));
			switch (step)
			{
				case 1:
					$.post("ajax.php", {
						dir : "design_camparitivo/prenotazione",
						page : "splash"
					}, function(page)
					{
						$("#design_camparitivo_inner").html(page);
					});
					break;
				case 2:
					$.post("ajax.php", {
						dir : "design_camparitivo/prenotazione",
						page : "step1"
					}, function(page)
					{
						$("#design_camparitivo_inner").html(page);
						cal=initCal("body_calendario");
					});
					break;
			}
		});

$("#tabella_orari td:not('.noSelect')").live("click", function()
{
	if (!$(this).find('.red').length)
	{
		$(this).toggleClass("selected");
	}
});

$("#privacy p").live("click", function()
{
	var disclaimer= $("#disclaimer_privacy_prenotazione");
	var text= $('#disclaimer_privacy_prenotazione_testo');
	disclaimer.show();
	if (!text.hasClass("jspScrollable"))
	{
		text.jScrollPane();
	}
});
$("#close_disclaimer").live("click",function()
{
	$("#disclaimer_privacy_prenotazione").hide();
});
$("#toStep3").live("click", function()
{
	var result=validate();
	if(!result.error)
	{
		$.post("ajax.php", {
			dir : "design_camparitivo/prenotazione",
			page : "step3",
			form : result.values,
			day : day,
			hours : selected,
			capsula: $("#capsula_assegnata").html()
		}, function(page)
		{
			$("#body_form_prenotazione").html(page);
		});
	}
	else
	{
		openPP(result.msg);
	}
});
var selected=[];
var day;
$("#toStep2").live("click", function()
{
	var number_hours_selected=$("#tabella_orari .selected").length;
	var prev_element;
	var contiguous=true;
	if (number_hours_selected)
	{
		$("#tabella_orari .selected").each(function(i,item)
		{
			if (i==0)
			{
				prev_element=item;
			}
			if(prev_element!=item)
			{
				var nextPrevElement=$(prev_element).next();
				if (!nextPrevElement.length)
				{
					nextPrevElement=$(prev_element).parent().next().find("td:first");
				}
				if(nextPrevElement.find(".prenotazione_ora_txt").html()!=$(item).find(".prenotazione_ora_txt").html())
				{
					contiguous=false;
				}
			}
			if(contiguous)
			{
				var tmp=$(item).find('.prenotazione_ora_txt').html();
				tmp=tmp.replace(/\s/gi,'');
				selected.push(tmp);
			}
			var ora=$(item).find(".prenotazione_ora_txt").html();
			prev_element=item;
		});
		if (!contiguous)
		{
			selected=[];
			openPP("ATTENZIONE! Le ore scelte devono essere contigue.");
		}
		else
		{
			$.post("ajax.php", {
				dir : "design_camparitivo/prenotazione",
				page : "step2",
				day : day,
				hours : selected,
				capsula: $("#buttons_capsule").find(".selected").attr("id")
			}, function(page)
			{
				$("#design_camparitivo_inner").html(page);
			});
		}
	}
	else
	{
		openPP("ATTENZIONE! Per poter proseguire devi selezionare l'ora.");
	}
});

$("#toStep1").live("click", function()
{
	$.post("ajax.php", {
		dir : "design_camparitivo/prenotazione",
		page : "step1"
	}, function(page)
	{
		$("#design_camparitivo_inner").html(page);
		cal=initCal("body_calendario");
	});
});

$("#nome,#cognome,#email").live("focusin", function()
{
	if ($(this).val() == $(this).attr("default_value"))
	{
		$(this).val("");
	}
});
$("#nome,#cognome,#email").live("focusout", function()
{
	if ($(this).val() == "")
	{
		$(this).val($(this).attr("default_value"));
	}
});

$("#partecipanti").live("change",function()
{
	$("#span_partecipanti").html($(this).val());
});

$("#checkbox").live("click",function()
{
	var span=$(this).find("span");
	span.toggle();
});

$("#design_camparitivo_menu li a").live("click",function()
{	
	var me = $(this);
	var tmpId=me.attr('href');
	tmpId=tmpId.substring(tmpId.indexOf('#'));
	if (tmpId!='#prenota')
	{
		me.parents('ul').find('.current').removeClass('current');
		me.parent().addClass('current');
		var clicked = $(me.attr('href'));
		var visible = clicked.parent().find("> div:visible");
		if (visible.length)
		{
			switchContent(visible, clicked);
		}else
		{
			$.post("ajax.php", {
				dir : 'design_camparitivo',
				page : 'inner_left_right'
			}, function(page)
			{
				$("#design_camparitivo_inner").removeClass("prenotazione");
				$("#design_camparitivo_menu").removeClass("prenotazione");
				$("#design_camparitivo_inner").html(page);
				$("#button_prenota").hide();
				visible = $("#design_camparitivo_right div:first");
				var tmpId=me.attr('href');
				clicked = $(tmpId.substring(tmpId.indexOf('#')));
				if (clicked.attr('id')==visible.attr('id'))
				{
					visible=$('<div id="dummy"/>');
				}
				_gaq.push(['_trackEvent','Camparitivo','Click',tmpId.substring(1)]);
				switchContent(visible, clicked);
			});
		}
	}
	return false;
});
