/**
 * aheadWorks Co.
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the EULA
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://ecommerce.aheadworks.com/LICENSE-M1.txt
 *
 * @category   AW
 * @package    AW_Seacrhautocomplete
 * @copyright  Copyright (c) 2003-2010 aheadWorks Co. (http://www.aheadworks.com)
 * @license    http://ecommerce.aheadworks.com/LICENSE-M1.txt
 */

var Searchcomplete = Class.create();
Searchcomplete.prototype = {
    field: "",
    initialize : function(){
        this.field = $('myInput');
    },
    initAutocomplete : function(url, destinationElement){
        var SACautocompleter = new Ajax.Autocompleter(
            this.field,
            destinationElement,
            url,
            {
                paramName: this.field.name,
                method: 'get',
                minChars: 3,
                frequency: queryDelay,
                onShow : function(element, update) {
                    var posSC = $('myInput').cumulativeOffset();
                    posSC.top = posSC.top + parseInt($('myInput').getHeight()) + 3;
                    if (!Prototype.Browser.IE)
                        posSC.left -= (parseInt($('myInput').getStyle('padding-left')) + 3);

                    $('myContainer').setStyle({
                        top: posSC.top+"px",
                        left: posSC.left+"px"
                    });

                    update.show();
                    $('myContainer').show();
                },
                onHide : function(element, update)
                {
                    $('myContainer').hide();
                    update.hide();
                },
                updateElement : function(element)
                {
                    return false;
                }
            }
        );
        SACautocompleter.startIndicator = function(){
            this.element.setStyle({
                backgroundImage: 'url("'+preloaderImage+'")',
                backgroundRepeat: 'no-repeat',
                backgroundPosition: 'right'
            });
        }
        SACautocompleter.stopIndicator = function(){
            this.element.style.backgroundImage = 'none';
        }
		
		SACautocompleter.updateChoices = function(choices){
			if(!this.changed && this.hasFocus) {
		      this.update.innerHTML = choices;
		      Element.cleanWhitespace(this.update);
		      Element.cleanWhitespace(this.update.down());
			  
			  if (this.update.firstChild && typeof(this.update.firstChild.innerHTML) == 'undefined'){
				  this.update.removeChild(this.update.firstChild);
			  }

		      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;
		          this.addObservers(entry);
		        }
		      } else {
		        this.entryCount = 0;
		      }

		      this.stopIndicator();
		      this.index = 0;

		      if(this.entryCount==1 && this.options.autoSelect) {
		        this.selectEntry();
		        this.hide();
		      } else {
		        this.render();
		      }
		    }
		}
    }
}

