var $j = jQuery.noConflict();

/*affiche le message d'erreur de connexion*/		
function erreurConnexion(){
			$j('#cadreAllerRetour, #cadrePax, #cadreRecapitulatif, #bloc_chargement, #chargement, #intro').hide();
			$j("#recap").html('&nbsp;').hide();
			$j('#cadreErreur').show();
}

/* affichage des différents cadres */				
function AfficherCadreAllerRetour(){
	$j("#recap").slideUp()
	$j("#cadrePax").hide();
	$j("#cadreRecapitulatif").hide();
	$j("#cadreAllerRetour").show();
	
	var title=$j("#etape1").attr("title");
	$j("#info_tunnel").text(title);
	$j("#etape1").attr({ src: "style/img/etape1_ov.gif"});
	$j("#etape2").attr({ src: "style/img/etape2_ou.gif"});
	$j("#etape3").attr({ src: "style/img/etape3_ou.gif"});
}

/* ----- Gestion des listes déroulantes aéroports ----- */

function Masquer(){
	$j("#msg0").slideUp();
	$j("#cadreDates").slideUp();
}

function ChangerLiaison(x){
	Masquer();
	if(x==1){
		$j("#retour").hide();
		ChargerVille1(1);
		InitialiserVille(2);
	}else{
		$j("#retour").show();
		ChargerVille1(2);
		InitialiserVille(2);
		InitialiserVille(3);
		InitialiserVille(4);
	}
};

function TypeLiaison(){
	var idBouton=($j(".liaison:checked").attr("id"));
	if(idBouton=="L1"){
		return(1);
	}else{
		return(2);
	}
}

function InitialiserVille(numVille){
	var maListe = '#Ville' + numVille;
	$j(maListe).jselect({
		data:[ [ '-', '-', true]],
		replaceAll:true
	});
}

function ChargerVille4(value,text){
	var Ville1=$j("#Ville1").val();
	var Ville2=$j("#Ville2").val();
	$j('#Ville4').jselect({
		replaceAll: true,
		loadUrl: "include/aeroports.php?T=2&Ville1=" + Ville1 + "&Ville2=" + Ville2 + "&Ville3=" + value,
		loadType: "POST",
		loadDataType: "json",
		onChange:function(value, text){
			Masquer();
		},
		loadOnError:function(){
			erreurConnexion();
		}
	});
}

function ChargerVille3(value,text){
	var Ville1=$j("#Ville1").val();
	$j('#Ville3').jselect({
		replaceAll: true,
		loadUrl: "include/aeroports.php?T=2&Ville1=" + Ville1 + "&Ville2=" + value,
		loadType: "POST",
		loadDataType: "json",
		loadOnError:function(){
			erreurConnexion();
		}
	});
}

function ChargerVille2(value, text){
	var x=TypeLiaison();
	$j('#Ville2').jselect({
		replaceAll: true,
		loadUrl: "include/aeroports.php?T=" + x + "&Ville1=" + value,
		loadType: "POST",
		loadDataType: "json",
		loadOnError:function(){
			erreurConnexion();
		}
	});
}

function ChargerVille1(){
	var x=TypeLiaison();
	var Lien ='include/aeroports.php?T='+ x + '&Ville1=-';
	$j('#Ville1').jselect({
		replaceAll: true,
		loadUrl: Lien,
		loadType: "POST",
		loadDataType: "json",
		onComplete:function(){
			$j("#intro").fadeOut('slow', function(){
					$j("#cadre").fadeIn('slow');
			}); 
		},
		loadOnError:function(){
			erreurConnexion();
		}
	});
}


/* ----- Gestion du formulaire ----- */

function Valider(msgErr){
	var valide=true;
	
	if($j("#Ville1").val()=='-' || $j("#Ville2").val()=='-'){
		msgErr[0]='Trajet aller non défini.';
		valide=false;
	}else{
		msgErr[0]='';
	}
	var v=$j('input:radio[name=Liaison]:checked').val();
	if(v==2){
		if($j("#Ville3").val()=='-' || $j("#Ville4").val()=='-'){
			msgErr[1]=' Trajet retour non défini.';
			valide=false;
		}	
	}
	return valide;
}


function Soumettre(){
	msgErr=Array();
	if(Valider(msgErr)){
		$j("#vol0").submit();
	}else{
		var msgText="<img src='style/attention.gif'>";
		for(y=0;y<msgErr.length;y++){
			msgText+=msgErr[y];
		}
		$j("#msg0").slideDown("fast", function(){
			$j(this).html(msgText);
		});
	}
}


/* ----- Traitement de la réponse ----- */

function chargerVol1(){
	$F='vols1.php';
	$j.ajax({
		url: $F,
		cache: false,
		success: function(html){
			$j("#cadreDates").html(html);
			$j("#cadreDates").slideDown();
		},
		error:function(){
			erreurConnexion();
		}
	});
}

function chargerVol2(){
	$j("#msg1").hide();
	$j.ajax({
		url: 'vols2tab.php?idVol1=' + idVol1 + '&idVol2=' + idVol2,
		cache: false,
		success: function(html){
			$j("#cadrePax").html(html);
		},
		error:function(){
			erreurConnexion();
		}
	});
	$j("#cadreDates").hide();
	$j("#cadrePax").show();
}		

function afficherMsg(y){
	switch(y){
		case 0:
			erreurConnexion();
			break;
		case 1:
			var Msg="<img src='style/attention.gif' alt=''>Pas de disponibilités actuellement sur ce vol.";
			break;	
		case 2:
			var Msg="<img src='style/attention.gif' alt=''>Pas de disponibilités actuellement pour le vol retour.";
			break;
		case 3:
			var Msg="<img src='style/attention.gif' alt=''>Pas de disponibilités actuellement pour le vol aller.";
			break;
		case 4:
			var Msg="<img src='style/attention.gif'>Aucune disponibilité actuellement ni sur l'aller, ni sur le retour.";
			break;		
	}
	$j("#msg0").hide("fast", function(){
		$j(this).html(Msg);
		$j(this).show();
	});
}

function Reponse0(data){
	switch(data.Resultat){
		case 0:
		case 1:
		case 2:
		case 3:
		case 4:
			$j("#cadreDates").hide();
			afficherMsg(data.Resultat);
			break;
		case 10: //AS OK
		case 11: //AR OK
			Masquer();
			chargerVol1();
			break;
	}
}

/* ----- Soumission Ajax ---- */		
var optionsVol0={
	dataType:'json',
	success:Reponse0
};

$j(function(){
	$j("#vol0").validate({
		submitHandler: function(form) {
			$j(form).ajaxSubmit(optionsVol0);
			return false;
		}
	});
});

/* ----- Point d'entrée ------ */
function moteur(Precontraint){
	$j("#nbPax").bind("change",function(){
		Masquer();
	});
	$j("#bt_vol0").bind("click",function()
	{
		Soumettre();
	});
	if(! Precontraint){
		$j('#Ville1').jselect({
			onChange:function(value, text){
				Masquer();
				if(value!="-"){
					ChargerVille2(value, text);
				}else{
					InitialiserVille(2);
				}			
				if(TypeLiaison()==2){
					InitialiserVille(3);
					InitialiserVille(4);
				}
			}
		});
		$j('#Ville2').jselect({
			onChange:function(value, text){
				Masquer();
				if(TypeLiaison()==2){
					if(value!="-"){
						ChargerVille3(value, text);			
						InitialiserVille(4);
					}else{
						InitialiserVille(3);
						InitialiserVille(4);
					}
				}
			}
		});
		$j('#Ville3').jselect({
			onChange:function(value, text){
				Masquer();
				if(value!="-"){
					ChargerVille4(value, text);
				}else{
					InitialiserVille(4);
				}

			}
		});
	}
		
	/*****bulle d'aide********/

  // Use the each() method to gain access to each elements attributes
   $j('a[rel]').each(function()
   {
      $j(this).qtip(
      {
         content: {
            text: '<img src="style/img/patience1.gif" alt="chargement...">',
            url: $j(this).attr('rel')
         },
         position: {
            corner: {
               target: 'bottomMiddle',
               tooltip: 'topMiddle'
            },
            adjust: {
               screen: true
            }
         },
         show: { 
            when: 'click', 
            solo: true
         },
         hide: 'unfocus',
         style: {
			border:{
				width: 2,
				color: '#FF8A15'
			},
            tip: true,
            width: 650
         }
      })
   });
}

function moteurStandard(){
	$j("input").val(["2"]);
	$j(".liaison").bind("click",function(){
		ChangerLiaison($j(this).val());
	});
	moteur(false);
	ChargerVille1();
	InitialiserVille(2);
	InitialiserVille(3);
	InitialiserVille(4);
}

function moteurPrecontraint(liaison){
	if(liaison==1){
		$j("input").val(["1"]);
		$j("#retour").hide();
	}else{
		$j("input").val(["2"]);
	}
	$j(".liaison").bind("click",function(){
		Masquer();
		if($j(this).val()==1){
			$j("#retour").hide();
		}else{
			$j("#retour").show();
		}
	});
	moteur(true);
	$j("#intro,#bloc_chargement, #chargement").fadeOut('slow', function(){
		$j("#cadre").fadeIn('slow');
	}); 
}


