/*
	ED - 12.07.2006
	Fonction AddEvent de John Resig 
	http://ejohn.org/projects/flexible-javascript-events/
*/

function addEvent( obj, type, fn ) {
   if ( obj.attachEvent ) {
     obj['e'+type+fn] = fn;
     obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
     obj.attachEvent( 'on'+type, obj[type+fn] );
   } else
     obj.addEventListener( type, fn, false );
 }
 
function removeEvent( obj, type, fn ) {
  if ( obj.detachEvent ) {
    obj.detachEvent( 'on'+type, obj[type+fn] );
    obj[type+fn] = null;
  } else
    obj.removeEventListener( type, fn, false );
}

// Fonction d'animation des logos défilants dans le footer
$(document).ready(function() {
		$("#partenaires").jCarouselLite({
    		auto: 2000,
    		speed: 2000
		});
});

// #####################################################################################
// Fonctions liées au menu vertical

var is_chrome = /chrome/.test( navigator.userAgent.toLowerCase() );
var is_ie6 = ($.browser.msie && $.browser.version < 7);

function ouvreUl(id){
          // fermeture de la rubrique ouverte
          fermeUl($("#ouvert"));
          // Ouverture de la rubrique
		  id.attr("id","ouvert");
		  id.parent("li").addClass("enCours");
          id.show(100,function(){$(".enCours").filter(id).removeClass("enCours"); });
        }

function fermeUl(id){
          id.hide(100,function(){$(this).parent("li").removeClass("enCours")});
          id.attr("id","");
        }

function switchUl(id){
          if (id.find("ul").attr("id") == "ouvert") {
            fermeUl(id.find("ul"));
          } else {
            ouvreUl(id.find("ul"));
          }
}

function initSousMenuVertical(_ul){
    _ul.hover(function (){ $(this).addClass("menuPlus");}, function (){ $(this).removeClass("menuPlus");});
	_ul.bind("click",function(){ switchUl($(this));} );
}


function initMenuVertical(){
    $(".rubNiveau1>ul").hide();
	$(".rubNiveau1>ul").parent().addClass("ssRub");
	$(".enCours ul").show().attr("id","ouvert");
	// Les animations de l'interface sont effectuées seulment sur les navigateurs récents
	if (!is_ie6) {
		initSousMenuVertical($(".ssRub"));
		$(".ssRub").hover(
			function() { if (!$(this).hasClass("enCours")) { $(this).animate( {marginRight: "0px"},100 );} }, 
			function() { if (!$(this).hasClass("enCours")) {$(this).animate( {marginRight: "10px"},100 );} }
		);
		// Au clic sur un lien dans le menu
    	$(".rubNiveau1 a").bind("click",function(e){
			 // Effet fluide qui ramène la page en haut avant de changer de rubrique 
			 $.scrollTo('0px',150,{easing:'swing'}); 
			 // on veut empecher que le sous menu ne se ferme avant rechargement (à cause du switchUL venant du ul parent) 
			 var doStop = ($(this).parent().hasClass("enCours") || $(this).parent().parent().attr("id")=="ouvert" )
			 if ( doStop ) {
				e.stopPropagation();
			 } else {
				fermeUl($(".enCours ul")); 
			 }
		});
	
		// Initialisation du sous menu services numériques
		$("div.servicesNumeriques").hover(
			function(){$("#menuSN").show(100);},
			function(){$("#menuSN").slideUp(100);}
		);
		$("body").bind("click",function(){$("#menuSN").slideUp(100);});
	}
}

// Fin menu vertical
//-------------------------------------------------------------------------------

function envoyerFormulaireRI() {
	$("body, input").css("cursor","wait"); $("#wait").css("visibility","visible");  
	$.post(
		   //url
		   "/specific/jsp/RIAjax.jsp", 
		   { 
		   	   //data
			   ORIGINE: $("#ORIGINE").val(),
			   VILLE: $("#VILLE").val(),
			   PAYS: $("#PAYS").val(),
			   DESTINATION: $("#DESTINATION").val(), 
			   CODE_RUBRIQUE:$("#CODE_RUBRIQUE").val() 
		   },
		   function(html) {
			   // callback
			   $("#t_res").html(html); 
			   $("#sortable").tablesorter(); 
			   $("body, input").css("cursor","default"); 
			   $("#wait").css("visibility","hidden"); 
		   },
		   "text") ;
} 

function initRI(){
	jQuery.ajaxSetup({ contentType: "application/x-www-form-urlencoded; charset=UTF-8"});
	$("#ORIGINE,#VILLE,#PAYS,#DESTINATION").bind("change",function(){$(this).parent().next().find("img").css("visibility","visible");});
  	$(".reset").hover(function(){$(this).css("cursor","pointer");},function(){$(this).css("cursor","default");});
  	$(".reset").bind("click",function(){$(this).css("visibility","hidden"); $(this).parent().prev().find("select").each(function(){ this[0].selected=true; });});
	$(".reset").css("visibility","hidden");
}

function initActu(){
	var i=0;
	$(".blocActu").each(function(i){if (i%2 == 1) {$(this).addClass("right"); i++;} });
}

$(document).ready(function(){
  	initMenuVertical();
	initActu();
  	$("#MOTS_CLEFS").bind("focus",function(){$(this).select();});
});

/*
// Gestion des liens externes en Js
function open_ext_link()
{
  var liens = document.getElementsByTagName('a');
	// On récupère tous les liens (<a>) du document dans une variable (un array), ici liens.
	// Une boucle qui parcourt le tableau (array) liens du début à la fin.
	for (var i = 0 ; i < liens.length ; ++i)  {
		// Si les liens ont un nom de class égal à lien_ext, alors on agit.
		if (liens[i].className == 'lien_ext')  {
			liens[i].title = 'S\'ouvre dans une nouvelle fenêtre';
			// Au clique de la souris.
			liens[i].onclick = function()  {
				window.open(this.href);
				return false; // On ouvre une nouvelle page ayant pour URL le href du lien cliqué et on inhibe le lien réel.
			};
		}
	}
}

window.onload = open_ext_link;
// Au chargement de la page, on appelle la fonction.
*/

/* Affichage d'une image type plan d'accès */
function ouvrirFenetrePlan(url, nom) {
   window.open(url, nom, "width=520,height=500,scrollbars=yes, status=yes");
}

function showMessageField(typeAide, f1, f2) {
	showMessageField2(typeAide, f1, f2, '');
}

/* Demande d'affichage d'une fenetre par un champ */
function showMessageField2(typeAide, f1, f2, form) {
	numToolbox = '';
	nomForm = form;
	field1 = f1;
	field2 = f2;
	fieldRequete = '';
	texte = '';
	nomApplet = '';
	// Type d'insertion (liste)
	typeInsertion = '';

	//AM 200309 : L'arbre des structures doit prendre en compte la langue courante dans le front office
	if (typeAide.indexOf('structure') != -1) {

	   	var indexSlash1 = typeAide.indexOf('/');
		var lg = '';
		var filtre = '';
		if (indexSlash1 != -1)
		{
			var indexSlash2 = typeAide.indexOf('/', indexSlash1 + 1);
			if (indexSlash2 != -1)
			{
				lg = typeAide.substring(indexSlash1 + 1, indexSlash2);
				filtre = typeAide.substring(indexSlash2 + 1);
			}
			else
			{
				lg = typeAide.substring(indexSlash1 + 1);
			}
		}
		sList = window.open('/adminsite/menu/menu.jsp?MODE=STRUCTURE&LANGUE='+lg+'&FILTRE='+filtre, 'menu2', 'scrollbars=yes, resizable=yes, status=yes, width=600, height=400, top=320, left=320');
	}

	//JSS 20040419 : affichage arbre structure en fonction du perimetre (back-office)
	else if (typeAide.indexOf('strbo/') != -1) {

		// formatte comme suit strbo/type/objet/action/langue
		var indexSlash1 = typeAide.indexOf('/');
		var indexSlash2 = typeAide.indexOf('/', indexSlash1 + 1);
		var indexSlash3 = typeAide.indexOf('/', indexSlash2 + 1);
		var indexSlash4 = typeAide.indexOf('/', indexSlash3 + 1);
		var indexSlash5 = typeAide.indexOf('/', indexSlash4 + 1);

		var permission = typeAide.substring(indexSlash1+1, indexSlash4);
		var lg = typeAide.substring(indexSlash4+1, indexSlash5);
		var filtre = typeAide.substring(indexSlash5+1, typeAide.length);

		sList = window.open('/adminsite/menu/menu.jsp?MODE=STRUCTURE&PERMISSION='+permission+'&LANGUE='+lg+'&FILTRE='+filtre, 'menu2', 'scrollbars=yes, resizable=yes, status=yes, width=600, height=400, top=320, left=320');
	}

	//FBI 20051110 : affichage arbre structure en front (filtre sur les structures non visibles)
	else if (typeAide.indexOf('strfo') != -1) {

	   	var indexSlash1 = typeAide.indexOf('/');
		var lg = '';
		var filtre = '';
		if (indexSlash1 != -1)
		{
			var indexSlash2 = typeAide.indexOf('/', indexSlash1 + 1);
			if (indexSlash2 != -1)
			{
				lg = typeAide.substring(indexSlash1 + 1, indexSlash2);
				filtre = typeAide.substring(indexSlash2 + 1);
			}
			else
			{
				lg = typeAide.substring(indexSlash1 + 1);
			}
		}
		sList = window.open('/adminsite/menu/menu.jsp?MODE=STRUCTURE&LANGUE='+lg+'&FILTRE='+filtre+'&FRONT=true', 'menu2', 'scrollbars=yes, resizable=yes, status=yes, width=600, height=400, top=320, left=320');
	}

	else if (typeAide == 'rubrique' || typeAide.indexOf('rubrique') != -1) {
		if (typeAide == 'rubrique') {
			 sList = window.open('/adminsite/menu/menu.jsp?MODE=RUBRIQUE', 'menu2', 'scrollbars=yes, resizable=yes, status=yes, width=600, height=400, top=320, left=320');
		}
		else {
			 var lg = typeAide.substring(typeAide.indexOf('rubrique')+8,typeAide.length);
			 if (lg != null && lg.length > 0)
				 sList = window.open('/adminsite/menu/menu.jsp?MODE=RUBRIQUE&LANGUE='+lg, 'menu2', 'scrollbars=yes, resizable=yes, status=yes, width=600, height=400, top=320, left=320');
			 else
				 sList = window.open('/adminsite/menu/menu.jsp?MODE=RUBRIQUE', 'menu2', 'scrollbars=yes, resizable=yes, status=yes, width=600, height=400, top=320, left=320');
		 }
	}
	//JSS 20040419 : affichage arbre rubrique en fonction du perimetre (back-office)
	else if (typeAide.indexOf('rubbo/') != -1)	{
		// formatte comme suit rubbo/type/objet/action

		var indexSlash1 = typeAide.indexOf('/');
		var indexSlash2 = typeAide.indexOf('/', indexSlash1 + 1);
		var indexSlash3 = typeAide.indexOf('/', indexSlash2 + 1);

		var permission = typeAide.substring(indexSlash1+1,typeAide.length);
		sList = window.open('/adminsite/menu/menu.jsp?MODE=RUBRIQUE&PERMISSION='+permission, 'menu2', 'scrollbars=yes, resizable=yes, status=yes, width=600, height=400, top=320, left=320');
	}

	else if (typeAide.indexOf('fichefil') != -1) {
		var proc = typeAide.substring(typeAide.indexOf('fichefil')+8,typeAide.length).toUpperCase();
		sList = window.open("/servlet/com.jsbsoft.jtf.core.SG?PROC="+proc+"&ACTION=RECHERCHER&TOOLBOX=LIEN_INTERNE", "fichefil", "width=500,height=500,top=210,left=310,scrollbars=yes, status=yes");
	}

	else if (typeAide == 'pagelibre') {
		sList = window.open("/servlet/com.jsbsoft.jtf.core.SG?PROC=SAISIE_PAGELIBRE&ACTION=RECHERCHER&TOOLBOX=LIEN_INTERNE&LANGUE_ARTICLE=0", "pagelibre", "width=500,height=330,top=210,left=310,scrollbars=yes, status=yes");
	}

	/* AM 200309 creation de page libre */
	else if (typeAide == ('pagelibre_creation')){
		 field2="LIBELLE_CODE_PAGE_TETE";
		 sList = window.open("/servlet/com.jsbsoft.jtf.core.SG?PROC=TRAITEMENT_PAGELIBRE&ACTION=AJOUTER&LANGUE=0&CODE_RUBRIQUE="+f2, "pagelibre_creation", "width=500,height=330,top=210,left=310,scrollbars=yes, status=yes");
	}

	// JSS 20040419 : arbre des groupes
	else if (typeAide == 'groupe_dsi') {
		sList = window.open('/adminsite/menu/menu.jsp?MODE=GROUPE', 'menu2', 'scrollbars=yes, resizable=yes, status=yes, width=600, height=400, top=320, left=320,resizable=yes,status=yes');
	}

	//JSS 20040419 : affichage arbre groupe en fonction du perimetre (back-office)
	else if (typeAide.indexOf('groupebo/') != -1)	{
		// formatte comme suit groupebo/type/objet/action

		var indexSlash1 = typeAide.indexOf('/');
		var indexSlash2 = typeAide.indexOf('/', indexSlash1 + 1);
		var indexSlash3 = typeAide.indexOf('/', indexSlash2 + 1);

		var permission = typeAide.substring(indexSlash1+1,typeAide.length);
		sList = window.open('/adminsite/menu/menu.jsp?MODE=GROUPE&PERMISSION='+permission, 'menu2', 'scrollbars=yes,,resizable=yes,width=350,height=210,top=320,left=320, resizable=yes,status=yes');
	}

	else if (typeAide == 'public_vise_dsi') {
		sList = window.open('/adminsite/menu/menu.jsp?MODE=GROUPE&PUBLIC_VISE=1', 'menu2', 'scrollbars=yes,width=420,height=210,top=320,left=320, resizable=yes,status=yes');
	}

	// JSS 20040419 : arbre des groupes
	else if (typeAide.indexOf('publicbo/') != -1)	{
		// formatte comme suit publicbo/type/objet/action

		var indexSlash1 = typeAide.indexOf('/');
		var indexSlash2 = typeAide.indexOf('/', indexSlash1 + 1);
		var indexSlash3 = typeAide.indexOf('/', indexSlash2 + 1);

		var permission = typeAide.substring(indexSlash1+1,typeAide.length);
		sList = window.open('/adminsite/menu/menu.jsp?MODE=GROUPE&PUBLIC_VISE=1&PERMISSION='+permission, 'menu2', 'scrollbars=yes,width=420,height=210,top=320,left=320, resizable=yes,status=yes');
	}

	else if (typeAide == 'utilisateur') {
		sList = window.open("/servlet/com.jsbsoft.jtf.core.SG?PROC=RECHERCHE_UTILISATEUR&ACTION=RECHERCHER", "annuaire2", "width=450,height=500,top=210,left=290,scrollbars=yes,resizable=yes,status=yes");
	}

	else if (typeAide == 'parcours') {
		sList = window.open("/servlet/com.jsbsoft.jtf.core.SG?PROC=SAISIE_UEUP&ACTION=RECHERCHER&TOOLBOX=LIEN_INTERNE&TYPE=PARCOURS&LANGUE_ARTICLE=-1", "parcours", "width=500,height=330,top=150,left=150,scrollbars=yes, status=yes");
	}

	else if (typeAide.indexOf('parcoursM') != -1) {
		var codeUe = typeAide.substring(9);
		sList = window.open("/servlet/com.jsbsoft.jtf.core.SG?PROC=TRAITEMENT_PARCOURS&ACTION=MODIFIER&CODE_UEUP_PARCOURS="+codeUe+"&TOOLBOX=LIEN_INTERNE&LANGUE_ARTICLE=-1", "parcours", "width=500,height=330,top=150,left=150,scrollbars=yes, status=yes");
	}

	else if (typeAide == 'photo') {
		indice='';
		if (f1=='' && f2=='')
		{
			indice = window.document.forms[indiceForm].NB_FICHIER_JOINT.value;
			eval('window.document.forms[indiceForm].NO_FICHIER_JOINT.value = '+indice+'');
		}
		sList = window.open("/servlet/com.jsbsoft.jtf.core.SG?PROC=SAISIE_PHOTO&ACTION=RECHERCHER&MODE_PHOTOTHEQUE=SELECTION&NO_FICHIER_JOINT="+indice, "photo", "width=650,height=535,top=20,left=100,scrollbars=yes, status=yes");
	}

	else if (typeAide == 'image') {
       sList = window.open("/servlet/com.jsbsoft.jtf.core.SG?PROC=SAISIE_PHOTO&ACTION=RECHERCHER&MODE_PHOTOTHEQUE=SAISIE", "photo", "width=650,height=535,top=20,left=100,scrollbars=yes, status=yes");
	}

	else if (typeAide == 'document') {
		sList = window.open("/servlet/com.jsbsoft.jtf.core.SG?PROC=TRAITEMENT_DOCUMENT&ACTION=INSERER", "document", "width=400, height=200, top=20, left=100, scrollbars=yes, status=yes, status=yes");
	}

	else if (typeAide == 'documentfront') {
		sList = window.open("/servlet/com.jsbsoft.jtf.core.SG?PROC=TRAITEMENT_DOCUMENT_FRONT&ACTION=INSERER&SAISIE_FRONT=true", "document", "width=400, height=130, top=20, left=100, scrollbars=yes, status=yes");
	}
	
	else if (typeAide == 'pagetete') {
		sList = window.open("/adminsite/toolbox/choix_objet.jsp?TOOLBOX=PAGE_TETE", "list", "width=500,height=330,top=100,left=100,scrollbars=yes,status=yes");
		pageTete = true;
	}

	else if (typeAide == 'commentaire') {
    		sList = window.open("/adminsite/toolbox/choix_objet.jsp?TOOLBOX=COMMENTAIRE", "list", "width=500,height=330,top=100,left=100,scrollbars=yes,status=yes");
		pageTete = true;
	}

	else if (typeAide == 'requete') {
		field1 = "";
		field2 = f2;
		nomForm = "";
		fieldRequete = f1;

		if (field2 == 'STATS') {
			field2 = "";
			sList = window.open("/adminsite/toolbox/choix_objet.jsp?TOOLBOX=LIEN_REQUETE&RESTRICTION=STATS", "list", "width=500,height=330,top=100,left=100,scrollbars=yes,status=yes");
		}
		else {
			field2 = "";
			sList = window.open("/adminsite/toolbox/choix_objet.jsp?TOOLBOX=LIEN_REQUETE&RESTRICTION=XML", "list", "width=500,height=330,top=100,left=100,scrollbars=yes,status=yes");
		}
	}
}


/* Traitement des donnees resultats renvoyees par la fenetre mere */
function saveField(objet, code, libelle) {

	/* Cas de l'administration */
	if (inBackOffice())	{
		if (!pageTete) {
				/* Cas de l'administration */
				if (field1 != "")
					eval( "window.document.forms[0]."+field1+".value = code;");
				if (field2 != "")
					eval( "window.document.forms[0]."+field2+".value = libelle;");
				
				//AM 200501: parcours LMD
				if(ajoutAuto != ""){
					eval("ajouter"+ajoutAuto+"();");
					ajoutAuto = "";
				}
				//AM 200501: parcours LMD
				if(modifAuto != ""){
					eval("validmodif"+modifAuto+"();");
					modifAuto = "";
				}
				
			}else{
				/* Cas d'une page de tete : on ajoute le type d'objet au code*/
				if (field1 != "")
					eval( "window.document.forms[0]."+field1+".value = code+',TYPE='+objet;");
				if (field2 != "")
					eval( "window.document.forms[0]."+field2+".value = objet + ' : ' +libelle;");
			}

	} else	{
		/* Cas particulier du WEB */
			var formCourant = "RECHERCHE_WEB";
			if( nomForm.length > 0)
				formCourant = nomForm;

		if (field1 != "")
			eval( "window.document."+formCourant+"."+field1+".value = code;");
		if (field2 != "")
			eval( "window.document."+formCourant+"."+field2+".value = libelle;");
	}
	
	if( fieldRequete != "") {
		chaine = "\"OBJET="+objet;
		if( code.length > 0)
		{
			chaine = chaine +"&"+code;
		}
		chaine = chaine +"\"";

		eval( "window.document.forms[0]."+fieldRequete+".value = "+ chaine );
	}

  field1 = "";
  field2 = "";

  nomForm = "";

  fieldRequete = "";

  numToolbox = "";

  texte = "";

  nomApplet = "";
  
  // JSS 20020612-001 Type d'insertion (liste)
  typeInsertion="";	
	
}


function inBackOffice () {

	for (i = 0; i < window.document.forms.length; i++)	{
		if ( (window.document.forms[i].name == 'RECHERCHE_WEB' || window.document.forms[i].name == nomForm )
			&& window.document.forms[i].name != "")
			return false;
	}
	return true;
}


/* Affichage d'un calendrier pour les formulaires avec recherche par date */
function affiche_calendrier(nom, form) {
	if (document.getElementById) {
		document.write("<a href=\"#\" onclick=\"window.open('/adminsite/calendrier/calendrierjs.html?champ=" + nom + "&form=" + form +"', 'calendrier', 'width=135, height=138, toolbar=no, location=no, status=yes, resizable=yes'); return false;\"><img src=\"/images/calendrier.png\" border=\"0\"></a>");
	}
}


/* Effacement du libelle d'un champ de recherche */
function effacerTextField (zoneSaisie, zoneLibelle, value, libelle)	{

	if (inBackOffice() )	{
		/* Cas de l'administration */
		eval( "window.document.forms[0]."+zoneSaisie+".value = value;");
		eval( "window.document.forms[0]."+zoneLibelle+".value = libelle;");
	} else	{
		eval( "window.document.RECHERCHE_WEB."+zoneSaisie+".value = value;");
		eval( "window.document.RECHERCHE_WEB."+zoneLibelle+".value = libelle;");
	}
}

/* Activation du service selectionné dans la liste des services */
function activerService() {

	var index = window.document.getElementById('selectservices').selectedIndex;
	if( index == 0)
		return;
	var url = window.document.getElementById('selectservices').options[index].value;

	/* Analyse target */
	var indexTarget = url.indexOf(';');
	if (indexTarget != -1) {
		var target =  url.substring(0,indexTarget);
		url = url.substring(indexTarget + 1);
		window.open(url, target);
	} else {
		window.location.href=url;
	}
}

/* Affichage d'une image dans une popup */
function afficheImage(source) {
	// Ouverture du pop-up
	window.open(source,'pop','status=no,directories=no,toolbar=no,location=no,menubar=no,scrollbars=yes,resizable=yes');
}

function atteindreAncre(ancre) {
	if (ancre != 'null'){
		window.location.href += '#' + ancre;
	}
}

/* Fonction utilisée dans la recherche avancée pour réinitialiser les formulaires */
function viderFormulaire(criteres) {
	criteres = criteres.split(";");
	var champReinit = "";
	var valeurChamp = "";
	
	for (var i=0; i < (criteres.length); i++) {
		champReinit = eval("document.RECHERCHE_WEB." + criteres[i].substring(0, criteres[i].indexOf("=")));
		valeurChamp = criteres[i].substring(criteres[i].indexOf("=")+1);
		
		champReinit.value = valeurChamp
	}
}


function afficherBoutonImprimer(intitule) {
	document.write('<span id="imprimer" onclick="window.print(); return false;">' + intitule + '</span>');
}
