var WIDTH_MAPO;

function goSVG(type){
	if (type == 'dip'){
		if (document.formu_dip.dip_form != null){
			var formulaire = document.formu_dip;
			var idEtape = formulaire.dip_form.value;
			var nomEtape = formulaire.dip_form.options[ formulaire.dip_form.options.selectedIndex].text;
			var reg = new RegExp("'","gi");
			nomEtape = nomEtape.replace(reg," ");
			document.location = "inc1_maporientation_recherche.php?recherche="+nomEtape+"&action=maporientation&rech=map&type_noeud=dip&id_noeud="+idEtape;
		}else{
			document.formu_dip.submit();			
		}
	}else{
		var formulaire = document.formu_pro;
			var idEtape = formulaire.emploi.value;
			var nomEtape = formulaire.emploi.options[ formulaire.emploi.options.selectedIndex].text;
			var reg = new RegExp("'","gi");
			nomEtape = nomEtape.replace(reg," ");

			//alert("inc1_maporientation_recherche.php?recherche="+encodeURI(nomEtape)+"&action=maporientation&rech=map&type_noeud=pro&id_noeud=em_"+idEtape);
			document.location = "inc1_maporientation_recherche.php?recherche="+nomEtape+"&action=maporientation&rech=map&type_noeud=pro&id_noeud=em_"+idEtape;		
			
	}
}

function majPro(tabConcerne,menuConcerne,menuEnCours){
	
	var formulaire = document.formu_pro;
	var menu = eval("formulaire."+menuConcerne);
	var tabTemp = eval(tabConcerne);
	var tabTempValue = eval(tabConcerne+"Value");
	//on réinitialise le menu déroulant
	//alert(menu.length);
	for (i=menu.length-1; i>0;i--){
		menu.options[i] = null;		
	}
	menu.length = 1;
	//alert(menuEnCours.value);
	var tabEmploi = tabTemp[menuEnCours.value].split("*");
	//alert(tabEmploi);
	var tabEmploiValue = tabTempValue[menuEnCours.value].split("*");
	for (i=1; i<tabEmploi.length;i++){
		menu.length = i+1;	
		var monOption = new Option(tabEmploi[i-1],tabEmploiValue[i-1]);
		menu.options[i]=monOption;		
	}	
}

function getParamFromUrl (url, param) {
	var monExpReg = new RegExp ('[&?]'+param+'=(\\w+)');
	return url.match (monExpReg);
}

function displayDebug (s) {
//	if (js_debug) {
		if (document.getElementById)
			document.getElementById("div_debug").innerHTML = '<a href="'+s+'" target="_blank">'+s+'</a>';
		else if (document.layers && document.layers["div_debug"])
			document.layers["div_debug"].value = '<a href="'+s+'" target="_blank">'+s+'</a>';
		else if (document.all)
			document.all["div_debug"].innerHTML = '<a href="'+s+'" target="_blank">'+s+'</a>';
//	}
}

function loadEntites (url)
{
	var before = "";
	//alert ('loadEntites : '+url);
	if (url == "" )
	{
		document.getElementById("dossiers").innerHTML = before;
		return;
	}

	var xhr = null; 
	if(window.XMLHttpRequest) // Firefox 
	   xhr = new XMLHttpRequest(); 
	else if(window.ActiveXObject) // Internet Explorer 
	   xhr = new ActiveXObject("Microsoft.XMLHTTP"); 
	else { // XMLHttpRequest non supporté par le navigateur 
	   return; 
	}
 	xhr.open("GET", url, true);
	xhr.onreadystatechange  = function () {
		if (xhr.readyState == 4)
			if (xhr.status == 200) {
				document.getElementById("dossiers").innerHTML = before + xhr.responseText;
			}
	}
	xhr.send(null);
}
var PAUSE = false;
function guideDisplay (url) {

 /* if (url.indexOf('itineraire-d-orientation') >= 0)
  {
    document.location.href = url;
    return;
  }
  if (document.getElementById("situation") && window.location.host.indexOf('fg3e') < 0)
    document.getElementById("situation").style.display = 'none';*/
  WIDTH_MAPO = document.getElementById("cadre_map").style.width;
 
	var w = document.getElementById("cadre_map").style.width;
	var str = '';
	
  document.getElementById("arbre_embed").style.height = document.getElementById("cadre_map").style.height;
	document.getElementById("arbre_embed").style.height = '0';
  if (!PAUSE)
  {
    PAUSE = true;
    call = 'guideDisplay ("'+url+'")';
    window.setTimeout(call,200);
    return ;    
  }
	var taburl = url.split(/\//);
	var reconstruct_url = '';
	for (i=0;i<taburl.length;i++)
	{
		if (taburl[i] == "guide_maporientation" || taburl[i] == "formations")
			var title = i + 1;
		else if (taburl[i] == "communaute" || taburl[i] == "article")
			var title = i + 3;
		else if (!title)
		{
			if (taburl[i] == "http:")
				continue;
			if (i > 0)
				reconstruct_url = reconstruct_url+"/";
			reconstruct_url = reconstruct_url + taburl[i];
		}
	}
	// IE et Firefox ne gère pas la chose de la même manière...
	reconstruct_url = ((reconstruct_url != "" && reconstruct_url.substr(0, 1) == "/" && reconstruct_url.substr(1, 1) != "/") ? "http:/" : "http:") + reconstruct_url;
	
	if (taburl[title] && taburl[title].search("&") != -1)
	{
		var data = taburl[title].split("&");
		var guide_title = data[0];
	}
	else
		var guide_title = taburl[title];
		
	var guide_langue = 'fr'; 
	if (!guide_title)
		if (getParamFromUrl(url, 'pageGuide'))
			var guide_title = getParamFromUrl(url, 'pageGuide');
		else
			var guide_title = 'Guide Maporientation';

	if (guide_title && guide_title.length > 0)
		str = str + '&guide_title='+guide_title[1];
	var guide_langue = getParamFromUrl(url, 'langue');
	if (guide_langue && guide_langue.length > 0)
		str = str + '&langue='+guide_langue[1];
	if (w < 1)
		w = '100%';
		//reseau='+reseau+'
	reconstruct_url = reconstruct_url.split('/fr')[0];
	var urlguide = reconstruct_url + '/includes/maporientation/arbre/guideEnTeteSvg_css.php?height_svg=22&width_svg='+w+'&guide_title='+guide_title;	
	//reconstruct_url = 'http://serveur/work/vianney/maporientation';
//	var urlguide = reconstruct_url + '/includes/maporientation/arbre/guideEnTeteSvg_css.php?&height_svg=22&width_svg='+w;

	document.getElementById("div_guide_entete").innerHTML = '<embed style="height:22px;width:'+w+';" id="guide_entete" TYPE="image/svg+xml" src="'+ urlguide +'" />';
	if (guide_title[1] == "contact" || guide_title[1] == "contact_formation" || guide_title == "partage-experience") 
	{
		iframeDisplay (url, guide_title);
		return;
	}
	
	document.getElementById("guide_div").style.display = 'block';

	var xhr = null; 
	if(window.XMLHttpRequest) // Firefox 
	   xhr = new XMLHttpRequest(); 
	else if(window.ActiveXObject) // Internet Explorer 
	   xhr = new ActiveXObject("Microsoft.XMLHTTP"); 
	else 
	{ // XMLHttpRequest non supporté par le navigateur 
	   document.getElementById("guide_contenu").innerHTML =  'Votre navigateur ne supporte pas les objets XMLHTTPRequest.<br>Nous vous conseillons de le mettre à jour.'; 
	   return; 
	}
	
 	xhr.open("GET", url + '&guideDisplay=1', true);
	xhr.onreadystatechange  = function () {
		if (xhr.readyState == 4)
			if (xhr.status == 200)
				document.getElementById("guide_contenu").innerHTML = xhr.responseText;
	}
	
	xhr.send(null);
	
	if (document.getElementById("bulle"))
		document.getElementById("bulle").style.visibility = "hidden";
}

function iframeDisplay (url, guide_title) {
	var w = parseInt(document.getElementById("cadre_map").style.width); 
	var h = parseInt(document.getElementById("cadre_map").style.height) - 22;
	
	var taburl = url.split(/\//);
	var reconstruct_url = '';
	for (i=0;i<taburl.length;i++)
	{
		if (taburl[i] == "guide_maporientation" || taburl[i] == "formations")
			var title = i + 1;
		else if (taburl[i] == "communaute" || taburl[i] == "article")
			var title = i + 3;
		else if (!title)
		{
			if (taburl[i] == "http:")
				continue;
			if (i > 0)
				reconstruct_url = reconstruct_url+"/";
			reconstruct_url = reconstruct_url + taburl[i];
		}
	}
	// IE et Firefox ne gère pas la chose de la même manière...
	reconstruct_url = ((reconstruct_url != "" && reconstruct_url.substr(0, 1) == "/" && reconstruct_url.substr(1, 1) != "/") ? "http:/" : "http:") + reconstruct_url;
	
	reconstruct_url = reconstruct_url.split('/fr')[0];

	if (guide_title != null && guide_title.length > 0)
		document.getElementById("div_guide_entete").innerHTML = '<embed id="guide_entete" TYPE="image/svg+xml" src="' + reconstruct_url + '/includes/maporientation/arbre/guideEnTeteSvg_css.php?height_svg=22&width_svg='+w+'&guide_title='+guide_title+'" />';
	else
		document.getElementById("div_guide_entete").innerHTML = '<embed id="guide_entete" TYPE="image/svg+xml" src="' + reconstruct_url + '/includes/maporientation/arbre/guideEnTeteSvg_css.php?height_svg=22&width_svg='+w+'" />';
	
	document.getElementById("arbre_embed").style.height = 0;
	document.getElementById("guide_contenu").innerHTML = '<iframe src="'+url+'&guideDisplay=false" frameborder="0" name="guide_iframe" id="guide_iframe" width=100% height=100%></iframe>';
	document.getElementById("guide_div").style.display = 'block';
}


function guideHide () {
  if (document.getElementById("situation"))
    document.getElementById("situation").style.display = 'block';

	document.getElementById("div_guide_entete").innerHTML = '';
	document.getElementById("guide_contenu").innerHTML = '';
	document.getElementById("guide_div").style.display = 'none';
	if (document.getElementById("dossiers"))
    loadEntites("");
	document.getElementById("arbre_embed").style.height = document.getElementById("cadre_map").style.height;
}

function goToIti (userId,userName, userId) {
  guideHide();
	var xhr_obj = null;
	if(window.XMLHttpRequest)  //Firefox
	xhr_obj = new XMLHttpRequest();
	else if(window.ActiveXObject) // Internet Explorer
	xhr_obj = new ActiveXObject("Microsoft.XMHLHTTP");
	else {
		alert("Votre navigateur ne supporte pas les objets XMLHttpRequest");
		return;
	}
	var url ='http://serveur/work/vianney/maporientation/includes/maporientation/arbre/firefox_browserEval.php';
	
	var param = 'go_to_iti='+userId+'&userName='+userName+'&userId='+userId;
	xhr_obj.open("POST", url, true);  // Mode synchone
	xhr_obj.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xhr_obj.send(param);
	
}

var counterHide;
var counterShow;

function dispGche (div1, div2, diff, sens) {
	var cur1 = parseInt(document.getElementById (div1).style.height);
	var cur2 = parseInt(document.getElementById (div2).style.height);

	if (sens == 'show') {
		cur1 = cur1 + 20;
		cur2 = cur2 - 20;
		
		if  (cur1 >= diff) {
			var temp = diff - cur1;
			document.getElementById (div1).style.height = diff;
			document.getElementById (div2).style.height = cur2 - temp;
			clearInterval(counterShow);
		}
		else {
			document.getElementById (div2).style.height = cur2;
			document.getElementById (div1).style.height = cur1;
		}
	}

	if (sens == 'hide') {
		cur1 = cur1 - 20;
		cur2 = cur2 + 20;
		
		if  (cur1 <= diff) {
			var temp = cur1 - diff;
			document.getElementById (div1).style.height = diff;
			document.getElementById (div2).style.height = cur2 + temp;
			clearInterval(counterHide);
		}
		else {
			document.getElementById (div1).style.height = cur1;
			document.getElementById (div2).style.height = cur2;
		}
	}
}

function showGche (div1, div2, diff) {
	clearInterval(counterShow);
	clearInterval(counterHide);
	counterShow = window.setInterval ("dispGche('"+div1+"','"+div2+"',"+diff+", 'show')", 50);
}

function hideGche (div1, div2, diff) {
	clearInterval(counterHide);
	clearInterval(counterShow);
	counterHide = window.setInterval ("dispGche('"+div1+"','"+div2+"',"+diff+", 'hide')", 50);
}

/* javascripts cvmap*/
function switch_bar(id_bar)
{
	if(id_bar=='affiche_haut')
		id_bar2='affiche_bas'
	else if (id_bar=='affiche_bas')
		id_bar2='affiche_haut'
	else if (id_bar=='affiche_haut2')
		id_bar2='affiche_bas2'	
	else if (id_bar=='affiche_bas2')
		id_bar2='affiche_haut2'
	document.getElementById(id_bar).id = id_bar2
}
function switch_bar_over(id_bar,etat,media_path)
{
	if(etat=='over')
	{
		if(id_bar=='affiche_haut')
			document.getElementById(id_bar).style.background='url('+ media_path +'/cvmap-barre-haut-over.gif) center'
		else if (id_bar=='affiche_bas')
			document.getElementById(id_bar).style.background='url('+ media_path +'/cvmap-barre-bas-over.gif) center'
		else if (id_bar=='affiche_haut2')
			document.getElementById(id_bar).style.background='url('+ media_path +'/cvmap-barre-haut-over.gif) center'
		else if (id_bar=='affiche_bas2')
			document.getElementById(id_bar).style.background='url('+ media_path +'/cvmap-barre-bas-over.gif) center'
	}
	else if(etat=='out')
	{
		if(id_bar=='affiche_haut')
			document.getElementById(id_bar).style.background='url('+ media_path +'/cvmap-barre-haut.gif) center'
		else if (id_bar=='affiche_bas')
			document.getElementById(id_bar).style.background='url('+ media_path +'/cvmap-barre-bas.gif) center'
		else if (id_bar=='affiche_haut2')
			document.getElementById(id_bar).style.background='url('+ media_path +'/cvmap-barre-haut.gif) center'
		else if (id_bar=='affiche_bas2')
			document.getElementById(id_bar).style.background='url('+ media_path +'/cvmap-barre-bas.gif) center'
	}
}
