/* F6.UI.AutoComplete class
 * This class is used to add autocompletion function on textboxes. In order to add autocompletion on a texbox, let's define following class on the element F6.UI.AutoComplete.autocomplete(this, event, targetListId)
 * targetListId param is the id of the list that you can get its suggest data. In addition, for every suggest item, you must have class "SuggestData" for it.
 */
 
var isOpera = navigator.userAgent.indexOf('Opera') > -1;
var isIE = navigator.userAgent.indexOf('MSIE') > 1 && !isOpera;
var isMoz = navigator.userAgent.indexOf('Mozilla/5.') == 0 && !isOpera;

F6.UI.AutoComplete = {}
    /**get
     *
     */
    F6.UI.AutoComplete.getData = function (id) {
        var checkbox = jQuery("#" + id + " li > .SuggestData");
        var arr = [];
        checkbox.each(function () {
            arr.push(jQuery(this).text());
        });

        return arr;
    }

    /**textboxSelect
     *
     */
    F6.UI.AutoComplete.textboxSelect = function (oTextbox, iStart, iEnd) {
        switch(arguments.length) {
           case 1:
               oTextbox.select();
               break;
           case 2:
               iEnd = oTextbox.value.length;
               /* falls through */
           case 3:          
               if (isIE) {
                   var oRange = oTextbox.createTextRange();
                   oRange.moveStart("character", iStart);
                   oRange.moveEnd("character", -oTextbox.value.length + iEnd);      
                   oRange.select();                                              
               } 
               else if (isMoz) {
                   oTextbox.setSelectionRange(iStart, iEnd);
               }                    
       }

       oTextbox.focus();
    }

    /**textboxReplaceSelect
     *
     */
    F6.UI.AutoComplete.textboxReplaceSelect = function (oTextbox, sText) {
        if (isIE) {
           var oRange = document.selection.createRange();
           oRange.text = sText;
           oRange.collapse(true);
           oRange.select();                                
        } 
        else if (isMoz) {
           var iStart = oTextbox.selectionStart;
           oTextbox.value = oTextbox.value.substring(0, iStart) + sText + oTextbox.value.substring(oTextbox.selectionEnd, oTextbox.value.length);
           oTextbox.setSelectionRange(iStart + sText.length, iStart + sText.length);
        }

        oTextbox.focus();
    }

    /**match
     *
     */
    F6.UI.AutoComplete.match = function (sText, arrValues) {
        for (var i=0; i < arrValues.length; i++) {
            if (arrValues[i].toLowerCase().indexOf(sText.toLowerCase()) == 0) {
               return arrValues[i];
            }
        }

        return null;
    }

    /**autocomplete
     *
     */
    F6.UI.AutoComplete.autocomplete = function (oTextbox, oEvent, targetListId) {
        var arrValues = F6.UI.AutoComplete.getData(targetListId);

        switch (oEvent.keyCode) {
            case 38: //up arrow  
            case 40: //down arrow
            case 37: //left arrow
            case 39: //right arrow
            case 33: //page up  
            case 34: //page down  
            case 36: //home  
            case 35: //end                  
            
            case 9: //tab  
            case 27: //esc  
            case 16: //shift  
            case 17: //ctrl  
            case 18: //alt  
            case 20: //caps lock
            case 8: //backspace  
            case 46: //delete
               return true;
               break;
			case 13: //enter  
				
				if  (jQuery("#"+ targetListId).hasClass("Accordion")) {
				F6.UI.ShopListControl.addListItem2(oTextbox,oEvent);
				}
				else if (jQuery("#"+ targetListId).hasClass("ShoppingList")) {
					F6.UI.AddControl.add(oTextbox,oEvent);
				}
				jQuery(oTextbox).attr("value","");
				jQuery(oTextbox).css("color","#000");
				jQuery(oTextbox).css("font-style","normal");
				return false;
				break;
            default:
                F6.UI.AutoComplete.textboxReplaceSelect(oTextbox, String.fromCharCode(isIE ? oEvent.keyCode : oEvent.charCode));
                var iLen = oTextbox.value.length;
				if (jQuery(".HighLightBox > .error").length > 0) {
					jQuery(".HighLightBox > .error").hide();
				}
				if (jQuery(".AddItemPanel .error").length > 0) {
					jQuery(".AddItemPanel .error").hide();
				}
                if ( iLen >= 3 ) {
                    var sMatch = F6.UI.AutoComplete.match(oTextbox.value, arrValues);

                    if (sMatch != null) {
                       oTextbox.value = sMatch;
                       F6.UI.AutoComplete.textboxSelect(oTextbox, iLen, oTextbox.value.length);
                    }  
                }

                return false;
        }
    }
