var AutoCompleteTimer = null;


function noSuggestions() {
	var Search = document.getElementById("ffqueryfield");
	if(!Search.value) {
		Search.value = "Mot(s)-clé(s), auteur, ISBN, ...";
	}


	dojo.connect(Search, "onfocus",function(e) {
		if(Search.value == "Mot(s)-clé(s), auteur, ISBN, ..." ){
			Search.value = "";
		}
	});
	
	dojo.connect(Search, "onblur", function(e) {
		if(Search.value == "" ){
			Search.value = "Mot(s)-clé(s), auteur, ISBN, ...";
		}
	});
	
	return true;
}

dojo.addOnLoad(function(){
	dojo.require("dojo.fx");
	dojo.require("dojox.fx._base");
	dojo.require("dojo.fx.easing");
	dojo.require("dijit.form._FormWidget");


	var Search      = dojo.byId("SearchString");
	if(document.getElementById("ffqueryfield")) {
		return noSuggestions();
	}
	var suggestions = dojo.byId("suggestions");
	
	dojo.attr(Search, {
		autocomplete:"off"
	});


	var SearchStr = document.getElementById("SearchString");
	if(document.getElementById("ffqueryfield")) {
		SearchStr = document.getElementById("ffqueryfield");
	}


	if(!Search.value) {
		SearchStr.value = "Mot(s)-clé(s), auteur, ISBN, ...";
	}


	var fadeArgs = {
		node: "suggestions",
		style:{dispay:"none"}
	};


	dojo.connect(Search, "onkeyup", function(e) {
		var sSearch = escape(this.value);
		clearTimeout(document.AutoCompleteTimer);
		document.AutoCompleteTimer = setTimeout("lookup('"+sSearch+"')", 800);
	});


	dojo.connect(Search, "onfocus",function(e) {
		if(!Search.focus()){
			Search.value = "";		
			fill(this.value);
		}
	});


	dojo.connect(Search, "onblur", function(e) {
		dojo.fadeOut(fadeArgs).play();


		if(Search.value == "" ){
			Search.value = "Mot(s)-clé(s), auteur, ISBN, ...";
		}


		setTimeout("dojo.style('suggestions','display','none')",500);
	});
});

function lookup(inputString) {
	if(inputString.length <= 0) {
		return true;
	} else {
		dojo.xhrGet({
			preventCache: true,
				handleAs: "json",
				contentType: "application/json; charset=iso-8859-15",
			url: '?ViewAction=ViewDojoSearchAutoComplete&ChangeAction=BlockMonitor&SearchString='+inputString,
			handle: function(response, ioArgs){
				data = eval(response);
				_buildSuggestionList(inputString, response);
			},
			error: function(response, ioArgs){
				console.error("HTTP status code: ",ioArgs.xhr.status);
				return response;
			}
		});


		return true;
	}
} // lookup


function fill(thisValue) {
	var suggestions = dojo.byId("suggestions");
	var SearchStr = document.getElementById("SearchString");
	SearchStr.value = thisValue;
	dojo.fadeOut({ node:"suggestions" }).play();
	dojo.style("suggestions", "display", "none");
}


function _buildSuggestionList(sSearch, ahSuggestions) {
	var suggestions = document.getElementById("suggestions");


	while (suggestions.firstChild) {
		suggestions.removeChild(suggestions.firstChild);
	}


	var SearchStr = document.getElementById("SearchString");
	if(document.getElementById("ffqueryfield")) {
		SearchStr = document.getElementById("ffqueryfield");
	}
	
	var Search    = dojo.byId("SearchString");
	if(document.getElementById("ffqueryfield")) {
		SearchStr = document.getElementById("ffqueryfield");
	}
	
	var nbSuggestions = 0;


	for(var i = 0; i < ahSuggestions.length; i++) {
		if(i > 8) continue;
		
		var hSuggestion = ahSuggestions[i];
		if(!hSuggestion) continue;
		
		if(sSearch.toLowerCase() == hSuggestion.Term.toLowerCase()) continue;
		
		console.log(hSuggestion.Term+': '+hSuggestion.CountRequests);
		dojo.query("ul#suggestions").addContent('<li id="suggest_'+i+'" alt="'+hSuggestion.Term+'" onclick="fill(\''+hSuggestion.Term+'\');">'+hSuggestion.Term+'</li>');


		nbSuggestions++;
	}
	
	console.log(nbSuggestions);


	var divSearch = dojo.byId("BarreRecherche");
	var oSearchInputCoords = dojo.coords(SearchStr, false);


	if (document.getSelection) { //Sur Firefox
		dojo.style("suggestions","left", oSearchInputCoords.x+"px");
		dojo.style("suggestions","top",  (oSearchInputCoords.t + 137)+"px");
	} else if (document.selection) { //Sur IE
		dojo.style("suggestions","left", oSearchInputCoords.x+"px");
		dojo.style("suggestions","top",  (oSearchInputCoords.t + 137)+"px");
	} else return;


	if(suggestions) {
		if(nbSuggestions > 0) {
			dojo.style("suggestions", "display", "block");
			dojo.fadeIn({ node:"suggestions" }).play();
		} else {
			dojo.fadeOut({node: "suggestions" }).play();
			dojo.style("suggestions", "display", "none");
		}
	}
}

