﻿ var sTextboxValue = "";  
    /**
 * An autosuggest textbox control.
 * @class
 * @scope public
 */
function AutoSuggestControl(oTextbox /*:HTMLInputElement*/,
                            oProvider /*:SuggestionProvider*/) {
       
    /**
     * Suggestion provider for the autosuggest feature.
     * @scope private.
     */
    this.provider /*:SuggestionProvider*/ = oProvider;
    
    /**
     * The textbox to capture.
     * @scope private
     */
    this.textbox /*:HTMLInputElement*/ = oTextbox;
    
    //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*/) {
    
     
    //make sure there's at least one suggestion
    if (aSuggestions.length > 0) {
        this.typeAhead(aSuggestions[0]);
    }
};


/**
 * Handles keyup events.
 * @scope private
 * @param oEvent The event object for the keyup event.
 */
AutoSuggestControl.prototype.handleKeyUp = function (oEvent /*:Event*/) {
  // alert('iKeyCode');
    var iKeyCode = oEvent.keyCode;
    trace(iKeyCode);
    if(iKeyCode == 13) {
        
        selectKeywordFromTextBox();
       //oEvent.keyCode = 0;
        return true;
    }
    
    if(iKeyCode == 8){
        hltItem = 0;
        updateKeywordList();
        }
        
    if(iKeyCode == 38 || iKeyCode == 40) {
        handleUpDown(iKeyCode);
        return;
        
    }
        
    //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
        hltItem = 0;
        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;
        }    
        oEvent.cancelBubble = true;
        oEvent.returnValue = false;
        
        //call the handleKeyUp() method with the event object
      
        oThis.handleKeyUp(oEvent);
    
        return true;
        
    };
    
};

/**
 * 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);
    }
};



/**
 * Provides suggestions for state names (USA).
 * @class
 * @scope public
 */
function KeywordSuggestions() {
    //this.keywords = keywordlist;
}

/**
 * Request suggestions for the given autosuggest control. 
 * @scope protected
 * @param oAutoSuggestControl The autosuggest control to provide suggestions for.
 */
KeywordSuggestions.prototype.requestSuggestions = function (oAutoSuggestControl /*:AutoSuggestControl*/) {
    
    var aSuggestions = [];
    var sTextboxValue = "";
//    var sTextboxValue = oAutoSuggestControl.textbox.value;
    
    if (sTextboxValue.length > 0){
    
        //search for matching  
        for (var i=0; i < keywords.length; i++) { 
            if (keywords[i].toUpperCase().indexOf(sTextboxValue.toUpperCase()) == 0) {
                if(!isKeywordActive(i))
                    aSuggestions.push(keywords[i]);
            } 
        }
        
       // for(i=0; i<aSuggestions.length; i++)
            //trace(aSuggestions[i]);
    }

    //provide suggestions to the control
    
      updateKeywordList();
    oAutoSuggestControl.autosuggest(aSuggestions);
};

var hltItem = 0;


function handleUpDown(keyCode) {
    var listElem = document.getElementById('keywordlist_mcontentwrapper');
    //var item
      
    //        var testList = document.getElementById('keywordlist_mcontentwrapper');
    //        var testCount = testList.getElementsByTagName('DIV');
    //        
    //        alert(testCount.length + ' ' + testList.getElementsByTagName('DIV')[0].textContent);
  
    try {  
        listElem.getElementsByTagName('div')[hltItem].className = 'filterlistitem';
        if(keyCode == 38) {
     
            hltItem--;   
        } else {
            if ((listElem.getElementsByTagName('div')[0].className == 'filterlistitem_highlighted')){alert('highlighted');}
            hltItem++;
        }
           
        var item =   listElem.getElementsByTagName('div')[hltItem];
        item.className = 'filterlistitem_highlighted';
        trace(item.scrollTop);
        var val = item.textContent ? item.textContent : item.innerText;
        if(val != null){document.getElementById('txtKeyword').value = val;}
        else{document.getElementById('txtKeyword').value = '';}
        if(item.offsetTop > 50) {
        fleXcrollTo('keywordlist', 0,item.offsetTop -40,false);
        }
  
    }catch(e){
        trace(e);
        if(keyCode == 38) {

            hltItem++;
        } else {
          
            hltItem--;
        }
              
        var item =   listElem.getElementsByTagName('div')[hltItem];
        item.className = 'filterlistitem_highlighted';
        trace(item.scrollTop);
        if(item.offsetTop > 50) {
            fleXcrollTo('keywordlist_mcontentwrapper', 0,item.offsetTop -40,false);
        }
  }
    
    //alert(hltItem);
     
}
