Preventivo = {
	// numero caratteri da visualizzare
	vociMenuLen : 22,
		
	cache : new Array(),
	
	init : function(){
		// setto evento su voci menu	
		$("input[type='checkbox']").each(function() {
			$(this).click(function() {
				var result = this.id.match(/check([0-9]*)/i);
				var obj = {
					id   : result[1],
					name : $("#checkLabel" + result[1]).text()
				};
				
				if($(this).attr('checked')) {
					Preventivo.cache.push( obj );
					var puntini = '';
					if(obj.name.length > Preventivo.vociMenuLen) puntini = '...';
					$("#sommario" + $(this).attr("rel")).append('<li id="repeat' + obj.id + '">' + obj.name.substring(0, Preventivo.vociMenuLen) + puntini + '</li>');
					
					// aggiunge a riepilogo
					$("#riepilogoPiatti" + $(this).attr("class")).append('<li id="repeatcheck' + obj.id + '">' + obj.name + '</li>');
					$("#riepilogoTitoloPiatti" + $(this).attr("class")).show();
					
					// counter ++
					var count = parseInt( $("#sommarioCount" + $(this).attr("rel")).text() ) + 1;
					if(count <= 0) count = 0;
					$("#sommarioCount" + $(this).attr("rel")).text(count);
					
					var tot = 0;
					$("div[id^='riepilogoTitoloPiatti']").each(function() {
						if( $(this).attr("style") != 'display: none;' ){
							tot++;
						}
					});
					if(tot > 0){
						$("#riepilogoPortata" + $(this).attr("rel")).show();
					}
					
					// add sfondo grigio al check
					$("#checkLabel" + result[1]).parent().attr("style", "background: #c0c0c0");
				}
				else{
					Preventivo.cache.pop( obj );
					$("#repeat" + obj.id).remove();
					$("#repeatcheck" + obj.id).remove();
					
					// counter --
					var count = parseInt( $("#sommarioCount" + $(this).attr("rel")).text() ) - 1;
					if(count <= 0) count = 0;
					$("#sommarioCount" + $(this).attr("rel")).text(count);
					
					var tot_riepilogo_portata = 0;
					$("ul[rel^='riepilogoPortata" + $(this).attr("class") + "']").find('li').each(function() {
						tot_riepilogo_portata++;
					});
					if(tot_riepilogo_portata == 0){
						$("#riepilogoTitoloPiatti" + $(this).attr("class")).hide();
					}
					
					if(count == 0){
						$("#riepilogoPortata" + $(this).attr("rel")).hide();
					}
					
					// remove sfondo grigio al check
					$("#checkLabel" + result[1]).parent().attr("style", "background: #FFFFFF");
				}
				
				//console.log(Preventivo.cache);
			});
		});
		
		// ritorno alla modifica del menu
		$("#modificaMenu").click(function() {
			Preventivo.resetMenu(null, 1);
			
			$("#riepilogo").slideUp(300);
			$("#richiestaPreventivo").slideUp(300);
		});
		
		// richiesta preventivo
		$("#richiediPreventivo").click(function() {
			Preventivo.resetMenu();
			
			$("#riepilogo").hide();
			$("#richiestaPreventivo").slideDown(300);
		});
		
		// bottone invio preventivo
		$("#preventivoSubmit").click(function() {
			var errors = '';
			$("#preventivoForm").find(".mandatory").each(function() {
				if( !$(this).attr('value') ){
					errors += $(this).attr('rel') + '\n';
				}
				if( $(this).attr('type') == 'checkbox' && !$(this).attr('checked') ){
					errors += $(this).attr('rel') + '\n';
				}
			});
			if(errors != "") alert(errors);
			else{
				var data = '';
				$("#preventivoForm").find("input").each(function() {
					data += '&' + $(this).attr('name') + '=' + $(this).attr('value');
				});
				
				$("#preventivoForm").find("textarea").each(function() {
					data += '&' + $(this).attr('name') + '=' + $(this).attr('value');
				});
				
				$("#riepilogo_center_down").hide();
				
				if(Preventivo.cache.length > 0){
					data += '&menu=' + escape( $("#riepilogo").html().replace(/à/,'&agrave;').replace(/è/, '&egrave;').replace(/ì/,'&igrave;').replace(/ò/,'&ograve;').replace(/ù/,'&ugrave;').replace(/é/,'&eacute;').replace(/ê/,'&ecirc;') );
				}
				
				Preventivo.submitForm(data);
			}
		});
		
		// configuro eventi menu dx
		var tot1 = 0;
		$("li[id^='titoloMenu']").each(function() {
			$(this).click(function() {
				if( $(this).attr('class') == 'attiva' ) $(this).attr('class', 'attiva_up');
				else $(this).attr('class', 'attiva');
				
				var result = this.id.match(/titoloMenu([0-9]*)/i);
				$('#preventivoImage').attr("src", $('#titoloMenuImage' + result[1]).attr("src") );
				
				Preventivo.resetMenu( $(this).children(":first").attr("rel") );
			});
			
			if(tot1 == 0){
				$(this).attr('class', 'attiva_up');
			}
			tot1++;
		});
		
		// nascondo altri menu
		var tot2 = 0;
		$("div[id^='testo_piatti']").each(function() {
			if(tot2 == 0){
				var result = this.id.match(/testo_piatti([0-9]*)/i);
				
				// configuro immagine di default
				$('#preventivoImage').attr("src", $('#titoloMenuImage' + result[1]).attr("src") );
			}
			else{
				$(this).hide();
			}
			tot2++;
		});
		
		$('#inviaPreventivoButton').click(function() {
			Preventivo.resetMenu();
			
			$("#richiestaPreventivo").hide();
			
			$("#riepilogo").show();
		});
	},
	
	resetMenu : function(menu, index){
		var tot = 1;
		$("div[id^='testo_piatti']").each(function() {
			var result = this.id.match(/testo_piatti([0-9]*)/i);

			if(menu && 'testo_piatti' + menu  == this.id || (index && index == tot) ){
				$(this).slideDown(300);
				$("#titoloMenu" + result[1]).attr('class', 'attiva_up');
				
				$("#sommarioBox" + result[1]).slideDown(300);
			}
			else{
				$(this).slideUp(300);
				$("#titoloMenu" + result[1]).attr('class', 'attiva');
				
				$("#sommarioBox" + result[1]).slideUp(300);
			}
			
			tot++;
		});
	},
	
	submitForm : function(data){
		$.ajax({
			type: "POST",
			url: 'amos2/modules/MODULE4/preventivo.php?act=invio_preventivo',
			data: data,
			success: function(risposta){
				if(risposta == 1){
					alert('Preventivo inviato correttamente!');
					window.location.reload();
				}
				else{
					alert(risposta);
				}
			}
		});
	}
};
