/* ------------------------------ GESTION DES ONGLETS --------------------------------- */

function showOnglet(id) {
	// Désactivation de l'onglet activé
	var tab=document.getElementsByTagName('DIV');
	for (var i=0;i<tab.length;i++) {
		if ((' '+tab[i].className).indexOf('divOngletActive')>0) {
			tab[i].className='divOngletInactive resize';
		}
		if ((' '+tab[i].className).indexOf('divTitreOngletActive')>0) {
			tab[i].className='divTitreOngletInactive';
		}
	}
	// Activation de l'onglet demandé
	var obj=document.getElementById('Onglet'+id);
	obj.className='divOngletActive resize';
	if (document.getElementById('TitreOnglet' + id)) {
		var obj = document.getElementById('TitreOnglet' + id);
		obj.className = 'divTitreOngletActive';
	}
}
var scrollTimerOnglet=null;
var idScrolling='';
var modeScrolling='';
function scrollOnglet() {
	var obj=document.getElementById(idScrolling);
	if (modeScrolling=='up') {
		obj.scrollTop-=10;
	} else if (modeScrolling=='down') {
		obj.scrollTop+=10;
	}
}
function startScrollOnglet(id,mode) {
	modeScrolling=mode;
	idScrolling=id;
	scrollTimerOnglet=setInterval('scrollOnglet()',1000);
}
function stopScrollOnglet () {
	scrollTimerOnglet=null;
}
/* -------------------------- AFFICHAGE DES STATIONS ----------------------------------- */
function Sstation(id,nom) {
        this.id=id;
	this.nom=nom;
}
var tabSstation=null;
function ShowMassif(massif_selected) {
	if (tabSstation==null) {
		eval(requestSelect('/scripts/getAllStation.php'));
	}
	var pays_selected='';
	if (typeof(massif_selected)=='undefined') {
		var massif_selected=0;
	} else {
		if (parseInt(massif_selected)>0) {
			var massif_selected=parseInt(massif_selected);
		} else {
			pays_selected=massif_selected;
			var massif_selected=0;
		}
	}
	// destruction de toutes les options
	var obj=document.getElementById("StationID1");
	while (obj.options.length>1) {
		obj.options[(obj.options.length-1)]=null;
	}
	for(var j=0;j<tabStation.length;j++) {
		var affiche=false;
		if ((massif_selected>0)&&(tabStation[j].id_massif==massif_selected)) {
			affiche=true;
		} else if ((pays_selected!='')&&(tabStation[j].pays==pays_selected)) {
			affiche=true;
		} else if ((pays_selected=='')&&(massif_selected==0)) {
			affiche=true;
		}
		if (affiche) {
			obj.options[obj.options.length]=new Option(tabStation[j].nom,tabStation[j].id);
			if (tabSstation!=null) {
				for(var k=0;k<tabSstation.length;k++) {
					if((tabSstation[k].id==tabStation[j].id)&&(tabSstation[k].nom!=tabStation[j].nom)) {
						obj.options[obj.options.length]=new Option('   - '+tabSstation[k].nom,tabStation[j].id);
						obj.options[obj.options.length-1].style.color=couleur_texte;
					}
				}
			}
		}
	}
}
/* -------------------------------- Adaptation de la taille du texte au contenu ----------- */
function fontsize() {
	var tab=document.getElementsByTagName('DIV');
	for (var i=0;i<tab.length;i++) {
		if ((' '+tab[i].className).indexOf('resize')>0) {
			if (tab[i].style.fontSize=='') {
				tab[i].style.fontSize='100%';
			}
			while (tab[i].scrollHeight>tab[i].offsetHeight) {
				tab[i].style.fontSize=(parseInt(tab[i].style.fontSize)-1)+'%';
			}
			if ((' '+tab[i].className).indexOf('width')>0) {
				while ((tab[i].scrollWidth>tab[i].offsetWidth)&&(parseInt(tab[i].style.fontSize)>=40)) {
					tab[i].style.fontSize=(parseInt(tab[i].style.fontSize)-1)+'%';
				}
			}
			//tab[i].style.overflow='visible'; // pour le cadre thématique
		}
	}
}
/* ------------------------------- Animation des promotions ------------------------------ */
var nbAnimPromo=3;
function movePromo(obj,from,to) {
	var tmp=obj.childNodes[from];
	obj.removeChild(obj.childNodes[from]);
	if (to!=null) {
		obj.insertBefore(tmp,obj.childNodes[to]);
	} else {
		obj.appendChild(tmp);
	}
}
function scrollPromo (mode) {
	var obj=document.getElementById('divAnimPromo');
	if (obj.childNodes.length>=(nbAnimPromo+2)) {
		if (mode=='right') {
			if (obj.childNodes.length>=(nbAnimPromo*2+2)) {
				var max=nbAnimPromo*2+1;
			} else {
				var max=obj.childNodes.length-1;
			}
			for (var i=(nbAnimPromo+2);i<=max;i++) {
				movePromo(obj,i,i-1);
			}
			for (var i=1;i<=nbAnimPromo-(nbAnimPromo*2+1-max);i++) {
				movePromo(obj,1);
			}
		} else if (mode=='left') {
			for (var i=nbAnimPromo;i>=1;i--) {
				movePromo(obj,i,i+1);
			}
			// Déplacement de la dernière offre
			var nb=obj.childNodes.length;
			for (var i=1;i<=nbAnimPromo;i++) {
				movePromo(obj,obj.childNodes.length-1,1);
			}
		}
	}
}

/* ------------------------------- Animation des prestiges ------------------------------ */

var defilement_prestige = true; // Défilement automatique activé par défaut
var compteur_prestige; // Déclaration globale du compteur
InitialiserPrestige(); // Ajout des fonctionnalités sur chaque bouton


function InitialiserPrestige(){
    // Div cachée contenant l'id de l'offre en cours d'affichage
    document.getElementById('id_courante').innerHTML = 0;
    // Bouton précédent
    document.getElementById('bouton_precedent').innerHTML = '<a href="#" onclick="AfficherPrestige(' + (document.getElementById('divPrestigeContenu').childNodes.length-1) + ');return false;"><</a>';
    // Pour chaque bouton avec un numéro
    for(i=0; i<4; i++) document.getElementById('bouton_lien'+i).innerHTML = '<a href="#" onclick="AfficherPrestige(' + i + ');return false;">' + (i+1) +'</a>';
    // Bouton suivant
    document.getElementById('bouton_suivant').innerHTML = '<a href="#" onclick="AfficherPrestige(1);return false;">></a>';
    // Lancement ou non du défilement automatique
    if(defilement_prestige) StartDefilement();
    else StopDefilement();
}

function AfficherPrestige(id_offre){
    // Vérification de l'id de l'offre à afficher
    if(id_offre < 0 ) id_offre = document.getElementById('divPrestigeContenu').childNodes.length - 1;
    else if(id_offre >= document.getElementById('divPrestigeContenu').childNodes.length) id_offre = 0;
    // Arrét du défilement automatique
    if(defilement_prestige) ResetDefilement();
    // Récupération du contenu de la div avec les infos sur les offres
    var offres = document.getElementById('divPrestigeContenu');
    // Mise à jour de l'affichage des infos concernant l'offre en cours
    for(var i=0; i<offres.childNodes.length; i++){
        if(i == id_offre) offres.childNodes[i].className = offres.childNodes[i].className.replace(' offre',' offreSelect');
        else offres.childNodes[i].className = offres.childNodes[i].className.replace(' offreSelect',' offre')
    }
    // Div cachée contenant l'id de l'offre en cours d'affichage
    document.getElementById('id_courante').innerHTML = id_offre;
    // Bouton précédent
    document.getElementById('bouton_precedent').innerHTML = '<a href="#" onclick="AfficherPrestige(' + (id_offre-1) + ');return false;"><</a>';
    // Recherche de la plage de liens
    var trouve = false;
    var premier = 0;
    while(!trouve){
        if((premier <= id_offre) && (id_offre < premier+4)) trouve = true;
        else premier += 4;
    }
    // Pour chaque bouton avec un numéro
    for(i=0; i<4; i++){
        document.getElementById('bouton_lien'+i).innerHTML = '<a href="#" onclick="AfficherPrestige(' + (premier+i) + ');return false;">' + (premier+i+1) +'</a>';
        if((premier+i) == id_offre) document.getElementById('bouton_lien'+i).className='navigSelect';
        else document.getElementById('bouton_lien'+i).className='';
    }
    // Bouton suivant
    document.getElementById('bouton_suivant').innerHTML = '<a href="#" onclick="AfficherPrestige(' + (id_offre+1) + ');return false;">></a>';
    // Relance du défilement automatique
    if(defilement_prestige) StartDefilement();
}

function StartDefilement(){
    defilement_prestige = true;
    compteur_prestige = window.setTimeout("AfficherPrestige(" + (parseInt(document.getElementById('id_courante').innerHTML) + 1) + ")", 2000);
    document.getElementById('bouton_lecture').className = 'pause';
    document.getElementById('bouton_lecture').innerHTML = '<a href="#" onclick="StopDefilement();return false;">||</a>';
}

function ResetDefilement(){
    window.clearTimeout(compteur_prestige);
}

function StopDefilement(){
    defilement_prestige = false;
    window.clearTimeout(compteur_prestige);
    document.getElementById('bouton_lecture').className = 'lecture';
    document.getElementById('bouton_lecture').innerHTML = '<a href="#" onclick="StartDefilement();return false;">&nbsp;&nbsp;</a>';
}

window.addEvent('load',fontsize);

// ----------------------- Fonctions utiles pour le formulaire de recherche ------------------------------------

var recherche = {
    debuts_sejours : new Array,
    debuts_courts_sejours : new Array,
    departements : new Array,
    stations : new Array,
    nb_criteres : 0,
    changeNbSemaines : function (NbSemaines) {
        var f=$('formSearch');
        // Mémorisation de la date sélectionnée
        var DteFrom=f.DteFrom.options[f.DteFrom.selectedIndex].value;
        if (this.debuts_sejours.length==0) {
            for (var i=1;i<f.DteFrom.options.length;i++) {
                this.debuts_sejours[this.debuts_sejours.length]=new DebSejour(f.DteFrom.options[i].value,f.DteFrom.options[i].innerHTML);
            }
        }
        if (NbSemaines=='0') {
            for (var i=(f.DteFrom.options.length-1);i>0;i--) {
                f.DteFrom.options[i]=null;
            }
            if (this.debuts_courts_sejours.length == 0) {
                var requete = new Request({
                url: '/scripts/getCSdeb.php',
                method: 'get',
                // A la fin, on recharge le formulaire de recherche
                onComplete: function(responseText) {
                    var f=$('formSearch');
                    eval(responseText);
                    for (var i=0;i<recherche.debuts_courts_sejours.length;i++) {
                        f.DteFrom.options[f.DteFrom.options.length]=new Option(recherche.debuts_courts_sejours[i].text,recherche.debuts_courts_sejours[i].value);
                        if (recherche.debuts_courts_sejours[i].value==DteFrom) {
                            f.DteFrom.options[f.DteFrom.options.length-1].selected=true;
                        }
                    }
                }
                });
                requete.send();
            }
        } else {
            for (var i=(f.DteFrom.options.length-1);i>0;i--) {
                f.DteFrom.options[i]=null;
            }
            for (var i=0;i<this.debuts_sejours.length;i++) {
                f.DteFrom.options[f.DteFrom.options.length]=new Option(this.debuts_sejours[i].text,this.debuts_sejours[i].value);
                if (this.debuts_sejours[i].value==DteFrom) {
                    f.DteFrom.options[f.DteFrom.options.length-1].selected=true;
                }
            }
        }
    },
    majDepartements : function () {
        if (this.stations.length==0) {
            var requete = new Request({
            url: '/scripts/getSearch.php',
            method: 'get',
            // A la fin, on recharge le formulaire de recherche
            onComplete: function(responseText) {
                var f=$('formSearch');
                eval(responseText);
                recherche.majDepartements();
            }
            });
            requete.send();
            return false;
        }
        var f=$('formSearch');
        var dpt_selected=f.DptID.options[f.DptID.selectedIndex].value;
        // On vide la liste
        while (f.DptID.options.length>1) {
            f.DptID.options[f.DptID.options.length-1]=null;
        }
        // Recherche du pays/massif sélectionné
        var massif_selected=f.MassifID.options[f.MassifID.selectedIndex].value;
        // Recherche la liste des départements possibles pour ce massif/pays
        var tab_dpt=new Array();
        for (i=0;i<this.stations.length;i++) {
            if (((massif_selected=='')||(this.stations[i].id_massif==massif_selected))) {
                if ((this.stations[i].id_departement!=0)&&(array_search(this.stations[i].id_departement,tab_dpt)===false)) {
                    tab_dpt[tab_dpt.length]=this.stations[i].id_departement;
                }
            }
        }
        // Récupération des objets département dans le tableau généré
        for (i=0;i<tab_dpt.length;i++) {
            var trouve=false;
            var j=0;
            while ((j<this.departements.length)&&(!trouve)) {
                if (tab_dpt[i]==this.departements[j].id) {
                    trouve=true;
                    tab_dpt[i]=this.departements[j];
                }
                j++;
            }
        }
        // Affichage des départements dans l'ordre des numéros
        for(i=0;i<tab_dpt.length;i++) {
            var min=tab_dpt[i].numero;
            var jmin=i;
            for (j=i+1;j<tab_dpt.length;j++) {
                if (tab_dpt[j].numero<min) {
                    min=tab_dpt[j].numero;
                    jmin=j;
                }
            }
            if (jmin!=i) {
                var tmp=tab_dpt[i];
                tab_dpt[i]=tab_dpt[jmin];
                tab_dpt[jmin]=tmp;
            }
            var ioption=f.DptID.options.length;
            f.DptID.options[ioption]=new Option(tab_dpt[i].numero+' - '+tab_dpt[i].nom,tab_dpt[i].id);
            if (tab_dpt[i].id==dpt_selected) {
                f.DptID.options[ioption].selected=true;
            }
        }
        this.majStations();
    },
    majStations : function() {
        if (this.stations.length==0) {
            var requete = new Request({
            url: '/scripts/getSearch.php',
            method: 'get',
            // A la fin, on recharge le formulaire de recherche
            onComplete: function(responseText) {
                var f=$('formSearch');
                eval(responseText);
                recherche.majStations();
            }
            });
            requete.send();
            return false;
        }
        var f=document.getElementById('formSearch');
        var station_selected=f.StationID.options[f.StationID.selectedIndex].value;
        // On vide la liste
        while (f.StationID.options.length>1) {
            f.StationID.options[f.StationID.options.length-1]=null;
        }
        var dpt_selected=0;
        var pays_selected='';
        var massif_selected=0;
        // On recherche le département sélectionné
        if (f.DptID) {
            if (f.DptID.options[f.DptID.selectedIndex].value!='0') {
                dpt_selected=parseInt(f.DptID.options[f.DptID.selectedIndex].value);
            }
        }
        if (!dpt_selected) {
            var massif_selected=f.MassifID.options[f.MassifID.selectedIndex].value;
            if (parseInt(massif_selected)>0) {
                massif_selected=parseInt(massif_selected);
            } else if (massif_selected!='0') {
                pays_selected=massif_selected;
                massif_selected=0;
            }
        }
        for (var i=0;i<this.stations.length;i++) {
            var afficher=true;
            if ((dpt_selected>0)&&(this.stations[i].id_departement!=dpt_selected)) {
                afficher=false;
            } else if ((massif_selected>0)&&(this.stations[i].id_massif!=massif_selected)) {
                afficher=false;
            } else if ((pays_selected!='')&&(this.stations[i].pays!=pays_selected)) {
                afficher=false;
            }
            if (afficher) {
                var ioption=f.StationID.length;
                f.StationID.options[ioption]=new Option(this.stations[i].nom,this.stations[i].id);
                if (this.stations[i].id==station_selected) {
                    f.StationID[ioption].selected=true;
                }
            }
        }
    },
    changeFormule : function(obj) {
        var f=$('formSearch');
        if (f.checkLogement&&f.checkMateriel&&f.checkForfait) {
            // Si les coches logement, matériel et forfait existent
            if ((!f.checkLogement.checked)&&(!f.checkMateriel.checked)&&(!f.checkForfait.checked)) {
                // Si aucune des 3 n'est cochée, on coche le logement
                eval('f.'+obj+'.checked=true;');
            }
            if (f.checkLogement.checked) {
                f.TypeLog.disabled=false;
            } else {
                f.TypeLog.disabled=true;
            }
        }
    },
    onCritere : function (ID) {
            var img=$('img'+ID);
            eval('var obj=$(\'formSearch\').'+ID+';');
            if (obj.value=='') {
                    img.style.borderColor='#000000';
            }
    },
    outCritere : function (ID) {
            var img=$('img'+ID);
            eval('var obj=$(\'formSearch\').'+ID+';');
            if (obj.value=='') {
                    img.style.borderColor='transparent';
            }
    },
    checkCritere : function(ID) {
            var img=$('img'+ID);
            eval('var obj=$(\'formSearch\').'+ID+';');
            if ((obj.value=='')&&(this.nb_criteres<3)) {
                    this.nb_criteres++;
                    obj.value='True';
                    img.style.borderColor='red';
            } else if (obj.value!='') {
                    this.nb_criteres--;
                    obj.value='';
                    img.style.borderColor='#000000';
            }
    }
};

function Station (id,id_departement,id_massif,pays,nom) {
	this.id=id;
        this.id_departement=id_departement;
	this.id_massif=id_massif;
	this.pays=pays;
	this.nom=nom;
}

function Departement (id,nom,numero) {
        this.id = id;
        this.nom = nom;
        this.numero = numero;
}

function DebSejour(value,text) {
	this.value=value;
	this.text=text;
}







