/**
 * AutoComplete Field - JavaScript Code
 *
 * This is a sample source code provided by fromvega.
 * Search for the complete article at http://www.fromvega.com
 *
 * Enjoy!
 *
 * @author fromvega
 *
 */

//global variables
var acListTotal   =  0;
var acListCurrent = -1;
var acDelay		  = 500;
var acURL		  = null;
var acSearchId	  = null;
var acResultsId	  = null;
var acSearchField = null;
var acSecondParamField = null;
var acResultsDiv  = null;

function setAutoComplete(field_id, results_id, get_url,secondParam_id){//MODIF  JOUVE			

	// initialize vars
	acSearchId  = "#" + field_id;
//	alert("field_id="+field_id);
	acResultsId = "#" + results_id
	acSecondParamField = secondParam_id ? $("#" + secondParam_id) : false;//MODIF  JOUVE			

	acURL 		= get_url;

	// create the results div
	$("body").append('<div id="' + results_id + '"></div>');

	// register mostly used vars
	acSearchField	= $(acSearchId);
	acResultsDiv	= $(acResultsId);

	// reposition div
	repositionResultsDiv();

	// on blur listener
	//acSearchField.blur(function(){ setTimeout("clearAutoComplete()", 200) });

	// on key up listener
	acSearchField.keyup(function (e) {
		// get keyCode (window.event is for IE)
		var keyCode = e.keyCode || window.event.keyCode;
		var lastVal = acSearchField.val();

		// check an treat up and down arrows
		if(updownArrow(keyCode)){
			return;
		}

		// check for an ENTER or ESC
		if(keyCode == 13 || keyCode == 27){
			clearAutoComplete();
			return;
		}

		// if is text, call with delay
		setTimeout(function () {autoComplete(lastVal)}, acDelay);

	});
}

//treat the auto-complete action (delayed function)
function autoComplete(lastValue)
{
	// get the field value
	var part = acSearchField.val();

	secondParam = (acSecondParamField && acSecondParamField.val()) ? acSecondParamField.val() : '';//MODIF  JOUVE			

	// if it's empty clear the results box and return
	if(part == ''){
		clearAutoComplete();
		return;
	}

	// if it's equal the value from the time of the call, allow
	if(lastValue != part){
		return;
	}
	// get remote data as JSON	
	$.getJSON(acURL + part,  { secondParam: secondParam },//MODIF  JOUVE			
			function(json){
		var ansLength = 0;

		if (null != json['Libelle'])
			// get the total of results
			ansLength = acListTotal = json['Libelle'].length;//MODIF  JOUVE			

		// if there are results populate the results div
		if(ansLength > 0){

			var newData = '';
			newData += '<div id="results-slider">';
			newData += '<ul>';
			if(ansLength>json['max']){
				var nbpage = Math.ceil(ansLength/json['max']);
				var divPerPage = json['max'];
			}else{
				var nbpage = 1;
				var divPerPage = ansLength;
			}
			var page = 0;
			//newData += '<li id="result-page-'+page+'">';
			newData += '<li>';
			for(i=0; i< ansLength; i++){
				if((i%divPerPage==0)&&(i>0)){
					page++;
					if(page<nbpage){
						newData += '</li>';
						//newData += '<li id="result-page-'+page+'">';
						newData += '<li>';
					}
				}
				newData += '<div id="'+json['Code'][i]+'" class="unselected"><input id="myselect_'+i+'" name="myselect_'+i+'" value="'+json['Code'][i]+'" type="checkbox"/> ' + json['Libelle'][i] + '</div>';//MODIF  JOUVE
			}
			newData += '</li>';
			newData += '</ul>';
			newData += '</div>';
			if (secondParam == "avancee"){
				newData += '<div id="ok" class="unselected"><input type="button" value="ok" onclick="copyValues();"/></div>';
			}
			// update the results div
			acResultsDiv.html(newData);
			acResultsDiv.css("display","block");

			// for all divs in results
			var divs = $(acResultsId + " #results-slider div");

			// on mouse over clean previous selected and set a new one
			divs.mouseover( function() {
				divs.each(function(){ this.className = "unselected"; });
				this.className = "selected";
			});

			// on click copy the result text to the search field and hide
			/*divs.click( function() {
				acSearchField.val(this.childNodes[0].nodeValue);
				$('#hidden'+acSearchField.attr('id')).attr('value',this.id); //MODIF  JOUVE				
				clearAutoComplete();
			});*/
			$('#results-slider ul li').css("width", $('#results-slider').parent().css("width"));
			$('#results-slider ul li div').css("width", $('#results-slider').parent().css("width"));
			$("#results-slider").sudoSlider({ customLink:'a.customLink', nextHtml:'<a href="#" class="nextBtn"> Next &gt;&gt;</a>', prevHtml:'<a href="#" class="prevBtn"> &lt;&lt; Previous </a>', speed:'100' });
			
		} else {
			clearAutoComplete();
		}
	});
}

//clear auto complete box
function clearAutoComplete()
{
	acResultsDiv.html('');
	acResultsDiv.css("display","none");
}

//reposition the results div accordingly to the search field
function repositionResultsDiv()
{
	// get the field position
	var sf_pos    = acSearchField.offset();
	var sf_top    = sf_pos.top;
	var sf_left   = sf_pos.left;

	// get the field size
	var sf_height = acSearchField.height();
	var sf_width  = acSearchField.width();

	// apply the css styles - optimized for Firefox
	acResultsDiv.css("position","absolute");
	acResultsDiv.css("left", sf_left - 5);
	acResultsDiv.css("top", sf_top + sf_height + 10);
	acResultsDiv.css("width", sf_width - 2);	
}


//treat up and down key strokes defining the next selected element
function updownArrow(keyCode) {
	if(keyCode == 40 || keyCode == 38){

		if(keyCode == 38){ // keyUp
			if(acListCurrent == 0 || acListCurrent == -1){
				acListCurrent = acListTotal-1;
			}else{
				acListCurrent--;
			}
		} else { // keyDown
			if(acListCurrent == acListTotal-1){
				acListCurrent = 0;
			}else {
				acListCurrent++;
			}
		}

		// loop through each result div applying the correct style
		acResultsDiv.children().each(function(i){
			if(i == acListCurrent){
				acSearchField.val(this.childNodes[0].nodeValue);
				this.className = "selected";
			} else {
				this.className = "unselected";
			}
		});

		return true;
	} else {
		// reset
		acListCurrent = -1;
		return false;
	}
}

function copyValues() {
	var i = 0;
	var acMySelect = $("#myselect_0");

	var acDiv;
	var str = "";
	var strhidden = "";

	while ( $(acMySelect).val() ) {
		if ($(acMySelect).is(":checked")) {
			acDiv = $("#"+$(acMySelect).val());
			if (str!="") str += " ; ";
			str += $(acDiv).text();
			if (strhidden!="") strhidden += " "
				strhidden += "P" + $(acMySelect).val();
		}
		i++;
		acMySelect = $("#myselect_"+i);
	}

	acSearchField.val(str);

	$('#hidden'+acSearchField.attr('name')).attr('value',strhidden);
//	$('#'+acSearchField.attr('name')+'_id').attr('value',strhidden);  
	clearAutoComplete();
}

