var global_enable = 1;

/**
 * An autosuggest textbox control.
 * @class
 * @scope public
 */
function AutoSuggestControl(TextboxName, /*:HTMLInputElement*/ 
                            PulldownListName,  /*:HTMLDiv to display the list of matches */
                            oProvider, /*:SuggestionProvider*/
                            PulldownCloseName
                            ) {

    var oTextbox = document.getElementById(TextboxName);
    var oPulldownList = document.getElementById(PulldownListName);
    var oPulldownClose = document.getElementById(PulldownCloseName);

    if (oPulldownList != null)
        oPulldownList.style.display = 'none';

    if (oPulldownClose != null)
        oPulldownClose.style.display = 'none';
       
    this.textbox_name = TextboxName;
    this.provider = oProvider;
    this.textbox = oTextbox;
    this.pulldownlist = oPulldownList;
    this.pulldownlist_name = PulldownListName;
    this.pulldownclose = oPulldownClose;
    this.pulldownclose_name = PulldownCloseName;
    
    //initialize the control
    this.init();
}

/**
 * Autosuggests one or more suggestions for what the user has typed.
 * If no suggestions are passed in, then no autosuggest occurs.
 * @scope private
 * @param aSuggestions An array of suggestion strings.
 */
AutoSuggestControl.prototype.autosuggest = function (aSuggestions /*:Array*/) {
    
    if (!global_enable) return;

    //if (aSuggestions.length > 0) this.typeAhead(aSuggestions[0]);

        var tLeft = 0, tTop = 0;
        var node = this.textbox;

        while ( node != null )
        {
            tTop  += node.offsetTop;
            tLeft += node.offsetLeft;
            node = node.offsetParent;
        }

        var suggest_list = '';

        for (var index = 0; index < aSuggestions.length; ++index) 
        {   
            var item = aSuggestions[index];  

            suggest_list += '<a class="aword" href="javascript:select_suggestion(\'' + this.textbox_name + '\',\'' + item + '\',\'' + this.pulldownlist_name + '\',\'' + this.pulldownclose_name + '\');">' + item + '</a><br/>';
        }

        this.pulldownlist.innerHTML = suggest_list;

        var offset = (getPageWidth() - 780) / 2;

        tLeft -= offset;

        this.pulldownlist.style.top = ( tTop + 10 ) + "px";
        this.pulldownlist.style.left = tLeft + "px";
        this.pulldownlist.style.display = 'block';

        this.pulldownclose.style.top = ( tTop + 10 + 205 ) + "px";
        this.pulldownclose.style.left = tLeft + "px";
        this.pulldownclose.style.display = 'block';

        //setTimeout("hide('"+this.pulldownlist_name+"')",350);
};


/**
 * Handles keyup events.
 * @scope private
 * @param oEvent The event object for the keyup event.
 */
AutoSuggestControl.prototype.handleKeyUp = function (oEvent /*:Event*/) {

    var iKeyCode = oEvent.keyCode;

    //make sure not to interfere with non-character keys
    if (iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode <= 46) || (iKeyCode >= 112 && iKeyCode <= 123)) {
        //ignore
    } else {
        //request suggestions from the suggestion provider
        this.provider.requestSuggestions(this);
    }
};

/**
 * Initializes the textbox with event handlers for
 * auto suggest functionality.
 * @scope private
 */
AutoSuggestControl.prototype.init = function () {

    //save a reference to this object
    var oThis = this;
    
    //assign the onkeyup event handler
    this.textbox.onkeyup = function (oEvent) {
    
        //check for the proper location of the event object
        if (!oEvent) {
            oEvent = window.event;
        }    
        
        //call the handleKeyUp() method with the event object
        oThis.handleKeyUp(oEvent);
    };
    
};

/**
 * Selects a range of text in the textbox.
 * @scope public
 * @param iStart The start index (base 0) of the selection.
 * @param iLength The number of characters to select.
 */
AutoSuggestControl.prototype.selectRange = function (iStart /*:int*/, iLength /*:int*/) {

    //use text ranges for Internet Explorer
    if (this.textbox.createTextRange) {
        var oRange = this.textbox.createTextRange(); 
        oRange.moveStart("character", iStart); 
        oRange.moveEnd("character", iLength - this.textbox.value.length);      
        oRange.select();
        
    //use setSelectionRange() for Mozilla
    } else if (this.textbox.setSelectionRange) {
        this.textbox.setSelectionRange(iStart, iLength);
    }     

    //set focus back to the textbox
    this.textbox.focus();      
}; 

/**
 * Inserts a suggestion into the textbox, highlighting the 
 * suggested part of the text.
 * @scope private
 * @param sSuggestion The suggestion for the textbox.
 */
AutoSuggestControl.prototype.typeAhead = function (sSuggestion /*:String*/) {

    //check for support of typeahead functionality
    if (this.textbox.createTextRange || this.textbox.setSelectionRange){
        var iLen = this.textbox.value.length; 
        this.textbox.value = sSuggestion; 
        this.selectRange(iLen, sSuggestion.length);
    }
};

function select_suggestion(input_textfield, value, suggestion_list, suggestion_close)
{
    var destination = document.getElementById(input_textfield);

    value = value.replace(/\<b\>/,'');
    value = value.replace(/\<\/b\>/,'');

    destination.value = value;

    hide(suggestion_list);
    hide(suggestion_close);
}

function close_suggestions(suggestion_list, suggestion_close)
{
    hide(suggestion_list);
    hide(suggestion_close);
}

function enable_suggestions()
{
    global_enable = 1;
}

function disable_suggestions()
{
    global_enable = 0;
}

