/*
	$Id$
*/

var survolActif = false;
var zoneActive = false;
var layerActif = null;
var layerPositionX = null;
var layerPositionY = null;
var layerTailleX = null;
var layerTailleY = null;
var tabZones        = Array();
var tabPays         = Array();
var tabDestinations = Array();

function Zone(zoneCode, zoneLib)
{
	this.zoneCode = zoneCode;
	this.zoneLib  = zoneLib;
}

function Pays(paysId, paysLib, zoneCode)
{
	this.paysId   = paysId;
	this.paysLib  = paysLib;
	this.zoneCode = zoneCode;
}

function survolZone(zone)
{
	if (document.getElementById(zone) != null && !zoneActive)
	{
		document.getElementById(zone).style.visibility = "visible";
	}
}

function sortieZone(zone)
{
	if (document.getElementById(zone) != null && !zoneActive)
	{
		document.getElementById(zone).style.visibility = "hidden";
	}
}

function survolLayer(zone, positionX, positionY, tailleX, tailleY)
{
	zoneActive = true;
	layerActif = zone;
	layerPositionX = positionX;
	layerPositionY = positionY;
	layerTailleX = tailleX;
	layerTailleY = tailleY;
	document.getElementById(layerActif).style.visibility = "visible";
}

function sortieLayer(e)
{
	x = (navigator.appName.substring(0,3) == "Net") ? e.pageX : event.x+document.body.scrollLeft;
	y = (navigator.appName.substring(0,3) == "Net") ? e.pageY : event.y+document.body.scrollTop;
	if (window.innerWidth != null)
	{
		x = x - ((window.innerWidth - 1000)/2);
		layerTailleX = layerTailleX - 14;
	}
	if ((x<=layerPositionX) || (x >= (layerPositionX + layerTailleX)) || (y<=layerPositionY) || (y >= (layerPositionY + layerTailleY)))
	{
		zoneActive = false;
		document.getElementById(layerActif).style.visibility="hidden";
	}
}

function changeZone()
{
	if (document.getElementById('select_zone')!=null)
	{
		// Purge des pays
		while (document.getElementById('select_pays').options.length > 1)
		{
			document.getElementById('select_pays').options[1] = null;
		}
		// Purge des destinations
		while (document.getElementById('select_dest').options.length > 1)
		{
			document.getElementById('select_dest').options[1] = null;
		}
		var selectedIndex = document.getElementById('select_zone').selectedIndex;
		if (selectedIndex == 0)
		{
			// Ajout de tous les pays dans l'ordre alphabétique
			var tabPays2 = Array();
			for (var p=0; p<tabPays.length; p++)
			{
				tabPays2[p] = tabPays[p];
			}
			tabPays2.sort(surTriPays1);
			for (var p=0; p<tabPays2.length; p++)
			{
				document.getElementById('select_pays').options[document.getElementById('select_pays').options.length] = 
					new Option(tabPays2[p].paysLib, tabPays2[p].paysId);
			}
			// Les pays et destinations restent vides
		}
		else
		{
			var zoneCode = document.getElementById('select_zone').options[selectedIndex].value;
			// Ajout de tous les pays de la zone dans l'ordre alphabétique
			var tabPays2 = Array();
			var p2 = 0;
			for (var p=0; p<tabPays.length; p++)
			{
				if (tabPays[p].zoneCode==zoneCode)
					tabPays2[p2++] = tabPays[p];
			}
			tabPays2.sort(surTriPays1);
			for (var p=0; p<tabPays2.length; p++)
			{
				document.getElementById('select_pays').options[document.getElementById('select_pays').options.length] = 
					new Option(tabPays2[p].paysLib, tabPays2[p].paysId);
			}
			// Les destinations restent vides
		}
	}
}

function changePays()
{
	if (document.getElementById('select_pays') != null)
	{
		var selectedIndex = document.getElementById('select_pays').selectedIndex;
		if (selectedIndex == 0)
		{
			changeZone();
		}
		else
		{
			var paysId = document.getElementById('select_pays').options[selectedIndex].value;
			// Purge des destinations
			while (document.getElementById('select_dest').options.length > 1)
			{
				document.getElementById('select_dest').options[1] = null;
			}
			// Ajout des destinations du pays dans l'ordre alphabétique
			for (var d=0; d<tabDestinations.length; d++)
			{
				if (tabDestinations[d].paysId==paysId)
					document.getElementById('select_dest').options[document.getElementById('select_dest').options.length] = 
						new Option(tabDestinations[d].destLib, tabDestinations[d].destId);
			}
		}
	}
}

function surClicGoCarte(zoneCode)
{
	formSetCombo(document.getElementById('select_zone'), zoneCode);
	changeZone();
	afficherDestinations();
	return false;
}

function surClicGoListe()
{
	afficherDestinations();
	return false;
}

// objet fonctionnel Destination pour le tableau
function Destination(destId, destEstVol, destEstVoyage, destEstWeekEnd, destLib, paysId, paysLib, zoneCode, zoneLib, destValiditeLib, destUrl)
{
	// attributs
	this.destId          = destId;
	this.destEstVol      = destEstVol;
	this.destEstVoyage   = destEstVoyage;
	this.destEstWeekEnd  = destEstWeekEnd;
	this.destLib         = destLib;
	this.paysId          = paysId;
	this.paysLib         = paysLib;
	this.zoneCode        = zoneCode;
	this.zoneLib         = zoneLib;
	this.destValiditeLib = destValiditeLib;
	this.destUrl         = destUrl;
	// méthodes
	this.genererLigne    = DestinationGenererLigne;
	this.estVisible      = DestinationEstVisible;
}

var lignePaire = false;

function DestinationGenererLigne()
{
	var ligne = document.createElement("TR");
    
    if ( lignePaire == true )
    {
        ligne.className = "lignePaire";
    }
    lignePaire = !lignePaire;
    
	// Pays
	var cellulePays = document.createElement("TD");
	cellulePays.align = "center";
	cellulePays.className = "";
	cellulePays.appendChild(document.createTextNode(this.paysLib));
	ligne.appendChild(cellulePays);

	// Destination
	var celluleDest = document.createElement("TD");
	celluleDest.align = "center";
	celluleDest.className = "";
	celluleDest.appendChild(document.createTextNode(this.destLib));
	ligne.appendChild(celluleDest);

	// Icônes
//	var celluleIcones = document.createElement("TD");
//	celluleIcones.align = "center";
//	celluleIcones.className = "arial11bleu";
//	if (this.destEstVol)
//	{
//		var imageVol = document.createElement("IMG");
//		imageVol.src = "../../projet-ressources/images/picto_avion.gif";
//		imageVol.alt = "Vol";
//		celluleIcones.appendChild(imageVol);
//	}
//	if (this.destEstVoyage)
//	{
//		var imageVoyage = document.createElement("IMG");
//		imageVoyage.src = "../../projet-ressources/images/picto_bagage.gif";
//		imageVoyage.alt = "Voyage";
//		celluleIcones.appendChild(imageVoyage);
//	}
//	if (this.destEstWeekEnd)
//	{
//		var imageWeekEnd = document.createElement("IMG");
//		imageWeekEnd.src = "../../projet-ressources/images/picto_week.gif";
//		imageWeekEnd.alt = "Week-end";
//		celluleIcones.appendChild(imageWeekEnd);
//	}
//	ligne.appendChild(celluleIcones);

	// Validité
//	var celluleValidite = document.createElement("TD");
//	celluleValidite.align = "center";
//	celluleValidite.className = "arial11bleu";
//	if (this.destValiditeLib!=null)
//	{
//		celluleValidite.appendChild(document.createTextNode(this.destValiditeLib));
//	}
//	ligne.appendChild(celluleValidite);

	// Lien vers fiche Destination
	var celluleFicheDest = document.createElement("TD");
	celluleFicheDest.align = "center";
	celluleFicheDest.className = "derniereColonne";
	var lienFicheDest = document.createElement("A");
	lienFicheDest.Destination = this;
	lienFicheDest.href = "#";
	lienFicheDest.onclick = surClicDestinationFiche;
	var imageFicheDest = document.createElement("IMG");
	imageFicheDest.src = "../../projet-ressources/images/icone_choixDestinations.gif";
	imageFicheDest.alt = "Fiche destination " + this.destLib;
	lienFicheDest.appendChild(imageFicheDest);
	celluleFicheDest.appendChild(lienFicheDest);
	ligne.appendChild(celluleFicheDest);

	// Lien vers fiche Pays
//	var celluleFichePays = document.createElement("TD");
//	celluleFichePays.align = "center";
//	celluleFichePays.className = "arial11bleu";
//	var lienFichePays = document.createElement("A");
//	lienFichePays.Pays = this;
//	lienFichePays.href = "#";
//	lienFichePays.onclick = surClicPaysFiche;
//	var imageFichePays = document.createElement("IMG");
//	imageFichePays.src = "../../projet-ressources/images/picto_pays.gif";
//	imageFichePays.alt = "Fiche pays " + this.paysLib;
//	lienFichePays.appendChild(imageFichePays);
//	celluleFichePays.appendChild(lienFichePays);
//	ligne.appendChild(celluleFichePays);

	return ligne;
}

function DestinationEstVisible(zoneCode, paysId, destId, destEstXxx)
{
	var retour = true;
	
	retour = retour && (zoneCode==null || zoneCode=="" || this.zoneCode==zoneCode);
	retour = retour && (paysId ==null  || paysId ==""  || this.paysId  ==paysId  );
	retour = retour && (destId ==null  || destId ==""  || this.destId  ==destId  );
	if (destEstXxx=="vol")
		retour = retour && this.destEstVol;
	if (destEstXxx=="voyage")
		retour = retour && this.destEstVoyage;
	if (destEstXxx=="week-end")
		retour = retour && this.destEstWeekEnd;

	return retour;
}

// Tri des pays par libellé pays
function surTriPays1(pays1, pays2)
{
	var cle1 = pays1.paysLib;
	var cle2 = pays2.paysLib;
	if (cle1==cle2)
		return 0;
	else if (cle1>cle2)
		return 1;
	else
		return -1;
}

// Tri des destinations par libellé pays / libellé destination
function surTriDestinations1(dest1, dest2)
{
	var cle1 = dest1.paysLib + "/" + dest1.destLib;
	var cle2 = dest2.paysLib + "/" + dest2.destLib;
	if (cle1==cle2)
		return 0;
	else if (cle1>cle2)
		return 1;
	else
		return -1;
}

// Tri des destinations par libellé destination
function surTriDestinations2(dest1, dest2)
{
	var cle1 = dest1.destLib;
	var cle2 = dest2.destLib;
	if (cle1==cle2)
		return 0;
	else if (cle1>cle2)
		return 1;
	else
		return -1;
}

function surClicDestEstXxx()
{
	afficherDestinations();
	return false;
}

function afficherDestinations()
{
	//var resultats = document.getElementById("idResultats");
	
	var resultats = document.getElementById("sousbloc_choixDestinations");
	
	if (resultats==null)
		return;
		
    //resultats.style.visibility = "visible";
    resultats.style.display = "block";
    
	var tableau = document.getElementById("idDestTableau");
	if (tableau==null)
		return;
	// paramètres
	var zoneCode   = formGetCombo(document.getElementById('select_zone'));
	// TODO: paysId au lieu de paysLib dans la valeur de la combo ?
	var paysId     = formGetCombo(document.getElementById('select_pays'));
	// TODO: destId au lieu de destLib dans la valeur de la combo ?
	var destId     = formGetCombo(document.getElementById('select_dest'));
	//var destEstXxx = formGetRadio(document.Form1.RgDestEstXxx);//JLD 20080128
	// vérifier qu'il y a au moins un paramètre de recherche
	if ((zoneCode==null || zoneCode=="") && (paysId==null || paysId=="") )//JLD 20080128 && (destId==null || destId=="")
	{
		alert("Vous devez sélectionnez au moins un critère de recherche !");
		return;
	}
	//alert(zoneCode + ", " + paysId + ", " + destId + ", " + destEstXxx);
	// vider le tableau
	for (var i=tableau.rows.length-1; i>=0; i--)
	{
		tableau.removeChild(tableau.rows[i]);
	}
	var destinations = new Array();
	for (var i=0; i<tabDestinations.length; i++)
	{
		// TODO: ne mettre que les destinations correspondant aux paramètres
		if (tabDestinations[i].estVisible(zoneCode, paysId, destId))//JLD 20080128 , destEstXxx
		{
			destinations[destinations.length] = tabDestinations[i];
		}
	}
	destinations.sort(surTriDestinations1);
	for (var i=0; i<destinations.length; i++)
	{
		// afficher les destinations
		tableau.appendChild(destinations[i].genererLigne());
	}
}

function surClicDestinationFiche()
{
	//window.location.href = "DestinationFiche.aspx?DEST_ID=" + this.Destination.destId;
	//window.location.href = "DestinationFiche.aspx?ARBO_ID=" + urlArboId + "&ARBO_VERSION=" + urlArboVersion + "&DEST_ID=" + this.Destination.destId;
	var url = this.Destination.destUrl;
	var p = url.lastIndexOf("/");
	window.location.href = url.substring(p+1);
	return false;
}

function surClicPaysFiche()
{
	window.location.href = "PaysFiche.aspx?ARBO_ID=" + urlArboId + "&ARBO_VERSION=" + urlArboVersion + "&PAYS_ID=" + this.Pays.paysId;
	return false;
}

function surChargementPageDestination(arboId, arboVersion)
{
	urlArboId      = arboId;
	urlArboVersion = arboVersion;
}

function getParametreUrl(param)
{
    tmpVal = "";
    
    var nom=new Array();
    var valeur=new Array();

    // On enlève le ?
    listeParam = window.location.search.slice(1,window.location.search.length);

    // On sépare le paramètres....
    // first[0] est de la forme param=valeur

    first = listeParam.split("&");

    for(i=0;i<first.length;i++){
        second = first[i].split("=");
        nom[i] = second[0];
        valeur[i] = second[1];
        
        if ( nom[i] == param )
        {
            tmpVal=valeur[i];
        }
    }
    
    return tmpVal
}

var urlArboId = getParametreUrl("ARBO_ID");
var urlArboVersion = getParametreUrl("ARBO_VERSION");

