var A_totale_pass;
var A_totale_adulti;
var A_totale_ragazzi;
var A_totale_infanti;
var A_totale_infanti_posto_letto;

var nomi_adulti;
var nomi_ragazzi;
var nomi_infanti;
var nomi_infanti_posto_letto;

// viene passata dallo step 3 al 4 e dal 6 al 7
var XML_GetVoyageServicesGroups;

var XML_dati_caricati;

var path_wizard = '/wizard/jadro/';
var RangeDepartureDate = 0.5;

function request(req_url, GET_data, POST_data) {
	var XMLResponse;

	//mettiamo un random all'url
	req_url += '?rnd=' + Math.random();

	if (typeof POST_data == 'undefined') {

		//tipo di richiesta
		REQ_type = "GET";

		//dati
		REQ_data = GET_data;
	} else {

		//tipo di richiesta
		REQ_type = "POST";

		//url deve contenere i dati GET
		req_url += ('&' + GET_data);

		//dati
		REQ_data = POST_data;

	}

	//’xml’ is an XML document object holding the returned XML
	$.ajax({async: false
			, type: REQ_type
		    , url: req_url
		    , data: REQ_data
		    , dataType: 'xml'
		    , success: function(xml){
		    	XMLResponse = xml; //mal che vada passare $(xml) che dovrebbe essere un oggetto jQuery con gli stessi nodi
		    	}
		    , timeout: 2000
		    , error: function(XMLHttpRequest, textStatus, errorThrown) {
			    //$('#debug').append('<div>errore ajax' + textStatus + errorThrown + '</div>');
			    XMLResponse = false;
		    }
			});
	return XMLResponse;
}


function carica_dati_url (url)
{
	XML_dati_caricati = request(url, '');

	// se risponde KO allora non deve essere caricato niente
	if ( $('DATABASE',XML_dati_caricati).text() == 'KO'  ) XML_dati_caricati = '';
}


function show_step(id)
{
	// evento che serve quando si crea un viaggio precompilato
	$("#salva").click();

	$('#contenitore')
		.fadeOut(250, function () {
			//$('#debug').append('<div>finito fadeout show ' + id + '</div>');
			$('#splash_screen').appendTo('#parcheggio');

			//ATTENZIONE: prima di muovere un elemento che potenzialmente contiene input radio, curo IE che ne cancella il valore
			//mette un attributo "ripristina" a tutti gli input radio checked (per evitare che IE cancelli il valore checked su appendTo o hide)
			$('#'+id + ' input[type=radio]:checked').each(function() {$(this).attr('ripristina','ripristina')});
			$('#'+id + ' input[type=checkbox]:checked').each(function() {$(this).attr('ripristina','ripristina')});

			//muovo l'elemento
			$('#'+id).appendTo('#contenitore');

			//ripristina  il valore degli input type checked che eventualmente IE ha cancellato. usa l'attributo "ripristina", che poi viene tolto.
			$('#'+id + ' input[ripristina]').each(function() {
				$(this).attr('checked',true);
				$(this).removeAttr('ripristina');
			});

		})
		.fadeIn(250); //conteneva  function () {$('#debug').append('<div>finito fadein show ' + id + '</div>');}
	;

	//parte per il riepilogo
	//nascondo tutto
	$('.box_riepilogo').addClass('hidden');

	// classe per rendere cliccabile la barra di avanzamento
	$('.av_active').unbind();
	$('#avanzamento li span').removeClass('av_active');

	//mostro a seconda di. (più alto lo step, più roba viene eseguita.)
	switch(id) {
		case 'step_finale': try{riep_step_7(); $('#av_n8 span')						 ;} catch(e) {alert(e.message);}
		case 'step_7': 		try{riep_step_6(); $('#av_n7 span').addClass('av_active');} catch(e) {alert(e.message);}
		case 'step_6': 		try{riep_step_5(); $('#av_n6 span').addClass('av_active');} catch(e) {alert(e.message);}
		case 'step_5': 		try{riep_step_4(); $('#av_n5 span').addClass('av_active');} catch(e) {alert(e.message);}
		case 'step_4': 		try{riep_step_3(); $('#av_n4 span').addClass('av_active');} catch(e) {alert(e.message);}
		case 'step_3': 		try{riep_step_2(); $('#av_n3 span').addClass('av_active');} catch(e) {alert(e.message);}
		case 'step_2': 		try{riep_step_1(); $('#av_n2 span').addClass('av_active');} catch(e) {alert(e.message);}
		case 'step_1':						   $('#av_n1 span').addClass('av_active');
		default: 	   //noop
	}

	$('#riepilogo').appendTo('#'+id+'_footer_riepilogo');

	// imposto il tooltip per ogni passo
	if ( $('#'+id+'_descr').text() != "" )
	{
		$('#step_help').attr('title', ' - '+$('#'+id+'_descr').text());

		$('#step_help').tooltip({
		    track: true,
		    delay: 0,
		    showURL: false,
		    opacity: 1,
		    fixPNG: true,
		    showBody: " - ",
		    extraClass: "help",
		    top: -15,
		    left: 5
		});
	}
	else
	{
		$('#step_help').unbind();
	}


	// evento per muoversi tra gli step cliccando sulla barra di avanzamento
	$('.av_active').click(function(){

		chi = $(this).text();
		chi = chi.replace(' ','');

		// nasconde lo step attuale; se nel contenitore c'è lo splash_screen vuol dire che l'utente ha cliccato due volte e non va bene
		// non va bene nemmeno se nel contenitore c'è lo stesso step che di dovrebbe caricare
		nascondi = $('#contenitore div').attr('id');
		//alert(nascondi);
		if ( nascondi != 'splash_screen' && nascondi != 'step_'+chi )
		{
			$('#debug_box').append(nascondi+'<br />');

			hide_step(nascondi);

			// nel secondo step bisogna nascondere anche la tooltip
			if (nascondi == 'step_2')
			{
				$('#tooltip_dispo').attr('style','display:none;left:-9999px');
				prec='';
			}

			// mostra lo step selezionato
			show_step('step_'+chi);
		}
	});
}

function hide_step(id, callback) {
	//$('#debug').append('<div>chiamato hide step ' + id + '</div>');
	$('#contenitore')
		.fadeOut(250, function () {
			//$('#debug').append('<div>finito fadeout hide ' + id + '</div>');

			//ATTENZIONE: prima di muovere un elemento che potenzialmente contiene input radio, curo IE che ne cancella il valore
			//mette un attributo "ripristina" a tutti gli input radio checked (per evitare che IE cancelli il valore checked su appendTo o hide)
			$('#'+id + ' input[type=radio]:checked').each(function() {$(this).attr('ripristina','ripristina')});
			$('#'+id + ' input[type=checkbox]:checked').each(function() {$(this).attr('ripristina','ripristina')});

			//muovo l'elemento
			$('#'+id).appendTo('#parcheggio');

			//ripristina  il valore degli input type checked che eventualmente IE ha cancellato. usa l'attributo "ripristina", che poi viene tolto.
			$('#'+id + ' input[ripristina]').each(function() {
				$(this).attr('checked',true);
				$(this).removeAttr('ripristina');
			});


			$('#splash_screen').appendTo('#contenitore');
		})
		.fadeIn(250, function () {
			//$('#debug').append('<div>finito fadein hide ' + id + '</div>');
			if (typeof callback != "undefined") {
				callback();
			}
		});
}

function errore_step(messaggio,step_da_mostrare) {

	$('#splash_error_message').html(messaggio);

	$('#splash_loading').fadeOut(250,function() {
		$('#splash_error').fadeIn(250);
		$('#splash_error_button').click(function(){
			$(this).unbind('click');
			$('#splash_error').fadeOut(250,function() {
				$('#splash_loading').fadeIn(250, function() {show_step(step_da_mostrare);});
			});
		});
	});
}

function tracciaGoogle(step)
{
	var pageTracker = _gat._getTracker("UA-1089123-2");
	pageTracker._setDomainName("amatori.com");
	pageTracker._initData();
	pageTracker._trackPageview('/acquisto/' + step);
}

/*
	Restituisce una variabile in query string
*/
function getQueryVariable(variable)
{
	var query = window.location.search.substring(1);
	var vars = query.split('&');
	for (var i=0;i<vars.length;i++)
	{
		var pair = vars[i].split('=');
		if (pair[0] == variable)
		{
  			return pair[1];
		}
	}
	return false;
}