/**
 * @title   AutoSuggestControl
 * @author  (C) 2006-2007 SB <subrata@thermo.sdsu.edu>
 *
 * control class that holds the provider and output objects.
 */
AutoSuggestControl = Class.create();

AutoSuggestControl.prototype = {
    
    initialize: function(oTextbox, oProvider) {
        this.provider = oProvider;
        this.textbox = oTextbox;    
        this.init();

    },
    
// select a range of text in the textbox and hightlight it    
    selectRange:  function (iStart, iLength) {
        if (this.textbox.createTextRange) {
            var oRange = this.textbox.createTextRange(); 
            oRange.moveStart("character", iStart); 
            oRange.moveEnd("character", iLength - this.textbox.value.length); 
            oRange.select();
        } else if (this.textbox.setSelectionRange) {
            this.textbox.setSelectionRange(iStart, iLength);
        } 

        this.textbox.focus(); 
    },
    
 // display suggested text with the new info hightlighted  
    typeAhead: function (sSuggestion) {
        if (this.textbox.createTextRange || this.textbox.setSelectionRange) {
            var iLen = this.textbox.value.length; 
            this.textbox.value = sSuggestion; 
            this.selectRange(iLen, sSuggestion.length);
        }
    },
               
    
 // typeahead only if there is a suggestion  
    autosuggest : function (aSuggestions) {
        if (aSuggestions.length > 0) {
            this.typeAhead(aSuggestions[0]);
        }
    },
    
 // autosuggest by inspecting the passed event  
    handleKeyUp  : function (oEvent) {
        var iKeyCode = oEvent.keyCode;
        if (iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode <= 46) || (iKeyCode >= 112 && iKeyCode <= 123)) {
        //ignore
        } else {
            this.provider.requestSuggestions(this);
        }
    },
        
        
 // assign onkeyup event to handleMethod  
    init :  function () {
        var oThis = this;
        this.textbox.onkeyup = function (oEvent) {
            if (!oEvent) {
                oEvent = window.event;
            }
            oThis.handleKeyUp(oEvent);
        }
    }

};






