/*
	This is the JavaScript file for the AJAX Suggest 
*/

var PartLookupHelp = Class.create({
  initialize: function(element, update, options) {
    //element          = $(element);
    this.element     = $(element); 
    this.update      = $(update);   
    this.index       = 0;     
    this.entryCount  = 0;
    this.hideTimeout = null;
    this.items 		 = null;

	Event.observe(this.update, "click", this.handleClick.bindAsEventListener(this));  
	Event.observe(this.element, "blur", this.handleBlur.bindAsEventListener(this));  
	Event.observe(this.update, "blur", this.handleBlur.bindAsEventListener(this));  
	Event.observe(this.element, "focus", this.handleFocus.bindAsEventListener(this));  
	Event.observe(this.update, "focus", this.handleFocus.bindAsEventListener(this));  
	Event.observe(this.element, "keyup", this.handleKeyup.bindAsEventListener(this));  
	Event.observe(this.update, "keyup", this.handleKeyup.bindAsEventListener(this)); 
	Event.observe(this.element, "keydown", this.handleKeydown.bindAsEventListener(this));  
	Event.observe(this.update, "keydown", this.handleKeydown.bindAsEventListener(this)); 
	//Event.observe(this.update, "mouseout", this.handleMouseout.bindAsEventListener(this));
	Event.observe(this.update, "mouseover", this.handleMouseover.bindAsEventListener(this)); 
  },

//Called from keyup on the search textbox.
//Starts the AJAX request.
searchSuggest : function(event) 
{    
	switch(event.keyCode) {
       case Event.KEY_TAB:
       case Event.KEY_RETURN:
       case Event.KEY_ESC:
       case Event.KEY_LEFT:
       case Event.KEY_RIGHT:
       case Event.KEY_UP:
       case Event.KEY_DOWN:
          Event.stop(event);
          return;
      }
      
    if(this.element.value.length < 2)
	    {
	    this.update.hide();
	    this.update.innerHTML = '';
	    return;
	    }
	var suggestURL = 'itemsuggest.php';
	var itemsnip = $F('complianceitem');
	new Ajax.Request(suggestURL, 
		{
		 method:'get',
		 parameters: { 'itemsnip': itemsnip },  
		 onSuccess: function(response) 
		 	{
				this.items = response.responseText.evalJSON(); 
		 		if(this.items.item.length > 0)		 	
			 		{
			 		var suggest = "<ul class='suggest_link'>";
			 		for (var i = 0 ; i < this.items.item.length ; i++) 
			 		{
		    			var item = this.items.item[i];
		    			suggest += "<li>" + item.code + "</li>";
		    		}
		    		suggest += "</ul>";
		    		this.update.innerHTML = suggest;
			 		//this.update.innerHTML = response.responseText;
			 		Element.cleanWhitespace(this.update);
      				Element.cleanWhitespace(this.update.down());
			 		if(this.update.firstChild && this.update.down().childNodes) 
			 			{
				        this.entryCount = this.update.down().childNodes.length;
				        for (var i = 0; i < this.entryCount; i++) 
				        	{
					          var entry = this.getEntry(i);
					          entry.autocompleteIndex = i;
					          //Event.observe(entry, "mouseover", this.handleMouseover.bindAsEventListener(this));
					        }
					    this.index = 0;
				    	this.render();
				        }
				    else 
				        { 
				        this.update.hide();
				    	this.update.innerHTML = '';
				        }
				    
			 		} 
			 	else
				 	{
				 	this.update.hide();
				    this.update.innerHTML = '';
				 	}
		 	}.bind(this)
		 }); 
},

handleClick : function(event)
	{
	var elt = Event.element(event);
	if(elt.tagName == 'LI' || elt.tagName == 'SPAN')
		{
		//this.element.value = elt.innerHTML;
		this.element.value = this.items.item[this.index].code;
		this.update.innerHTML = '';
		this.update.hide();
		}
	event.stop();
	},
	
	
handleMouseover : function(event)
	{
	var element = Event.findElement(event, 'LI');

    if('undefined' != typeof(element))
    {
	    if(this.index != element.autocompleteIndex) 
	    {
	        this.index = element.autocompleteIndex;
	        this.render();
	    }
	}
    Event.stop(event);
	/*var elt = Event.element(event);
	if(elt.tagName == 'LI')
		{
		this.suggestOver(elt);
		}
	event.stop();*/
	},
		
handleFocus : function(event)
	{
	if(typeof(this.update) == 'undefined'
		|| typeof(this.element) == 'undefined')
		{
		return;
		}
	var elt = Event.element(event);
	if(elt.getAttribute('id') == this.element.getAttribute('id')
		|| elt.getAttribute('id') == this.update.getAttribute('id'))
		{
		if (this.hideTimeout) clearTimeout(this.hideTimeout); 
		}
	},
	
handleBlur : function(event)
	{
	this.hideTimeout = setTimeout(this.hide_suggest.bind(this), 250);
	},
	
hide_suggest : function()
{
	if(typeof(this.update) != 'undefined')
		{
		//this.update.hide();
		this.update.fade();
		}
},
	
//keyPress event. The following methods are adopted mostly from scriptaculous control.js
handleKeyup : function(event) 
{
	this.searchSuggest(event);
},

handleKeydown : function(event) 
{	
    if(typeof(this.update) != 'undefined' && this.update.visible())
      switch(event.keyCode) {
       //case Event.KEY_TAB:
       case Event.KEY_RETURN:
         this.selectEntry();
         Event.stop(event);
         return;
       case Event.KEY_ESC:
         this.update.hide();
         Event.stop(event);
         return;
       case Event.KEY_LEFT:
       case Event.KEY_RIGHT:
         Event.stop(event);
         return;
       case Event.KEY_UP:
         this.markPrevious();
         this.render();
         Event.stop(event);
         return;
       case Event.KEY_DOWN:
         this.markNext();
         this.render();
         Event.stop(event);
         return;
       //case Event.KEY_TAB:
         //this.selectEntry();
       //  return;
      }
},

 render : function() 
 {
    if(this.entryCount > 0) 
    	{
      	for (var i = 0; i < this.entryCount; i++)
      		{
        	this.index==i ? 
	         Element.addClassName(this.getEntry(i),"selected") : 
             Element.removeClassName(this.getEntry(i),"selected");
             Element
		    }
		this.update.show();
	    }
	else 
		{
	      this.update.hide();
	    } 
  },
  
  markPrevious : function() {
    if(this.index > 0) this.index--
      else this.index = this.entryCount-1;
    //this.getEntry(this.index).scrollIntoView(true);
    this.getEntry(this.index).scrollIntoView(false);
  },
  
  markNext : function() {
    if(this.index < this.entryCount-1) this.index++
      else this.index = 0;
    this.getEntry(this.index).scrollIntoView(false);
  },
  
  getEntry : function(index) {
    return this.update.firstChild.childNodes[index];
  },
  
  getCurrentEntry : function() {
    return this.getEntry(this.index);
  },
  
  selectEntry : function() {
    var elt = this.getCurrentEntry();
    if(elt.tagName == 'LI')
		{
		this.element.value = this.items.item[this.index].code;
		//this.element.value = elt.innerHTML;
		this.update.innerHTML = '';
		this.update.hide();
		}
  }
});
