
var mouse_x = 0;
var mouse_y = 0;
var mouse_lb = false;

var obj_current = null;
var ulBlinding = null;

var IE = (navigator.appName == "Microsoft Internet Explorer") ? true : false; //block toutes animation si IE

var clientHeight = 0;
var clientWidth = 0;

function getXHR() 
{
	var xhr = null;
	
	if (window.XMLHttpRequest)
	{
		xhr = new XMLHttpRequest();
	}	
	else if (window.ActiveXObject) 
	{
		try { xhr = new ActiveXObject("Msxml2.XMLHTTP");}
		catch (e) { xhr = new ActiveXObject("Microsoft.XMLHTTP"); }
	}
	else 
	{
		alert("Navigateur ne supportant pas les objets XMLHttpRequest");
	}
	return xhr;
}

function mouseMove(e) 
{
		//position de la souris
		if (navigator.appName!="Microsoft Internet Explorer") 
		{ // Si on est pas sous IE
			mouse_x = e.pageX;
			mouse_y = e.pageY;
		}
		else { // Modif proposé par TeDeum, merci à lui
			if(document.documentElement.clientWidth>0) 
			{
				mouse_x = event.x + document.documentElement.scrollLeft;
				mouse_y = event.y + document.documentElement.scrollTop;
			} else 
			{
				mouse_x = event.x + document.body.scrollLeft;
				mouse_y = event.y + document.body.scrollTop;
			}
		}
}
function mouseUp(e)
{
	mouse_lb = false;
}
function mouseDown(e)
{
	mouse_lb = true;
}

document.onmousemove	= mouseMove;
document.onmouseup		= mouseUp;
document.onmousedown	= mouseDown;

function debug(txt)
{
	$("debug").innerHTML += txt+"<br />";
}

function getClientRect()
{
	clientHeight = (window.innerHeight!=undefined) ? window.innerHeight : document.documentElement.offsetHeight;
	clientWidth = (window.innerWidth!=undefined) ? window.innerWidth : document.documentElement.offsetWidth;
}


function getParent(element, parentTagName) 
{
	if ( ! element )
		return null;
	else if ( element.nodeType == 1 && element.tagName.toLowerCase() == parentTagName.toLowerCase() )
		return element;
	else
		return getParent(element.parentNode, parentTagName);
}
function envoiMail(formulaire){
	//verif nom
	if(formulaire.elements["Nom"].value==""){
		alert("saisissez votre nom");
		return false;
	}
	//verif email
	mail = formulaire.elements["Email"].value;
	if (!((mail.indexOf("@")>=0)&&(mail.indexOf(".")>=0))) {
		 alert("Mail invalide !");
		 return false;
	}
	//verif objet
	if(formulaire.elements["Objet"].value==""){
		alert("entrez un objet");
		return false;
	}
	//verif message
	if(formulaire.elements["Message"].value==""){
		alert("message vide !");
		return false;
	}
	
	//verif code
	if(formulaire.elements["code"].value==""){
		alert("Veuillez saisir le code anti spam, merci !");
		return false;
	}
	
	formulaire.submit();
	alert("Message envoye");
	//return true;
}
function pop_it(url, nom, w, h) {
	fen = window.open(url, nom, "height="+h+",width="+w+",left="+(screen.availWidth/2-(w/2))+",top="+(screen.availHeight/2-(h/2))+",menubar='no',toolbar='no',location='no',status='no'");
	if(nom!="fen")return fen;
}

function sql2array(tableSQL, parent)
{
	myArray = new Array();
	// recuperations des donnees depuis sql
	new Ajax.Request('scripts/sql2array.php',
	{
		method:'get',
		parameters: 'tableSQL='+tableSQL+"&parent="+parent,
		onSuccess: function(ok){
			var response = ok.responseText || "no response text";
			//alert(response);
			// decoupe les entrees
			var myArray_temp = response.split('-'+tableSQL+'-');
			
			for(i=0; i<myArray_temp.length; i++)
			{
				// decoupe les params
				params = myArray_temp[i].split('-param-');
				
				id = 0;
				for(j=0; j<params.length; j++)
				{
					// decoupe -> index value
					param = params[j].split('#:#');
					if(param[0])
					{
						index = param[0];
						value = param[1];
						
						// considere que le premier index (0) est id
						if(j==0)
						{
							id = value;
							myArray[id] = new Array();
							// myArray[id] = new objet(tableSQL+"_"+id);
						}
						
						myArray[id][index] = value;
						// debug(index+" : "+value);
					}
				}
			}
			chargement = true;
			
			//alert(myArray.inspect());		
			return myArray;
			
		},
		onFailure: function(){ alert('Probleme ajax...') }
	});
	
	return myArray;
	
}


function array2ul(myArray, myUl)
{
	manque = false;
	for(objId in myArray)
	{
		if(myArray[objId].id)
		{
			var obj = myArray[objId];
			//sans parent
			if(!obj.parent || obj.parent == 0)
			{	
				if(!$('li_'+objId))myUl.appendChild(createLi(obj));
			}
			else // avec parent
			{
				var objUl = $('ul_'+obj.parent);
				if(objUl)// hack IE
				{
					if(!$('li_'+objId))objUl.appendChild(createLi(obj));
					$('ul_'+objId).style.display = 'none';
				}
				else
				{
					manque = true; // hack IE
				}
			}
		}
	}
	
	if(manque)array2ul(myArray, myUl);// hack IE
	
}


function createLi(obj)
{
	if(!obj.id) return;
		
	var new_li = document.createElement("li");
	new_li.id = "li_"+obj.id;
	if(obj.parent!=0)
	{
		new_li.onclick = expandLi;
	}
	
	// l'image
	//if(obj.type!="pdt" && obj.image != "")
	{
		var new_img = document.createElement("img");
		new_img.className = "image";
		new_img.id = "image_"+obj.id;
		new_img.src = obj.image;
		new_li.appendChild(new_img);
	}
	
	// la ref
	var new_span = document.createElement("span");
	new_span.className = "ref";
	new_span.id = "ref_"+obj.id;
	new_span.innerHTML = obj.ref==""?"&nbsp;":obj.ref;
	new_span.title = "Référence";
	new_li.appendChild(new_span);
	
	// le nom
	var new_span = document.createElement("span");
	new_span.className = "designation";
	new_span.id = "nom_"+obj.id;
	new_span.innerHTML = obj.designation;
	new_span.title = "Désignation";
	new_li.appendChild(new_span);
	
	// la sous liste
	var new_ul = document.createElement("ul");
	new_ul.id = "ul_"+obj.id;
	new_ul.style.paddingBottom = 4+"px";
	new_ul.style.paddingTop = 2+"px";
	new_li.appendChild(new_ul);
	//debug("add_ul"+obj.id);
	
	return new_li;
}

/***********************
	expand li
***********************/
var blockExpand = false;
var tpsExpand = 400;
var timerExpand = null;


function expandLi(event)
{
	if(blockExpand) return;
	blockExpand = true;
	timerExpand = setInterval("expand_finish()", tpsExpand);
	var id = getNumObj(this.id);
	var obj = produits[id];

	//debug($('ul_'+id).innerHTML);
	if($('ul_'+id).style.display == 'none' && $('ul_'+id).innerHTML!="")
	{	
		Effect.BlindDown('ul_'+id,{duration:tpsExpand/1000});
		ulBlinding = $('ul_'+id);
	}
	else if($('ul_'+id).style.display == 'none')
	{
		//$('ul_'+id).style.display = 'block';
	}
	else if($('ul_'+id).innerHTML!="")
	{
		Effect.BlindUp('ul_'+id,{duration:tpsExpand/1000});
		ulBlinding = $('ul_'+id);
	}
	else
	{
		//$('ul_'+id).style.display = 'none';
	}
	
}


function expandLi2(id)
{
	if(blockExpand) return;
	blockExpand = true;
	timerExpand = setInterval("expand_finish()", tpsExpand);
	var obj = produits[id];
	
	if($('ul_'+id))
	{
		if($('ul_'+id).style.display == "none" && $('ul_'+id).innerHTML!="")
		{	
			Effect.BlindDown('ul_'+id,{duration:tpsExpand/1000});
			ulBlinding = $('ul_'+id);
		}
		else if($('ul_'+id).style.display == 'none')
		{
			//$('ul_'+id).style.display = 'block';
		}
		else if($('ul_'+id).innerHTML!="")
		{
			Effect.BlindUp('ul_'+id,{duration:tpsExpand/1000});
			ulBlinding = $('ul_'+id);
		}
		else
		{
			//$('ul_'+id).style.display = 'none';
		}
	}
}

function expand_finish()
{
	clearInterval(timerExpand);
	blockExpand = false;
	
	if(ulBlinding)ulBlinding.style.marginBottom = "0px";
}

function afficheProduits(parent)
{
	new Ajax.Request('scripts/sql2ul_dyn.php',
	{
		method:'get',
		parameters: 'parent='+parent,
		onSuccess: function(ok){
			var response = ok.responseText || "no response text";
			$("produits").innerHTML = response;
		},
		onFailure: function(){ alert('Probleme ajax...') }
	});


}

/*********************************
	fonctions outils
*********************************/
function getNumObj(id)
{
	id = id.split("_");
	return id[id.length-1];
}

function renderName(str, len)
{
	if(str.length<len)return str;
	
	str = str.substr(0, len-3);
	str += "...";
	return str;
}

function min2hmin_str(min)
{
	if(min==0)return "";
	
	h = Math.floor(min/60);
	m = min-(h*60);
	
	str = h<10?"0"+h+":":h+":";
	str += m<10?"0"+m:m;
	//str += (h==0 && m!=0)?" min":"";
	
	return str;
}

function hightLight(obj)
{
	new Effect.Highlight(obj);
}

/*********************
	cookies
*********************/
function cre_cook0(nom,contenu) 
{
	document.cookie = nom + "=" + escape(contenu)
}

function cre_cook(nom,contenu,jours) 
{
	var expireDate = new Date();
	expireDate.setTime(expireDate.getTime() + jours*24*3600*1000);
	document.cookie = nom + "=" + escape(contenu)
	+ ";expires=" + expireDate.toGMTString();
}

function lit_cook(nom) 
{
	var deb,fin
	deb = document.cookie.indexOf(nom + "=")
	if (deb >= 0) 
	{
		deb += nom.length + 1
		fin = document.cookie.indexOf(";",deb)
		if (fin < 0) fin = document.cookie.length
		return unescape(document.cookie.substring(deb,fin))
	}
	return ""
}

function tue_cook(nom) { cre_cook(nom,"",-1) }

function askCook(nom) 
{
	c=prompt("Mettre dans le cookie :","")
	cre_cook0(nom,c)
}

function litCook(nom) 
{
	c=lit_cook(nom)
	if(c=="") alert("Le cookie est vide !")
	else alert("Le cookie vaut : "+c)
}



function Scroller_GetCoords(){

	if (document.all){
		if (!document.documentElement.scrollLeft)
		scrollX = document.body.scrollLeft;
		else
		scrollX = document.documentElement.scrollLeft;

		if (!document.documentElement.scrollTop)
		scrollY = document.body.scrollTop;
		else
		scrollY = document.documentElement.scrollTop;
	}   
	else{
		scrollX = window.pageXOffset;
		scrollY = window.pageYOffset;
	}  

	alert(scrollY);;
}

//window.onscroll = Scroller_GetCoords;
// $('fond').onscroll = Scroller_GetCoords;














