jQuery(function () {
	// Tabs
	var tabsControl = jQuery('#breadcrumb').tabs();		// Deactivate other tabs
	
	//
	jQuery(".sceglipacchetto-form").click(updatePacket);

	// Step 2
	jQuery('#sPartecipanti').change(updatePartInfo); 	
	
	
	jQuery(".room-check").click(updatePrice);
	
	
	// Step 3
	jQuery('#fatturazione').click(updateSpedizioneInfo);
	
	// Control
	jQuery(".go-next-step-1").click(function(){validateStep1();return false;});  
	jQuery(".go-next-step-2").click(function(){validateStep2();return false;}); 
 	jQuery(".go-next-step-3").click(function(){validateStep3();return false;}); 	
	
	updatePacket();	
	updatePartInfo();
	updateSpedizioneInfo(); 
	
	goNextStep();
	
	jQuery("#booking  p.booking-step-btn").css("visibility","visible");
	
	jQuery("#concludi-ts").attr('disabled',false);
	
	
	// History
	// DA IMPLEMENTARE
	/*jQuery('.bread a:eq(0)').history(function() {
	//$('#show-hide').hide();
		console.log("A");
	});
	jQuery('.bread b:eq(1)').history(function() {
	//$('#show-hide').show();
		console.log("B");
	});
	jQuery('.bread a:eq(2)').history(function() {
	 //$('#show-hide').show();
		console.log("C");
	});	
	jQuery.ajaxHistory.initialize();*/
	
	
	jQuery('a.step-img').click(function(){jQuery(this).parent().find('input.sceglipacchetto-form').attr("checked", true);updatePacket();return false;}); 	

	
});		


function goNextStep(){
	if (currentStep == 2){
		if (validateStep1){
			jQuery('#breadcrumb').tabs('option', 'disabled', [2]);	
			//jQuery('#breadcrumb').tabs('select', '#step-2-tab');
			jQuery('#breadcrumb').tabs('option', 'selected', 1);
			//currentStep=2;
			window.scroll(0,300); 
			//console.log('1');
		}		
	}else if (currentStep == 3){
		if (validateStep2){		
	       jQuery('#breadcrumb').tabs('option', 'disabled', []);	
			//jQuery('#breadcrumb').tabs('select', '#step-2-tab');
			jQuery('#breadcrumb').tabs('option', 'selected', 2);
			//currentStep=2;
			window.scroll(0,300); 
			//console.log('2');
		}
		
	}else{			
			jQuery('#breadcrumb').tabs('option', 'disabled', [1,2]);
			jQuery('#breadcrumb').tabs('option', 'selected', 0);
			
			window.scroll(0,300); 
			//console.log('3');
		
	}
	jQuery('#error-message').hide().html();
	
	
	return false;
}


function validateStep3(){
	if (validateStepFinal() && jQuery("#mainPost").valid()) {
			jQuery("#concludi-ts").attr('disabled',true);
			jQuery("#mainPost").submit();
			return false;			
		}else{
			//console.log('non valido');
			window.scroll(0,300);
			if (jQuery(".sceglipacchetto-form:checked").length > 0 && jQuery('.room-check:checked').length > 0 && jQuery('#sPartecipanti').val() != 0){
			trowError('Alcuni campi non sono stati compilati correttamente');
			}else{
			trowError('Indica il numero di persone e la tipologia dell\'alloggiamento.');	
			}
			return false;		
		
		}
	}


function validateStepFinal(){
	
	if (jQuery(".sceglipacchetto-form:checked").length > 0 && jQuery('.room-check:checked').length > 0 && jQuery('#sPartecipanti').val() != 0){
	updatePrice();
	return true;
	}else{
	jQuery('#breadcrumb').tabs('option', 'selected', 1);		
	return false;
	}
	
}
	




function validateStep2(){
	
	if (validateStep1() && jQuery('#sPartecipanti').val() != 0 && jQuery('.room-check:checked').length > 0){
	currentStep=3;
	updatePrice();
	goNextStep();	
	return true;
	}else{
	jQuery('#breadcrumb').tabs('option', 'selected', 1);	
	trowError('Indica il numero di persone e la tipologia dell\'alloggiamento.');	
	return false;
	}
	
	
	
}



function validateStep1(){
	
	//currentVal = jQuery(".sceglipacchetto-form:checked").val();
	
	if (jQuery(".sceglipacchetto-form:checked").length > 0){
	currentStep=2;	
	goNextStep();
	//window.scrollTo(-100,0); 
	return true;
	}else{
	jQuery('#breadcrumb').tabs('option', 'selected', 0);		
	trowError('Devi scegliere un pacchetto per poter continuare');	
	return false;
	}
	
	
	
}

function updatePacket(){	
	jQuery('#riepilogo-pacchetto').html(jQuery(".sceglipacchetto-form:checked").parent().parent().find('h5').html());	
	currentPacket = jQuery(".sceglipacchetto-form:checked").val();
	
	jQuery(".ecom-value").html('--');
	
	
}
	


function  updatePrice(){
	
	jQuery("#tipo-alloggiamento").html(jQuery(".room-check:checked").parent().find("label span").html());
	jQuery(".ecom-value").html('&euro; '+(Math.round(jQuery(".room-check:checked").parent().find(".importo-check").html()*jQuery('#n-persone').html())).toFixed(2));	
}
	
function  updatePartInfo(){	

	var value = jQuery('#sPartecipanti').val(); 
	jQuery('#n-persone').html(value);	
	//jQuery(".room-check").attr("checked", false); 
	
	if (value == 0){	
		jQuery("#people-1").hide();	
		jQuery('#n-persone').html("--");
	}else{				
		for(i=jQuery(".room-check").length; i>0; i--) {
			
		//	importo-check
			
			//console.log(Partecipanti[currentPacket][value]['room-'+i]);
			
			if (currentPacket && value!=0){			
				var importo = Partecipanti[currentPacket][value]['room-'+i];
				if (importo){
					jQuery("#room-"+i+" strong.importo-check").html((Partecipanti[currentPacket][value]['room-'+i]/value).toFixed(2));
					jQuery("#room-"+i).show();
					
					if (soldOut[currentPacket][value]['room-'+i] == true){						
						jQuery("#room-"+i+" input.room-check").attr("disabled", true);		
						jQuery("#room-"+i).addClass('esaurito');
					}else{						
						jQuery("#room-"+i+" input.room-check").attr("disabled", false);	
						jQuery("#room-"+i).removeClass('esaurito');
					}
					
					
					//jQuery("#room-"+i+" input.room-check").attr("checked", true); 
					
				}else{
					jQuery("#room-"+i+" strong.importo-check").html('');
					jQuery("#room-"+i+" input.room-check").attr("checked", false);
					jQuery("#room-"+i).hide();		
				}
			}
			
		//	jQuery("#room-"+i).hide();		
		}
		
	
	if (jQuery("input.room-check:checked").length == 0 && currentPacket && value!=0){
		for(i=jQuery(".room-check").length; i>0; i--) {
			if (value!=0){			
					var importo = Partecipanti[currentPacket][value]['room-'+i];
					if (importo){
							jQuery("#room-"+i+" input.room-check").not(":disabled").attr("checked", true);
				}
			}
		}
	}
		
		
		
		updatePrice();
		jQuery("#people-1").show();		
		jQuery("#others-par").hide();
		if (value == 4){
			jQuery("#partecipante-2,#partecipante-3,#partecipante-4").show().find(".optrequired").each(function(){jQuery(this).addClass('required');});
			jQuery("#others-par").show();
		}else if (value == 2){
			jQuery("#partecipante-3,#partecipante-4").hide().find(".optrequired").each(function(){jQuery(this).removeClass('required');});
			jQuery("#partecipante-2").show().find(".optrequired").each(function(){jQuery(this).addClass('required');});
			jQuery("#others-par").show();
			
		}else if (value == 3){
			jQuery("#partecipante-4").hide().find(".optrequired").each(function(){jQuery(this).removeClass('required');});
			jQuery("#partecipante-2,#partecipante-3").show().find(".optrequired").each(function(){jQuery(this).addClass('required');});	
			jQuery("#others-par").show();
		}else {
			jQuery("#partecipante-2,#partecipante-3,#partecipante-4").hide().find(".optrequired").each(function(){jQuery(this).removeClass('required');});		
		}
		
	}	
}

	
function  updateSpedizioneInfo(){
	var value = jQuery('#fatturazione:checked').val();  
	if (value == 1){
		jQuery('#fatturazione-tab').show().find(".optrequired").each(function(){jQuery(this).addClass('required');});
		}
	else{
		jQuery('#fatturazione-tab').hide().find(".optrequired").each(function(){jQuery(this).removeClass('required');});
		}
	}
	
function trowError(message){
jQuery('#error-message').show().html(message);
}
