var isIE6 = false, isIE7 = false,
 isFIREFOX2 = false, isFIREFOX = false,
 isNETSCAPE7 = false, isNETSCAPE = false,
 isOPERA9 = false, isOPERA = false,
 isUNKNOWN = false; 

var strChUserAgent = navigator.userAgent;
 var intSplitStart = strChUserAgent.indexOf("(",0);
 var intSplitEnd = strChUserAgent.indexOf(")",0);
 var strChStart = strChUserAgent.substring(0,intSplitStart);
 var strChMid = strChUserAgent.substring(intSplitStart, intSplitEnd);
 var strChEnd = strChUserAgent.substring(strChEnd);

 if(strChMid.indexOf("MSIE 7") != -1)
 isIE7 = true;
 else if(strChMid.indexOf("MSIE 6") != -1)
 isIE6 = true;
 else if(strChEnd.indexOf("Firefox/2") != -1)
 isFIREFOX2 = true;
 else if(strChEnd.indexOf("Firefox") != -1)
 isFIREFOX = true;
 else if(strChEnd.indexOf("Netscape/7") != -1)
 isNETSCAPE7 = true;
 else if(strChEnd.indexOf("Netscape") != -1)
 isNETSCAPE = true;
 else if(strChStart.indexOf("Opera/9") != -1)
 isOPERA9 = true;
 else if(strChStart.indexOf("Opera") != -1)
 isOPERA = true;
 else
 isUNKNOWN = true;




function getAbsolutePos(obj) {
	var x=0,y=0;
	while (obj!=null){
		x+=obj.offsetLeft-obj.scrollLeft;
		y+=obj.offsetTop-obj.scrollTop;
		obj=obj.offsetParent;
	}
	 return {x:x,y:y};
}

/* ----- Cookie ----- */

function EcrireCookie(nom, valeur) {
	var argv=EcrireCookie.arguments;
	var argc=EcrireCookie.arguments.length;
	var expires=(argc > 2) ? argv[2] : null;
	var path=(argc > 3) ? argv[3] : null;
	var domain=(argc > 4) ? argv[4] : null;
	var secure=(argc > 5) ? argv[5] : false;
	document.cookie=nom+"="+escape(valeur)+
	((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
	((path==null) ? "" : ("; path="+path))+
	((domain==null) ? "" : ("; domain="+domain))+
	((secure==true) ? "; secure" : "");
}

function LireCookie (name) {
	 if ( document.cookie) { // Le cookie est-il valide ?
			  index = document.cookie.indexOf( name);
			  if ( index != -1) {
					   nDeb = (document.cookie.indexOf( "=", index) + 1);
					   nFin = document.cookie.indexOf( ";", index);
					   if (nFin == -1) {nFin = document.cookie.length;}
					   return unescape(document.cookie.substring(nDeb, nFin));
			  }
	 }
	 return null;
}


// ---------------------------------- moteur -------------------------------------------
//affichage d'une infobulle
function moteurInfoBulle() {
	if ($("infobulleMoteur")) {
		$("infobulle").style.display = "none" ;
		$("infobulle").destroy() ;
	}

	
	request = new Ajax.Request("/comparateur/ajax/infobulles/moteur.php?proto=1",
							   {method:"get",
							   onComplete:function (transport) {
								   		responseText = transport.responseText ;
																	
										eval(responseText) ;
									}
								}) ;
	

}


function searchEngineSelect(form) {
	if (form.whereSearch.options[form.whereSearch.selectedIndex].value == "comparateur") {
		form.action = "/comparateur/preparesearch.php" ;
	}
}

function selectSearchToggle() {
	panel = $('searchSelect') ;
	
	if (panel.style.display != "block") {
		panel.style.display = "block" ;
		
		panel.onmouseout = function(e) {
			//on vérifie si on quitte pas pour un élément enfant
			if (!e) var e = window.event;
			var tg = (window.event) ? e.srcElement : e.target;
			if (tg.nodeName != 'DIV') return;
			var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
			while (reltg != tg && reltg.nodeName != 'BODY')
				reltg= reltg.parentNode
			if (reltg== tg) return;

			this.style.display = "none"; 
										   
	    } ;

			
	}
	else {

		panel.style.display = "none" ;
	}
}


function moteurSectionSelect(option) {
	
	//si on touche au champ global
	if (option.name == "all") {		
		if (option.checked) {
			
			l_options = $$('.moteurOption') ;
			
			for(i = 0; i < l_options.length; i++) {
				l_options[i].checked = true ;
				
				if (l_options[i].name != "all") {
					l_options[i].disabled = true ;	
				}
			}
			
		}		
		else {
			l_options = $$('.moteurOption') ;
			
			for(i = 0; i < l_options.length; i++) {
				
				l_options[i].checked = false ;	
				l_options[i].disabled = false ;
			}			
		}		
	}
	
	var expire = new Date();
	var oneYear = expire.getTime() + (365 * 24 * 60 * 60 * 1000);
	expire.setTime(oneYear);


	//sauvegarde des paramètres dans un cookie
	l_options = $$('.moteurOption') ;
	
	for(i = 0; i < l_options.length; i++) {		
		if (l_options[i].name != "all")
			EcrireCookie("moteur_params[" + l_options[i].value + "]",l_options[i].checked,expire, "/") ;					
	}		
}



var qs ;

var infobulleDisplay = true ;

function storeQS(field) {
	
	if (field.value == "Que recherches-tu ?") {	
		//qs = field.value ;
		field.value = "" ;
		
		field.style.fontStyle = "normal" ;
		field.style.fontWeight = "bold" ;
		field.style.color = "#990066" ;
	}
	else {
		field.value = "" ;	
	}
	
	if (infobulleDisplay) {
		moteurInfoBulle() ;
		infobulleDisplay = false ;
	}
}

function autoCompleteQS(field) {
	if (field.value != qs) {	

		if (field.value.length == 0) {			
			field.style.fontStyle = "italic" ;
			field.style.fontWeight = "normal" ;	
			field.style.color = "#BA8FBF" ;	
			field.value = "Que recherches-tu ?" ;
		}
		else if (qs == "Que recherches-tu ?") {
			field.style.fontStyle = "italic" ;
			field.style.fontWeight = "normal" ;	
			field.style.color = "#BA8FBF" ;	
			field.value = "Que recherches-tu ?" ;	
		}
		else {
			field.style.fontStyle = "normal" ;
			field.style.fontWeight = "bold" ;
			field.style.color = "#990066" ;
			//field.value = qs ;
		}
	}
	
	suggestBox = $('suggestions') ;
	
	if (suggestBox.style.display == "block") {		
		suggestBox.style.display = "none" ;		
	}
	
}

function gotoMarchand(id) {
	location.href = "/comparateur/goto.php?id=" + id + "&mode=m" ;	
}


var sq = false ;

function liveSearch(field) {

	if (sq != false) {
		window.clearTimeout(sq) ;
		sq = false ;		
	}	
		
	if ((field.value.length >= 3) && (field.value != "Que recherches-tu ?")) 
		sq = window.setTimeout("delayedSearch(\"" + field.value + "\");",100) ;
	else {
		$('suggestions').style.display = "none" ;
	}
}

function delayedSearch(value) {	
	l_options = $$('.moteurOption') ;
	
	ok = 1 ;

	for(i = 0; i < l_options.length; i++) {		
		if ((l_options[i].value != "comparateur") && (l_options[i].checked)) {
			ok = 0 ;		
		}
	}


	if (ok) {
		if ($('suggestions').style.display == "block") {
			$('suggestions').style.display = "none" ;	
		}

		var request = new Ajax.Request('/comparateur/ajax/liveSearch.php?req=' + value,
									   {method:"post", 
										onComplete: function(transport) {
												responseText = transport.responseText ;
												
												suggestBox = $('suggestions') ;
												
												if (responseText.length > 0) {
													suggestBox.innerHTML = responseText ;
													
													if (suggestBox.style.display != "block") {
														suggestBox.style.display = "block" ;
													}
												}
											}
										}) ;	
	
	}
	
}

function changeSQ(value) {
	$('qs').value = value ;

	$('suggestions').style.display = "none" ;								  

	$('qs').style.fontStyle = "normal" ;
	$('qs').style.fontWeight = "bold" ;
	$('qs').style.color = "#990066" ;
	
	$('xform_moteur').submit() ;

}

