$(document).ready(function() {
		
	/* -------------------------------------------------------------- 
	   DEVIS
	-------------------------------------------------------------- */
	var id = $('#devis');
	if (id.length == 1) {
		
		loading = function(display) {
			if (display) {
				var devisWidth = $("#devis").width();
				var devisHeight = $("#devis").height();
				//alert(devisWidth+" x "+devisHeight);
				$('#devis-loading').css({opacity: 0.75, width: devisWidth, height: devisHeight}).show();
			} else {
				$('#devis-loading').hide();
			}
		};
		//Permet de déselectionner un input de type radio
		var optionsGroup = $('input.option-groupe:radio', id);
		optionsGroup.livequery(function() {
			if ($(this).is(':checked')) $(this).data('isChecked', 1);
			else $(this).removeData('isChecked');
		});
		optionsGroup.livequery('click', function() {
			var optionsGroup = $('input.option-groupe:radio', id);
			var index = optionsGroup.index(this);
			if ($(this).data('isChecked') == 1) {
				$(this).removeData('isChecked').removeAttr('checked');
			} else {
				$(this).data('isChecked', 1);
			}
			optionsGroup.not(':eq('+index+')').each(function(i) {
				if ($(this).data('isChecked') == 1 && !$(this).is(':checked')) $(this).removeData('isChecked');
			});
		});
		
		/* -------------------------------------------------------------- 
		   ETAPE CURISTE NB
		-------------------------------------------------------------- */
		$('input#devis_avec_hebergement, input#devis_sans_hebergement', id).livequery('click', function() {
			$('select#devis_curiste_nb').change();
		});
		
		$('select#devis_curiste_nb', id).livequery('change', function() {
			var nbCuriste = $(this).val();
			var nbCuristeMax = $('input#devis_max_pers').val();
			var avecHebergement = $('input#devis_avec_hebergement').attr('checked');
			var nbNonCuriste = avecHebergement ? nbCuristeMax - parseInt(nbCuriste) : 0;
			var devis_noncuriste_nb = $('select#devis_noncuriste_nb');
			var valSelected = $('option:selected', devis_noncuriste_nb).val();
			$('option', devis_noncuriste_nb).remove();
			
			for (var i = 0; i<=nbNonCuriste; i++) {
				var selected = valSelected == i ? ' selected="selected"' : '';
				devis_noncuriste_nb.append('<option value="'+i+'"'+selected+'>'+i+'</option>');
			};
		}).change();
		
		$('select#hebergement-liste', id).livequery('change', function() {
			var val = $(this).val();
			loading(true);
			$.ajax({
				type: "POST",
				url: "/devis-verifications.php",
				data: {hebergementId:val, action: 'getOptionsHebergement'},
				success: function(response) {
					loading(false);
					var variables = parseResponse(response);
					if (variables.erreur == 0) {
						var etape = $('#etape-3 .hebergement-options-ajax').html(variables.html);
						//initPension(etape);
						$('#devis #erreur-ajax').empty();
					} else {
						$('#devis #erreur-ajax').html(createErrorMessage(variables.html)).show();
						self.location.href = "#erreur-ancre";
					}
				}
			});
			return false;
		});
		
		$('select.thalasso-liste', id).livequery('change', function() {
			loading(true);
			//On doit afficher les options de ce type de prestation !
			
			$('#focus-ie', id).focus();
			
			var val = $(this).val();
			var curisteDomId = $(this).parents('.curiste').attr('id');
			var curisteDomIdSplit = curisteDomId.split('-');
			curisteVal = curisteDomIdSplit[1];
			
			if (curisteVal == 1) {
				$.ajax({
					type: "POST",
					url: "/devis-verifications.php",
					data: {thalassoId:val, action: 'getOtherCuristes'},
					success: function(response) {
						loading(false);
						var variables = parseResponse(response);
						if (variables.erreur == 0) {
							$('#etape-2 #other-curistes-ajax').html(variables.html);
							$('#devis #erreur-ajax').empty();
						} else {
							$('#devis #erreur-ajax').html(createErrorMessage(variables.html)).show();
							self.location.href = "#erreur-ancre";
						}
					}
				});
			}
			$.ajax({
				type: "POST",
				url: "/devis-verifications.php",
				data: {thalassoId:val, curisteNum:curisteVal, action: 'getOptionsThalasso'},
				success: function(response) {
					loading(false);
					var variables = parseResponse(response);
					if (variables.erreur == 0) {
						$('#etape-2 #'+curisteDomId+' .thalasso-options-ajax').html(variables.html);
						$('#devis #erreur-ajax').empty();
					} else {
						$('#devis #erreur-ajax').html(createErrorMessage(variables.html)).show();
						self.location.href = "#erreur-ancre";
					}
				}
			});
			return false;
		});
		
		parseResponse = function(data) {
			var variables = {erreur:1, html:'Erreur inconnue'};
			
			if (data.substring(0, 14) === 'erreur=0&html=') {
				variables.erreur = 0;
				variables.html = data.substring(14);
			} else if (data.substring(0, 14) === 'erreur=1&html=') {
				variables.erreur = 1;
				variables.html = data.substring(14);
			}
			
			return variables;
		};
		
		createErrorMessage = function(msg) {
			var html;
			html = '<div class="alert">';
				html += '<div class="erreur">';
					html += '<div class="ico png"></div>';
					html += '<div class="wrap">'+msg+'</div>';
				html += '</div>';
			html += '</div>';
			
			return html;
		};
		
		$('#etape-1 a.bt-valider-1', id).livequery('click', function() {
			loading(true);
			var form = $(this).parents('form');
			var curisteNbVal = $('select#devis_curiste_nb', form).val();
			var nonCuristeNbVal = $('select#devis_noncuriste_nb', form).val();
			var avecHebergementVal = $('input#devis_avec_hebergement', form).attr('checked') ? 1 : 0;
			$.ajax({
				type: "POST",
				url: "/devis-verifications.php",
				data: {curisteNb:curisteNbVal, nonCuristeNb:nonCuristeNbVal, avecHebergement:avecHebergementVal, etape: 1},
				success: function(response) {
					loading(false);
					var variables = parseResponse(response);
					if (variables.erreur == 0) {
						$('#etape-1').html(variables.html);
						$('#devis #erreur-ajax').empty();
					} else {
						$('#devis #erreur-ajax').html(createErrorMessage(variables.html)).show();
						self.location.href = "#erreur-ancre";
					}
				}
			});
			return false;
		});
		
		$('#etape-2 a.bt-valider-2', id).livequery('click', function() {
			loading(true);
			var formValues = $(this).parents('form').serialize();
			formValues = formValues+'&etape=2'; 
			$.ajax({
				type: "POST",
				url: "/devis-verifications.php",
				data: formValues,
				success: function(response) {
					loading(false);
					var variables = parseResponse(response);
					if (variables.erreur == 0) {
						var etape = $('#etape-2').html(variables.html);
						initDatePicker(etape);
						$('#devis #erreur-ajax').empty();
					} else {
						$('#devis #erreur-ajax').html(createErrorMessage(variables.html)).show();
						self.location.href = "#erreur-ancre";
					}
				}
			});
			return false;
		});
		
		function datepickerSelect(dateText, inst) {
			if (inst.id == 'date-debut') {
				loading(true);
				$.ajax({
					type: "POST",
					url: "/devis-verifications.php",
					data: {dateDebut:dateText, action: 'setDates'},
					success: function(response) {
						loading(false);
						var variables = parseResponse(response);
						if (variables.erreur == 0) {
							var etape3 = $('#dates-ajax').html(variables.html);
							
							$('#devis #erreur-ajax').empty();
						} else {
							$('#dates-ajax').empty();
							$('#devis #erreur-ajax').html(createErrorMessage(variables.html)).show();
							self.location.href = "#erreur-ancre";
						}
					}
				});
				
			}
			return false;
		}
		
		function datepickerDatesContraintes(date) {
			var institutId = $('input[name="devis[institut_id]"]').val();
			
			/* --- //DEBUT - FIFOU A MODIFIER SI NECESSAIRE --- */
			//Exception Ouistreham : maxDate au 01 nov. 2010
			/*if (institutId == 2) {
				if (date >= new Date(2010, 10, 1)) return [false, ''];
			}*/
			/* --- //FIN - FIFOU A MODIFIER SI NECESSAIRE --- */
			
			//Exception Antibes : fermeture du 28/11/2010 au 12/12/2010
			/*else if(institutId == 5) {
				if (date >= new Date(2010, 10, 28) && date <= new Date(2010, 11, 12)) return [false, ''];
			}*/
			
			return [true, ''];
		}
		
		initDatePicker = function(dom) {
			//var institutId = $('input[name="devis[institut_id]"]').val();
			var _minDate = '+2d';
			
			/* --- //DEBUT - FIFOU A MODIFIER --- */
			var _maxDate = new Date(2012, 11, 31); //var _maxDate = new Date(2011, 11, 31);
			/* --- //DEBUT - FIFOU A MODIFIER --- */
			
			dom.find('#date-debut').datepicker({
				beforeShowDay:datepickerDatesContraintes,
				onSelect:datepickerSelect,
				minDate:_minDate,
				maxDate:_maxDate}).attr('readonly', 'readonly');
		};
		
		initFlaAvantages = function() {
			var params = {wmode:"transparent", menu:"false", quality:'best'};
			var attributes = {};
			var flashvars = {};
			swfobject.embedSWF("/inc/swf/4-avantages.swf", "avantages-swf", "480", "50", "8.0.0", false, flashvars, params, attributes);	
		};
		
		$('#etape-4 a.bt-valider-4', id).livequery('click', function() {
			loading(true);
			var formValues = $(this).parents('form').serialize();
			formValues = formValues+'&etape=4';
			$.ajax({
				type: "POST",
				url: "/devis-verifications.php",
				data: formValues,
				success: function(response) {
					loading(false);
					var variables = parseResponse(response);
					if (variables.erreur == 0) {
						var etape4 = $('#etape-4').html(variables.html);
						$('#devis #erreur-ajax').empty();
						initFlaAvantages();
					} else {
						$('#devis #erreur-ajax').html(createErrorMessage(variables.html)).show();
						self.location.href = "#erreur-ancre";
					}
				}
			});
			return false;
		});
		
		$('#etape-3 a.bt-valider-3', id).livequery('click', function() {
			loading(true);
			var formValues = $(this).parents('form').serialize();
			formValues = formValues+'&etape=3';
			$.ajax({
				type: "POST",
				url: "/devis-verifications.php",
				data: formValues,
				success: function(response) {
					loading(false);
					var variables = parseResponse(response);
					if (variables.erreur == 0) {
						var etape3 = $('#etape-3').html(variables.html);
						initDatePicker(etape3);
						$('#devis #erreur-ajax').empty();
					} else {
						$('#devis #erreur-ajax').html(createErrorMessage(variables.html)).show();
						self.location.href = "#erreur-ancre";
					}
				}
			});
			return false;
		});
		/*
		$('#etape-2 a.bt-prec-1', id).livequery('click', function() {

			$.ajax({
				type: "POST",
				url: "/devis-verifications.php",
				data: {etape: 0},
				success: function(response) {
					var variables = parseResponse(response);
					if (variables.erreur == 0) {
						var etape = $('#etape-1').html(variables.html);
						etape.find('select#devis_curiste_nb').change();
						alert(etape.find('select#devis_curiste_nb').length);
						$('#devis #erreur-ajax').empty();
					} else {
						$('#devis #erreur-ajax').html(createErrorMessage(variables.html)).show();
						self.location.href = "#erreur-ancre";
					}
				}
			});
			return false;
		});
		*/
		$('#etape-2 a.bt-prec-2', id).livequery('click', function() {
			loading(true);
			$.ajax({
				type: "POST",
				url: "/devis-verifications.php",
				data: {etape: 1},
				success: function(response) {
					loading(false);
					var variables = parseResponse(response);
					if (variables.erreur == 0) {
						$('#etape-1').html(variables.html);
						$('#devis #erreur-ajax').empty();
					} else {
						$('#devis #erreur-ajax').html(createErrorMessage(variables.html)).show();
						self.location.href = "#erreur-ancre";
					}
				}
			});
			return false;
		});
		
		$('#etape-4 a.bt-prec-3', id).livequery('click', function() {
			loading(true);
			$.ajax({
				type: "POST",
				url: "/devis-verifications.php",
				data: {etape: 2},
				success: function(response) {
					loading(false);
					var variables = parseResponse(response);
					if (variables.erreur == 0) {
						var etape = $('#etape-2').html(variables.html);
						initDatePicker(etape);
						$('#devis #erreur-ajax').empty();
					} else {
						$('#devis #erreur-ajax').html(createErrorMessage(variables.html)).show();
						self.location.href = "#erreur-ancre";
					}
				}
			});
			return false;
		});
		
		$('#etape-5 a.bt-prec-4', id).livequery('click', function() {
			loading(true);
			$.ajax({
				type: "POST",
				url: "/devis-verifications.php",
				data: {etape: 3},
				success: function(response) {
					loading(false);
					var variables = parseResponse(response);
					if (variables.erreur == 0) {
						var etape = $('#etape-3').html(variables.html);
						initDatePicker(etape);
						$('#devis #erreur-ajax').empty();
					} else {
						$('#devis #erreur-ajax').html(createErrorMessage(variables.html)).show();
						self.location.href = "#erreur-ancre";
					}
				}
			});
			return false;
		});
	}
});
