/*
	Sous menu dynamique en JS
	15/02/2007 Guillaume MARTIN
*/


var IE = document.all?true:false

if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouseXY;

var tempX = 0;
var tempY = 0;

var m_TimeOut_Disparition = null;

function getMouseXY(e) {
  if (IE) {
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {
    tempX = e.pageX
    tempY = e.pageY
  }  
  
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  
  return true
}


var m_max_profondeur = 5;
var m_couleur_rub = "";
var m_couleur_sep = "A6C4DB";
var debug = "0";

var m_rub_p = new Array();
var m_rub_id = new Array();
var m_rub_nom = new Array();

var m_gr_p = new Array();
var m_gr_id = new Array();
var m_gr_nom = new Array();

function m_add_rubriques(id_p, id_r, nom_r) {
	var index_r = m_rub_p.length;
	m_rub_p[index_r]		= id_p;
	m_rub_id[index_r]		= id_r;
	m_rub_nom[index_r]	= nom_r;
}

function m_add_groupes(id_p, id_g, nom_g) {
	var index_r = m_gr_p.length;
	m_gr_p[index_r]			= id_p;
	m_gr_id[index_r]		= id_g;
	m_gr_nom[index_r]		= nom_g;
}


var XmlHttp = false;
if(window.XMLHttpRequest) {
	try {
		XmlHttp = new XMLHttpRequest();
  } catch(e) {
		XmlHttp = false;
  }			
  
} else if(window.ActiveXObject) {
 	try {
  	XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch(e) {
  	try {
    		XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  	} catch(e) {
    		XmlHttp = false;
  	}
	}
}
	

	function Get_XMLValues() {
					
			var query_http = "xml_groupes.php?recup=1";
			XmlHttp.open("GET",query_http,true);
			XmlHttp.onreadystatechange = OnState_Get_XMLValues;
			XmlHttp.send(null);
			
			
		return true;
	}
	
	function OnState_Get_XMLValues() {
					
			if (XmlHttp.readyState == 4) {
				if (debug == "1") {
					alert("Resultat obtenu !");
				}
				var _nom = null;
				var _parent = null;
				var _id = null;
				var xml_response = XmlHttp.responseXML;
				var erreur = xml_response.getElementsByTagName("erreur").item(0).firstChild.data;
				var resultats = xml_response.getElementsByTagName("groupe");
				if (erreur == 0 && resultats.length > 0) {
					for (i=0; i < resultats.length; i++) {
						_nom = resultats.item(i).getElementsByTagName("nom").item(0).firstChild.data;
						_parent = resultats.item(i).getElementsByTagName("parent").item(0).firstChild.data;
						_id = resultats.item(i).getElementsByTagName("id").item(0).firstChild.data;
						
						m_add_groupes(_parent,_id,_nom);
						
					}
					
				}
				resultats = xml_response.getElementsByTagName("rubrique");
				if (erreur == 0 && resultats.length > 0) {
					for (i=0; i < resultats.length; i++) {
						if (resultats.item(i).getElementsByTagName("nom").item(0).firstChild) {
							_nom = resultats.item(i).getElementsByTagName("nom").item(0).firstChild.data;
							_parent = resultats.item(i).getElementsByTagName("parent").item(0).firstChild.data;
							_id = resultats.item(i).getElementsByTagName("id").item(0).firstChild.data;
							
							m_add_rubriques(_parent,_id,_nom);
						}
					}
					
				}
			}
			
	}
	

function m_set_content(id_p, profondeur) {
	var i = 0;
	var content_in = "";
	
	
	for (i = 0; i < m_rub_p.length; i++) {
		if (m_rub_p[i] == id_p) {
			// Ajoute au content !
			content_in +=  "<tr><td class='arial11'><b><a href='" + "rubrique.php?id=" + m_rub_id[i] + "' style='color:" + m_couleur_rub + "' onMouseOut='m_enclenche_timer()' onMouseOver='m_affiche(\"" + m_rub_id[i] + "\",\"" + (parseInt(profondeur) + 1) + "\");'>" + m_rub_nom[i] + "</a></b></td></tr>\n";
			content_in += "<tr><td class='arial11' bgcolor='" + m_couleur_sep + "'><img src='interfpicts/pixel.gif' alt='' width='124' height='1' border='0'></td></tr>\n";
		}
	}
	if (content_in != "") {
		content = "<table cellspacing='1' cellpadding='0'>";
		content += "<tr><td class='arial11' bgcolor='" + m_couleur_sep + "'><img src='interfpicts/pixel.gif' alt='' width='124' height='1' border='0'></td></tr>\n";
		content +=  "<tr><td class='arial11'><b><a href='" + "rubrique.php?id=" + id_p + "' style='color:" + m_couleur_rub + "'>Tous les produits</a></b></td></tr>\n";
		content += "<tr><td class='arial11' bgcolor='" + m_couleur_sep + "'><img src='interfpicts/pixel.gif' alt='' width='124' height='1' border='0'></td></tr>\n";
		content += content_in;
		content += "</table>";
		return content;
	}
	return "";
}

function m_affiche(id_r,profondeur) {
	var div_rubriques = document.getElementById("m_rubriques_" + profondeur);
	if (!div_rubriques) {
		alert("Erreur Rub " + profondeur);
	}
	m_declenche_timer();
	
	for (i = profondeur; i < m_max_profondeur; i++) {
		var div_tmp_rub = document.getElementById("m_rubriques_" + i);
		if (div_tmp_rub) {
				div_tmp_rub.style.visibility = "hidden";
		}
	}
	
	
	content = m_set_content(id_r,profondeur);
	if (content == "") {
		return;
	}
	
	new_prof = parseInt(profondeur) + 1;
	
	div_rubriques.style.zIndex = new_prof * 10;
	div_rubriques.style.left = eval(((new_prof - 1) * 130) + 140) + 'px';
	
	div_rubriques.style.top = ( tempY - 7 ) + 'px';
	div_rubriques.style.width = "150px";
	div_rubriques.style.backgroundColor = "#000000";
	div_rubriques.style.border = "1px dotted #A6C4DB";
	
	div_rubriques.innerHTML = content;
	
	div_rubriques.style.visibility = "visible";
	
}

function m_set_content_groupe(id_p, profondeur) {
	var i = 0;
	var content_in = "";
	var lien = "";
	
	
	for (i = 0; i < m_gr_p.length; i++) {
		if (m_gr_p[i] == id_p) {
			// Ajoute au content !
			// if (parseInt(m_gr_id[i]) == m_gr_id[i]) {
				lien = "groupe.php?id=" + m_gr_id[i];
			// } else {
			// 	lien = "touslesgroupes.php";
			// }
			content_in +=  "<tr><td class='arial11'><b><a href='" + lien + "' style='color:" + m_couleur_rub + "' onMouseOut='m_enclenche_timer()' onMouseOver='m_affiche_groupe(\"" + m_gr_id[i] + "\",\"" + (parseInt(profondeur) + 1) + "\");'>" + m_gr_nom[i] + "</a></b></td></tr>\n";
			content_in += "<tr><td class='arial11' bgcolor='" + m_couleur_sep + "'><img src='interfpicts/pixel.gif' alt='' width='124' height='1' border='0'></td></tr>\n";
		}
	}
	if (content_in != "") {
		content = "<table cellspacing='1' cellpadding='0'>";
		content += "<tr><td class='arial11' bgcolor='" + m_couleur_sep + "'><img src='interfpicts/pixel.gif' alt='' width='124' height='1' border='0'></td></tr>\n";
		content += content_in;
		content += "</table>";
		return content;
	}
	return "";
}

function m_affiche_groupe(id_r,profondeur) {
	var div_rubriques = document.getElementById("m_rubriques_" + profondeur);
	if (!div_rubriques) {
		alert("Erreur Rub " + profondeur);
	}
	m_declenche_timer();
	
	for (i = profondeur; i < m_max_profondeur; i++) {
		var div_tmp_rub = document.getElementById("m_rubriques_" + i);
		if (div_tmp_rub) {
				div_tmp_rub.style.visibility = "hidden";
		}
	}
	
	
	content = m_set_content_groupe(id_r,profondeur);
	if (content == "") {
		return;
	}
	
	new_prof = parseInt(profondeur) + 1;
	
	div_rubriques.style.zIndex = new_prof * 10;
	div_rubriques.style.left = eval(((new_prof - 1) * 130) + 140) + 'px';
	
	div_rubriques.style.top = ( tempY - 7 ) + 'px';
	div_rubriques.style.width = "150px";
	div_rubriques.style.backgroundColor = "#000000";
	div_rubriques.style.border = "1px dotted #A6C4DB";
	
	div_rubriques.innerHTML = content;
	
	div_rubriques.style.visibility = "visible";
	
}


function m_del_rubriques () {
	
	m_declenche_timer();
	
	for (i = 0; i < m_max_profondeur; i++) {
		var div_tmp_rub = document.getElementById("m_rubriques_" + i);
		if (div_tmp_rub) {
				div_tmp_rub.style.visibility = "hidden";
		}
	}
	
}


function m_enclenche_timer() {
	if (m_TimeOut_Disparition) {
		clearTimeout(m_TimeOut_Disparition);
		m_TimeOut_Disparition = null;
	}
	
	m_TimeOut_Disparition = setTimeout("m_del_rubriques()",2000);
	
}

function m_declenche_timer() {
	if (m_TimeOut_Disparition) {
		clearTimeout(m_TimeOut_Disparition);
		m_TimeOut_Disparition = null;
	}
}


function changeProd(ligne,colonne,clef,tmout) {
	/* Changement de produit */
	Pdiv_old = document.getElementById("prod_" + ligne + "_" + colonne + "_" + clef);
	if (!Pdiv_old) {
		return false;
	}
	clef = parseInt(clef) + 1;
	Pdiv = document.getElementById("prod_" + ligne + "_" + colonne + "_" + clef);
	if (!Pdiv) {
		clef = "0";
		Pdiv = document.getElementById("prod_" + ligne + "_" + colonne + "_" + clef);
		if (!Pdiv) {
			return false;
		}
	}
	Pdiv_old.style.display = 'none';
	Pdiv.style.display = 'block';
	
	setTimeout("changeProd('" + ligne + "','" + colonne + "','" + clef + "', "+ tmout + ");",tmout);
	
	return true;
}

extraits_win = null;
function extraits (ref) {
	
	extraits_win = window.open("./extraitsmp3/popup.php?ref=" + escape(ref),"goeland_extraits","menubar=no, status=no, scrollbars=no, menubar=no, width=550, height=40");
	extraits_win.focus();
}

function close_concert() {
	dv_concert = document.getElementById("div_concert");
	if (!dv_concert) return;
	
	dv_concert.style.display = 'none';
	
	
}

extra_pic = null;
function view_pic (img_dir) {	
	extra_pic = window.open("./popup_extrapic.php?img=" + escape(img_dir),"goeland_extrapic","menubar=no, status=no, scrollbars=yes, menubar=no, width=300, height=300");
	extra_pic.focus();
}

setTimeout("close_concert();",10000);


function switch_divs(_d1, _d2) {	
	
	_div1 = document.getElementById(_d1);
	_div2 = document.getElementById(_d2);
	
	if (!_div1 || !_div2) {
		return false;
	}
	
	
	_div1.style.display = 'none';
	_div2.style.display = 'block';
		
}

var PanierXmlHttp = false;

function panier_newref(_ref) {

	
	

	if (!_ref || _ref.value.length <= 2) {
		return false;
	}
	
	
	if (!PanierXmlHttp) {
	
				if(window.XMLHttpRequest) {
					try {
						PanierXmlHttp = new XMLHttpRequest();
				  } catch(e) {
						PanierXmlHttp = false;
				  }			
				  
				} else if(window.ActiveXObject) {
				 	try {
				  	PanierXmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
					} catch(e) {
				  	try {
				    		PanierXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
				  	} catch(e) {
				    		PanierXmlHttp = false;
				  	}
					}
				}
	
	}
	
	if (!PanierXmlHttp) {
		alert("Votre navigateur n'est pas à jour");
		return false;
	}
	
	var request = "votrepanier_ajax.php?ref=" + _ref.value + "&RAND=" + escape(Math.floor(Math.random()*1000000)) ;
	
	PanierXmlHttp.open("GET",request,false);
	PanierXmlHttp.send(null);
	
			
					
	if (PanierXmlHttp.readyState == 4) {
		
		var xml_response = PanierXmlHttp.responseXML;
		var erreur = xml_response.getElementsByTagName("erreur").item(0).firstChild.data;
		if (erreur != 1) {
			
			var _div_nom = document.getElementById('newref_nom');
			var _div_taille = document.getElementById('newref_taille');
			var _div_prix = document.getElementById('newref_prix');
			var _xml_nom = xml_response.getElementsByTagName("nom").item(0).firstChild.data;
			var _xml_prix = xml_response.getElementsByTagName("prix").item(0).firstChild.data;
			var _xml_stock = xml_response.getElementsByTagName("stockT").item(0).firstChild.data;
			var _tailles = xml_response.getElementsByTagName("taille");
			
			// alert(_xml_nom);
			
			if (_div_nom) {
				_div_nom.innerHTML = _xml_nom;
			}
			_div_prix.innerHTML = "";
			
			if (_div_taille) {
				
				
				if (_xml_stock <= 0) {
					_div_taille.innerHTML = "Produit épuisé";
				} else {
				
					if (_tailles.length > 1) {
							var tnom;
							var tstock;
							var ttaille;
							
							var content = "";
							
							content+= "<option value=''>Choisissez</option>";
							var i = 0;
							for (i = 0; i < _tailles.length; i++) {
								tnom = _tailles.item(i).getElementsByTagName("tnom").item(0).firstChild.data;
								tstock = _tailles.item(i).getElementsByTagName("stock").item(0).firstChild.data;
								ttaille = _tailles.item(i).getElementsByTagName("back").item(0).firstChild.data;
								
								
								content+= "<option value='" + ttaille +  "'";
								if (tstock <= 0) {
									content += " disabled='disabled'";
								}
								content+= ">" + tnom;
								if (tstock <= 0) {
									content += " épuisé";
								}
								content+= "</option>";
								
								
							}
							
							_div_taille.innerHTML = "<select id='newref_taille_field'>" + content + "</select>";
							
					} else {
						_div_taille.innerHTML = "<input type='hidden' id='newref_taille_field' value='-' />";
						_div_taille.innerHTML+= "Taille unique";
					}
					
					
					_div_prix.innerHTML = "<div style='float: left; width: 100px; text-align:left;'><a href='#' onclick='return valid_newRef();'>Enregistrer</a></div>";
					
					
				}
				
			}
			
				
			if (_div_prix) {
				_div_prix.innerHTML+= _xml_prix;
			}
				
		} else {
			alert("Produit inconnu");
		}
		
	} else {
		alert("Vérifiez votre connexion internet.");
		return false;
	}
	
}

function valid_newRef() {
	
	var field_ref = document.getElementById('newref_ref_field').value;
	var field_qt = document.getElementById('newref_qt_field').value;
	var field_taille = document.getElementById('newref_taille_field').value;
	
	if (field_taille.length <= 0) {
		alert('Choisissez une taille');
		return false;
	}
	
	// alert (field_ref + ' / ' + field_qt + ' / ' + field_taille);
	document.location.href="produit.php?ajoutpanier=1&ref=" + escape(field_ref) + "&quantite=" + escape(field_qt) + "&taille=" + escape(field_taille);
	
	return false;
}

function newref_noreturn(e) {
	var keynum;
	
	if(window.event) // IE
	  {
	  keynum = e.keyCode;
	  }
	else if(e.which) // Netscape/Firefox/Opera
	  {
	  keynum = e.which;
	  }
	
	
	if (keynum == 13) {
		return false;
	}
	
	return true;
}


function mos_affiche (id) {
	_div = document.getElementById('new_produits_' + id);
	_ct = document.getElementById('mos_content');
	
	_ct.innerHTML = _div.innerHTML;
	
	
	_ct.style.top = (tempY - 7) + 'px';

	_ct.style.right = '160px';
	_ct.style.width = "200px";

	_ct.className	= "tumevois";
	
}

function mos_cache (id) {
	_ct = document.getElementById('mos_content');
	
	_ct.className	= "tumevoispas";
}

