//Déclaration de la variable contenant la réponse
var xmlDocument;
var safari=false;

//using Safari ? Mind if we lol ?
if (navigator.userAgent.match("(S|s)afari") || navigator.userAgent.match("(O|o)pera")) {
	safari=true;
}
//Firefox
if(document.implementation.createDocument) {
	xmlDocument = document.implementation.createDocument('', '', null);
	//listener sur le chargement du fichier XML
	xmlDocument.addEventListener('load', loadHandler, false);
}
//IE
else {
	//création d'un objet ActiveX
	xmlDocument = new ActiveXObject('Microsoft.XMLDOM');
}

//Fonction pour Firefox - elle est appelée quand le fichier XML est chargé
function loadHandler () {
	xmlProcessor(this);
	xmlProcessor2(this);
}

function loadXMLDoc(url) {
	xmlDocument.async = "false";
	//Utile seulement pour IE
	xmlDocument.onreadystatechange=state_Change;
	if (!safari) {
		xmlDocument.load(url);
	}
	else {
		if (window.XMLHttpRequest)
		{
			xmlhttpXMLCatalogue = new XMLHttpRequest();
			xmlhttpXMLCatalogue.onreadystatechange = process_Safari;
			xmlhttpXMLCatalogue.open("GET", url + "?randomvar=" + Math.random(),true); 
			xmlhttpXMLCatalogue.send(null);
		}
	}
}

function process_Safari() {
	if (xmlhttpXMLCatalogue.readyState == 4)
	{
		if (xmlhttpXMLCatalogue.status != 200) 
		{
			alert("Il y a eu un problème lors de la connexion:\n"+xmlhttpXMLCatalogue.statusText);
		}
		else {
			var content = xmlhttpXMLCatalogue.responseText;
			var parser = new DOMParser();
			xmlDocument = parser.parseFromString(content, "text/xml"); 
			xmlProcessor(xmlDocument);
			xmlProcessor2(xmlDocument);
		}
	}
}

function state_Change() {
	//readyState == 4 seulement si le fichier est lu correctement (pas d'erreurs dans le XML)
	if (xmlDocument.readyState==4) {
		xmlProcessor(xmlDocument);
		xmlProcessor2(xmlDocument);	
	}
}

/*------------------------------------------------------------*/
/*    fonctions personalisées
/*------------------------------------------------------------*/


var tempsNav =150;
var timeOut;
var cpt_rayon = 1;
function aff(nomdiv){
	stopAffiche();
	for (var i = 1; i<=cpt_rayon; i++){
		var id2 = "navGauche"+i;
		if(nomdiv != id2){
			if (document.getElementById(id2)){
				document.getElementById(id2).style.display='none';
			}
		}
	}
	if (document.getElementById(nomdiv)){
		document.getElementById(nomdiv).style.display='block';
	}
}

function masq(){
	for (var i = 1; i<=cpt_rayon; i++){
		if (document.getElementById('navGauche'+i)){
			document.getElementById('navGauche'+i).style.display='none';
			
		}
		
	}
}

function projectionBlock(){
	timeOut = setTimeout('masq()', tempsNav);
}
function stopAffiche(){
	if(timeOut)
		clearTimeout(timeOut);
}


/*************************/

var tempsNav2 =150;
var timeOut2;
var cpt_rayon2 = 1;
function aff2(nomdiv){
	
	stopAffiche2();
	
	for (var i = 1; i<=cpt_rayon2; i++){
		var id2 = "navGauche2-"+i;
		if(nomdiv != id2){
			if (document.getElementById(id2)){
				document.getElementById(id2).style.display='none';
			}
		}
	}
	if (document.getElementById(nomdiv)){
		document.getElementById(nomdiv).style.display='block';
	}
}

function masq2(){
	for (var i = 1; i<=cpt_rayon; i++){
		if (document.getElementById('navGauche2-'+i)){
			document.getElementById('navGauche2-'+i).style.display='none';
			
		}
		
	}
}

function projectionBlock2(){
	timeOut2 = setTimeout('masq2()', tempsNav2);
}
function stopAffiche2(){
	if(timeOut2)
		clearTimeout(timeOut2);
}


/*------------------------------------------------------------*/
/*    FIN fonctions personalisées
/*------------------------------------------------------------*/

//On appelle la fonction avec la réponse XML en paramètre
function xmlProcessor2(catalogue3) {
	//on démarre notre nav perso
	
	var contenuNav = "<div id=\"cadreMenu\" onmouseover=\"aff('navGauche"+cpt_rayon+"');\" onmouseout=\"projectionBlock();\"><img src=\"../site/img/ouvrage-public.gif\">\n";
	contenuNav += "<div style=\"display:none\" id=\"navGauche" + cpt_rayon + "\" class=\"cadreMenuRayon\">\n";	
	//liste des rayons
	rayons3 = catalogue3.documentElement.childNodes;
	//on boucle sur les nodes

	for (var i = 0; i < rayons3.length; i++) {
		

		
		//pour firefox : verifi si le noeux n'est pas vide ( saut de ligne du xml )
		if ( rayons3.item(i).nodeType == 1  ) {

	
			//pour s'assurer qu'on est bien sur une node rayon
			var numrayons = rayons3.item(i).getAttribute("num");
			
			if (numrayons != 241 && numrayons != 251 && numrayons != 261 && numrayons != 271 && numrayons != 281 && numrayons != 291 && numrayons != 301 && numrayons != 311) {
				
				if ( rayons3.item(i).tagName == "rayon" ) {
				//formatage du rayon
			
				
						//on récupère les attributs du rayon qu'on stock dans une variable à afficher
						contenuNav += "<a href=\""+ rayons3.item(i).getAttribute("url") +"" + rayons3.item(i).getAttribute("url") + "\" class=\"lienRayonXml\" style=\"color:#fff;\">" + rayons3.item(i).getAttribute("nom") + "</a>\n";
						cpt_rayon++;	
					
				}
				
				//formatage fin de cellule rayon
				//contenuNav += "\n";
				else{
				}
				
			}
		}
	}
	contenuNav += "</div></div>\n";
	//on affiche notre variable de nav
	document.getElementById("menu").innerHTML = contenuNav;
}


function xmlProcessor(catalogue) {
	//on démarre notre nav perso
	var contenuNav2 = "<ul id=\"cadreMenuD\" style=\"width:660px;\">\n";
	//liste des rayons
	rayons = catalogue.documentElement.childNodes;
	//on boucle sur les nodes
	
	for (var i = 0; i < rayons.length; i++) {
		
		//pour firefox : verifi si le noeux n'est pas vide ( saut de ligne du xml )
		if ( rayons.item(i).nodeType == 1  ) {
			//contenuNav2 += "<div class=\"cadreLienRayon\">\n";
				
			//pour s'assurer qu'on est bien sur une node rayon
			var numrayons = rayons.item(i).getAttribute("num");
			if ( numrayons != 361 && numrayons != 321 && numrayons != 331 && numrayons != 341 && numrayons != 351 && numrayons != 401 && numrayons != 391 && numrayons != 381  && numrayons != 411 && numrayons != 311 && numrayons != 291) {
	
				if ( rayons.item(i).tagName == "rayon" ) {
				//formatage du rayon
			
				contenuNav2 += "<li onmouseover=\"aff2('navGauche2-"+cpt_rayon2+"');\" onmouseout=\"projectionBlock2();\" style=\"position:relative;\">\n";
				contenuNav2 +="<a href=\"" + rayons.item(i).getAttribute("url") + "\" class=\"lienRayon2\">" + rayons.item(i).getAttribute("nom") + "</a>\n";			
			
				//liste des gammes de ce rayon
				gammes = rayons.item(i).childNodes;
			
					// si le rayon a des gammes le clic rayon affiche les gammes - sinon on garde le lien rayon
					if ( gammes.length != 0 ) {
							
						contenuNav2 += "<div class=\"cadreMenuGamme2\" style=\"display:none\" id=\"navGauche2-" + cpt_rayon2 + "\">\n";
			
						//on boucle sur les gammes
						for (var j = 0; j < gammes.length; j++) {
							//pour s'assurer qu'on est bien sur une node gamme
							if (gammes.item(j).tagName == "gamme") {
								//formatage de la gamme
								//on récupère les attributs du rayon qu'on stock dans une variable à afficher
								contenuNav2 += "<a href=\"" + gammes.item(j).getAttribute("url") + "\" class=\"lienGamme\">" + gammes.item(j).getAttribute("nom") + "</a>\n";
								
					
							}
						}
						//formatage fin du cadre gammes
						contenuNav2 += "</div></li>\n";	
						cpt_rayon2++;
						
					} 
					else {
					}
				}
				
			}
		}
	}
	contenuNav2 += "</ul>\n";
	//on affiche notre variable de nav
	document.getElementById("menuD").innerHTML = contenuNav2;
	
	//xmlProcessor2(xmlDocument);
}
	
//on charge le fichier catalogue.xml qui se situe dans le répertoire boutique (et boutique_us)
loadXMLDoc("../boutique/architecture_catalogue.xml");



