﻿
function AutoComplete(elementId, popupContainerId, searchFunction) 
{
    this.Element = $('#' + elementId);
    this.PopupContainer = $('#' + popupContainerId);
    this.SearchFunction = searchFunction;
    this.SelectedIndex = 0;
    this.Results = [];
    
    this.HandleTabHack = function(e)
        {
            if (e.keyCode == 13 || e.keyCode == 9)
            {
                if (this.SelectedIndex > -1)
                {
                    this.Select(this.Results.filter('.Selected a').children()[0]);
                    return false;
                }
            }
        }
    this.HandleKeyPress = function(e)
        {
            var code = e.keyCode ? e.keyCode : e.which;
            switch (code)
            {
                case 38:        // Up
                    this.SelectedIndex --;
                    if (this.SelectedIndex < 0)
                        this.SelectedIndex = 0;
                    this.UpdateSelectedIndex();
                    break;
                case 40:        // Down
                    this.SelectedIndex ++;
                    if (this.SelectedIndex > this.Results.length - 1)
                        this.SelectedIndex = this.Results.length - 1;
                    this.UpdateSelectedIndex();
                    break;
                case 27:        // Escape
                    this.PopupContainer.hide();
                    this.SelectedIndex = -1;
                    this.UpdateSelectedIndex();
                    break;
                default:
                    if (this.Element.val().length >= 2)
                        SoundsLive.Web.Main.Services.AutoCompleteService.GetSuggestions(this.Element.val(), Function.createDelegate(this, this.Update));
                    else
                        this.PopupContainer.hide();
                    break;
            }
        }
    this.Select = function(e)
        {
            this.Element.val(e.innerHTML);
            this.PopupContainer.hide();
            eval(this.SearchFunction);
            return false;
        }
    this.Update = function(data)
        {
            var list = this.PopupContainer.find('ul');
            
            list.empty();
            for (var i = 0; i < data.length; i ++)
                list.append('<li><a href="#" onclick="return searchAutoComplete.Select(this);">' + data[i] + '</a></li>');
        
            this.Results = this.PopupContainer.find('li');
            this.SelectedIndex = -1;
            
            if (data.length > 0)
                this.PopupContainer.show();
            else
                this.PopupContainer.hide();
        }
    this.UpdateSelectedIndex = function()
        {
            if (this.SelectedIndex > -1)
            {
                this.Results.removeClass('Selected');
                this.Results.slice(this.SelectedIndex, this.SelectedIndex + 1).addClass('Selected');
            }
        }

    this.Element.keyup(Function.createDelegate(this, this.HandleKeyPress));
    this.Element.keypress(Function.createDelegate(this, this.HandleTabHack));
}
