/** 
 * @author gpartner
 */  

//key action up and down
function keyaction(textBoxId, containerDivId){
    var ac = this;
    this.textbox   = document.getElementById(textBoxId);
    this.div       = document.getElementById(containerDivId);
    this.list      = this.div.getElementsByTagName('div');
    
    this.pointer = null;
    this.textbox.onkeyup = function( e ) {
        e = e || window.event;
        switch( e.keyCode ) {
            case 38: //up
                //ac.selectDiv(-1);
                //break;
            case 40: //down
                //ac.selectDiv(1);
                //break;
            default :
            	//call keyaction(); for new request, keyboard down from position 0 
            	new keyaction('gsaKeyWord_input', 'gsa_autoUl');
            	Auto.autoGet(textBoxId);
            	break;
        }
    };
		
    this.selectDiv = function( inc ) {
        if( this.pointer !== null && this.pointer+inc >= 0 && this.pointer+inc <= this.list.length ){ 
	        if(this.pointer+inc < this.list.length ){
	            //this.list[this.pointer].className = 'gsa_ul';
				 this.list[this.pointer].className = 'gsa_mouseOut';
	            this.pointer += inc;
	            //this.list[this.pointer].className = 'gsa_active';
				 this.list[this.pointer].className = 'gsa_mouseOver';
	            this.textbox.value = this.list[this.pointer].innerHTML;
	            
	            	
		        
        	}
    	}
        if( this.pointer === null ) {
            this.pointer = 0;
			
				this.list[this.pointer].className = 'gsa_mouseOver';
           // this.list[this.pointer].className = 'gsa_active';
            this.textbox.value = this.list[this.pointer].innerHTML;
        } 
    };
	
}
  
/*Auto.autoGet(value);*/
var Auto = {  
      
    autoGet : function(value) {
	var textBoxResults = document.getElementById(value);
	var valueInText = textBoxResults.value;
        if(valueInText == '') {
            this.closePop();  
        } else {
            // get the return value list from json  
			getListJson(valueInText); 
        }  
    },  
      
    autoCallback : function(list) {  
		
        //remove the old pop li  
        this.closePop();  
		
        if (list.length > 0) { 
			
			//for(var i = 0;i < list.length; i++){
			var nbTot=list.length;
			if(nbTot>5){
				nbTot=5;
			}
			for(var i = 0;i < nbTot; i++){
				
                //$("gsa_autoUl").className = 'gsa_autoUl';  
                var autoLi = document.createElement("li");  
             
				//this.className = 'gsa_active';   
                // onmouseout event  
                autoLi.onmouseout = function() {  
                    this.className = 'gsa_mouseOut';   
                    Assist.isOnmouseover = false;  
                };  
                  
                // onmouseover event  
                autoLi.onmouseover = function() {  
                    this.className = 'gsa_mouseOver';  
                    Assist.isOnmouseover = true;  
                };  
                  
                // onclick event  
                autoLi.onclick = function() {  
                    Assist.autoPush(this);  
                };
                //Display the list and replace %20 -> "_"
                liValue = '<div class="gsa_resultName">' + list[i].name.replace("%20"," ") + '</div>';  				
                autoLi.innerHTML = liValue;  
//$("gsa_autoUl").appendChild(autoLi);  
document.getElementById("gsa_autoUl").appendChild(autoLi);  
				
            }  
              
            //pop the li  
            //var closeLi = document.createElement("li");  
            //closeLi.style.textAlign = "right";  
            //closeLi.innerHTML = '<a href="javascript:void(0);" onclick="Auto.closePop();">Close</a>';  
            //$("autoUl").appendChild(closeLi);  
        }  
        
     
    },  
    
    //remove the list line by line 
    closePop : function() {  

		var ul = document.getElementById("gsa_autoUl").childNodes.length;  
		for(var i = ul - 1; i >= 0 ; i--) {  
			document.getElementById("gsa_autoUl").removeChild(document.getElementById("gsa_autoUl").childNodes[i]);  
		}  
		document.getElementById("gsa_autoUl").className = "gsa_borderNull";  
		/*var ul = $("gsa_autoUl").childNodes.length;  
			for(var i = ul - 1; i >= 0 ; i--) {  
				$("gsa_autoUl").removeChild($("gsa_autoUl").childNodes[i]);  
			}  
			$("gsa_autoUl").className = "gsa_borderNull";  
		*/
        
    }  
};  

	var Assist = {  
	  
	    isOnmouseover : false,  
	      
	    // close the auto complete message when onclick the page of the body(input lose focus)  
	    autoClose : function() {  
	        if(this.isOnmouseover == false) {  
	            Auto.closePop();  
	        }  
	    },  
	      
	    autoPush : function(obj) {  
	        //$("gsaKeyWord_input").value = obj.childNodes[0].firstChild.nodeValue;  
			document.getElementById("gsaKeyWord_input").value= obj.childNodes[0].firstChild.nodeValue;			
	        Auto.closePop();
			doGSA_search();
	        //document.forms[0].submit();  
	    }  
	  
	};  
  
/*
 * the json data 
 */
function getListJson(name) {  

	
	var xmlhttp = null;
	if (window.XMLHttpRequest){
	 // code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }else{
		  // code for IE6, IE5
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	 if(!xmlhttp)
		{
		alert('Abandon :( Impossible de créer une instance XMLHTTP');
		return false;
		}
	 xmlhttp.onreadystatechange = function(){
		
		if (xmlhttp.readyState==4)
		{
			if (xmlhttp.status == 200)
			{				
				var summaryJson = xmlhttp.responseText;
				var summ = eval("(" + summaryJson + ")");
				Auto.autoCallback(summ.results);
			}
		}
	};
	
	
	xmlhttp.open('GET',domainGSAServices+"/suggest"+'?site=Internet&client=default_frontend&access=p&format=rich&q='+name,true);
	xmlhttp.send(null);
	
	/* data static for testing */
	/*var summ = { "query":"f", "results": [ {"name":"formuler une synthĂ¨se", "type":"suggest"}, {"name":"formulaire sud ouest", "type":"suggest"}, {"name":"ffsa", "type":"suggest"}, {"name":"fiche de demande de dns", "type":"suggest"}, {"name":"filetype:pdf", "type":"suggest"}, {"name":"fleches", "type":"suggest"}, {"name":"flĂ©chĂ©", "type":"suggest"}, {"name":"fonds quatorze compartiments", "type":"suggest"}, {"name":"formulaire demande de dns", "type":"suggest"}, {"name":"france", "type":"suggest"}] }
	Auto.autoCallback(summ.results);
	*/
}  
