loader1 = false;
loader2 = false;

function changerLoader1(){
	$("#please-wait").css({ width : maps.getDirectionProgress() * 100 } );
};

function changerLoader2(){
	$("#please-rewait").css({ width : maps.getDirectionsProgress() * 100 } );
};



afterCompute = "";
forceSubmit = false;
$(window).load(function (){
	
	
	// Grille tarifs
	$("#grille-tarifs").validate();
	$("#grille-tarifs").submit(function (){	
		loader2 = setInterval("changerLoader2()",500);
		if (!$(this).valid()){
			return false;
		}
		GT_city = $("#mail_grid_city").val();
		GT_email = $("#mail_grid_email").val();
		GT_distance = [];
		$("#grille-tarifs").fadeOut('slow',function (){
			$("#grille-tarifs-pending").fadeIn();
		});

		// When the direction will be computed
		directionsComputed = function (){
			dataS = {
				email : GT_email,
				city : GT_city
			};
			$(GT_distance).each(function (i,e){
				dataS["dest_"+i] = e;
			});
			$.ajax({
				url: $("#grille-tarifs").attr('action'),
				type: "POST",
				dataType: "xml",
				data: dataS,
				success: function (e){
					$("#grille-tarifs-pending").fadeOut('slow',function (){
						$("#grille-tarifs-after").fadeIn();
						clearInterval(loader2);
					});
				}
			});
			return false;
		};

		directionComputed = function (i,e){
			GT_distance[i] = e;
		};

		$("#grille-tarifs-after a").click(function (){
			$("#grille-tarifs-after").fadeOut('slow',function (){
				$("#grille-tarifs").fadeIn();
			});
			return false;
		});

		// Get the directions and compute it
		$.ajax({
			url: $("#grille-tarifs").attr('action'),
			type: "POST",
			dataType: "xml",
			data : {
				email: GT_email,
				city: GT_city
			},
			success: function (e){
				maps.computeDirections(e,"simulateur-map",directionComputed,directionsComputed);
			}
		});
		
		return false;
	});
	// Fin grille tarifs
	
	
	
	
	
	
	$("#airport_order_return_sure").click(function (){
		if ($(this).attr('checked'))
		{
			$("#return-sure-container").css({opacity: 0.4});
		} else {
			$("#return-sure-container").css({opacity: 1});
		}
	});
	
	// Simulateur
	$("#simulateur-form").validate();
	$("#simulateur-price-close a").click(function (){
		$("#simulateur-price").hide();
		return false;
	});
	$("#simulateur-result-close a").click(function (){
		$("#simulateur-result").hide();
		return false;
	});
	$("#simulateur-cost").click(function (){
		//alert('cost');
		afterCompute = "cost";
	});
	$("#simulateur-view").click(function (){
		afterCompute = "map";
	});
	$("#simulateur-next").click(function (){
		afterCompute = "submit";
	});

	$("#simulateur-form").submit(function (){
		// Check if form is filled
		if (!$(this).valid()){
			return false;
		}
		// Check city
		if (!$("#autocomplete_airport_order_city").val())
		{
			$(".city_error").show();
			return false;
		} else {
			$(".city_error").hide();
		}
		// Check date
		if (!$("#airport_order_start_at").val())
		{
			$(".start_at_error").show();
			return false;
		} else {
			$(".start_at_error").hide();
		}
		
		// Loading
		loader1 = setInterval("changerLoader1()",500);
		$("#please-wait-wrapper").fadeIn();
		
		// Check if must force the submit
		if (forceSubmit){
			return true;
		}

		$("#please-wait").show();
		// Get parameters
		city = $("#airport_order_city").val();
		dest = 0;
		city_end = 0;
		if ($("#airport_order_end").val() != "undefined" && $("#airport_order_end").val() != null)
		{
			dest = $("#airport_order_end").val();
		}
		if ($("#airport_order_city_end").val() != "undefined" && $("#airport_order_city_end").val() != null)
		{
			city_end = $("#airport_order_city_end").val();
		}
		
		// When the distance will be computed
		distanceComputed = function (d){
			//alert('distance is computed');
			$("#simulateur-result").hide();
			// Send to server
			$("#airport_order_distance").val(d);
			
			if (afterCompute == "submit"){
				forceSubmit = true;
				$("#simulateur-form").submit();
			}
			
			$.ajax({
				url: $("#simulateur-form").attr('action'),
				type: "POST",
				dataType: "xml",
				data: {
					distance : d,
					people: $("#airport_order_people").val(),
					children: $("#airport_order_children").val(),
					is_privative : $("#airport_order_is_privative").attr('checked'),
					is_confortable : $("#airport_order_is_confortable").attr('checked'),
					coupon : $("#airport_order_coupon").val(),
					start_at: $("#airport_order_start_at").val(),
					cityId: city,
					destId: dest,
					cityEndId: city_end,
					mode: $("#airport_order_mode").val()
				},
				success: function (e){
					// Retrieve informations
					globalPrice = 0;					
					okCoupon = $(e).find('simulation').attr('coupon');
					if (okCoupon == 0)
					{
						//alert("Le code promo entré n'est pas valide !");
						$("#airport_order_coupon").addClass('border-red');
						$("#please-wait-wrapper").fadeOut();
						return;
					} else {
						$("#airport_order_coupon").removeClass('border-red');
					}
					$(e).find('detail[name=F]').each(function (i,e){
						globalPrice = $(e).attr('value');
					});
					$(e).find('detail[name=C]').each(function (i,e){
						couponDetails = $(e).attr('value');
					});
					month = 0;
					$(e).find('detail[name=P]').each(function (i,e){
						month = $(e).attr('value');
					});
					
					var percent = 0;
					if (month == 1)
					{
						percent = 2;
					}
					if (month == 2)
					{
						percent = 4;
					}
					if (month == 3)
					{
						percent = 6;
					}
					if (month > 3)
					{
						percent = 8;
					}
					
					if (month != 0 && couponDetails == "Aucun"){
						couponDetails = month+" mois à l'avance ("+percent+"%)";
					}
					$("#simulateur-pricing").text(globalPrice);
					dest = $("#airport_order_end").val();
					dest = $("#airport_order_end").find("option[value="+dest+"]").text();
					message = '<p><strong>Départ</strong> : '+$("#autocomplete_airport_order_city").val();
					message += '<br /><strong>Arrivée</strong> : '+dest;
					message += '<br /><strong>Nb. pers.</strong> : '+$("#airport_order_people").val();
					message += '<br /><strong>Nb. enfants</strong> : '+$("#airport_order_children").val();
					if (couponDetails != "Aucun")
					{
						message += '<br /><strong>Réduction</strong> : '+couponDetails;
					} else {
						couponDetails = 'Aucune réduction (départ &lt; 1 mois)';
						message += '<br /><i>Aucune réduction (départ &lt; 1 mois)</i>';
					}
					message += '</p><p style="padding-top:10px;font-size:15px;"><span class="red"><strong>Prix TOC-TOC</strong> : '+globalPrice+' &euro;<span style="font-size:12px;"></span></span>';
					message += '<br />&nbsp;';
					message += '<br /><input type="image" src="'+$("#simulateur-next").attr('src')+'" onclick="$(\'#simulateur-next\').click(); return false;" /></p>';
					maps.activeMap.openInfoWindow(maps.getLastWaypoint(),message);
					$(".coupon_details_text").html(couponDetails);
					$("#simulateur-details").hide();
					$("#please-wait").hide();
					clearInterval(loader1);
					$("#please-wait-wrapper").fadeOut();
					if (afterCompute == "cost"){
						$("#simulateur-price").show();
						pos = $("#simulateur-cost").offset();
						$("#simulateur-price").css('left',pos.left-20);
						$("#simulateur-price").css('top',pos.top-110);
					} else if (afterCompute == "map") {
						$("#simulateur-result").show();
						pos = $("#simulateur-view").offset();
						$("#simulateur-result").css('left',pos.left-260);
						$("#simulateur-result").css('top',pos.top-396);
					}
				}
			});
			return false;
		};
		
		// Get the direction and compute it
		
		$("#simulateur-result").css('left','-1000px');
		$("#simulateur-result").show();
		$("#simulateur-details").hide();
		$.ajax({
			url: $("#simulateur-form").attr('action'),
			type: "POST",
			dataType: "xml",
			data : {
				cityId: city,
				cityEndId: city_end,
				destId: dest,
				mode: $("#airport_order_mode").val()
			},
			success: function (e){
				//alert(e);
				maps.computeDirection(e,"simulateur-map",distanceComputed);
				//alert(distanceComputed);
			}
		});
		
		return false;
	});
	$("#simulateur-result").hide();
	$("#simulateur-price").hide();
});
