// SUGGESTION TOOL

//
function initSuggest() {
	// check version... positionnement FFS
	if ( navigator.userAgent.indexOf("Firefox/3") >= 0 ) {
		// Test sous FireFox 3 OK
		document.getElementById("suggest-artist").style.zIndex = "1";
		document.getElementById("suggest-artist").style.position = "absolute";
		document.getElementById("suggest-artist").style.width = "203px";
		document.getElementById("suggest-artist").style.marginTop = "-4px";

		document.getElementById("suggest-song").style.zIndex = "1";
		document.getElementById("suggest-song").style.position = "absolute";
		document.getElementById("suggest-song").style.width = "203px";
		document.getElementById("suggest-song").style.marginTop = "-4px";
	} else if ( navigator.userAgent.indexOf("MSIE 7.0") ) {
		// Test sous IE7 OK
		document.getElementById("suggest-artist").style.zIndex = "1";
		document.getElementById("suggest-artist").style.position = "absolute";
		document.getElementById("suggest-artist").style.width = "203px";
		document.getElementById("suggest-artist").style.top = "26px";
		document.getElementById("suggest-song").style.margin = "auto";
		document.getElementById("suggest-artist").style.left = "4px";

		document.getElementById("suggest-song").style.zIndex = "1";
		document.getElementById("suggest-song").style.position = "absolute";
		document.getElementById("suggest-song").style.width = "203px";
		document.getElementById("suggest-song").style.top = "57px";
		document.getElementById("suggest-song").style.left = "4px";
	} else {
		// suggestions desactivees
		;
	}
	mainLoopSuggest();
}

// global vars for the main loop
// Sur le timer :
// - very good typing speed = 300 CPM = 1 char every 200ms.
// - Passer de 0ms à 500ms fait passer du simple au tiers en quantité de requetes.
var globalBoxChangedBool = false;
var globalBoxChangedName = true;
var globalTiming = 250;
//var test = 0;

// handles the timing side of the job
function mainLoopSuggest() {
	if ( globalBoxChangedBool == true) {
		globalBoxChangedBool = false;
		// test de perf
		//test = test + 1;
		//document.getElementById("test").value = "Requete#" + test;
		var box = globalBoxChangedName;
		if ( (box == "artist") && document.getElementById(box+"box").value.length > 3) {
			suggest(box, document.getElementById("artistbox").value, document.getElementById("songbox").value);
		}
		if ( (box == "song") && document.getElementById("artistbox").value.length > 0) {
			suggest(box, document.getElementById("artistbox").value, document.getElementById("songbox").value);
		}
		document.getElementById("suggest-"+box).style.display = "block";
	}
	setTimeout('mainLoopSuggest()', globalTiming);
}

// updates either search box
function updateSearchBox(box, query) {
	document.getElementById(box+"box").value = query;
	document.getElementById("suggest-"+box).style.display = "none";
	toggleLabel(box);
}

// 
function startSuggest(box) {
	if (document.getElementById(box+"box").value.length > 0) {
		globalBoxChangedName = box;
		globalBoxChangedBool = true;
	}
	toggleLabel(box);
}
// 
function stopSuggest(box) {
	setTimeout('document.getElementById("suggest-'+box+'").style.display = "none";', 1000);
}
//
function toggleLabel(box) {
	if (document.getElementById(box+"box").value.length > 8) {
		document.getElementById(box+"box").style.background = "white";
	} else {
		document.getElementById(box+"box").style.background = "white url('/img/inputlabel"+box+".gif') center center repeat-x";
	}
}
function suggest(box, artist, song) {
	var xmlHttp;
	try {		
		// Firefox, Opera 8.0+, Safari		
		xmlHttp=new XMLHttpRequest();
	}	
	catch (e) {	
		// Internet Explorer
		try {			
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");		
		}
		catch (e) {
			try {				
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");	
			}
			catch (e) {	
				alert("Your browser does not support AJAX!");
				return false;			
			}
		}
	}
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {		
			document.getElementById("suggest-" + box).innerHTML = xmlHttp.responseText;
		}
	}	
	xmlHttp.open("GET", "suggest.php?box=" + box + "&artist=" + artist + "&song=" + song, true);
	xmlHttp.send(null);
}


// ci-dessous = à jarter d'ici et surotut des pages web concernees
function initArtistBox(){
	if ( document.getElementById("artistbox").value == "artist" ) {
		document.getElementById("artistbox").value = "";
	}
}

function initSongBox(){
	if ( document.getElementById("songbox").value == "song" ) {
		document.getElementById("songbox").value = "";
	}
}