﻿function AutoComplete(elementId, popupContainerId, searchButtonId, ajaxUrl) 
{
    this.Element = jQuery('#' + elementId);
    this.PopupContainer = jQuery('#' + popupContainerId);
    this.SearchButton = jQuery('#' + searchButtonId);
    this.AjaxUrl = ajaxUrl;
    this.SelectedIndex = 0;
    this.Results = [];
    
    this.Element.attr('autocomplete', 'off');
    
    this.HandleTabHack = function(e)
        {
            var code = e.keyCode ? e.keyCode : e.which;
            if (code == 13 || code == 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)
                    {
                        jQuery.ajax({
                        type: "POST",
                            url:  this.AjaxUrl,
                            data: {searchTerm : this.Element.val()},
                            success : CreateDelegate(this, this.Update),
                            dataType: 'xml',
                            error:function (xhr, ajaxOptions, thrownError){
                                    //alert(xhr.status);
                                    //alert(thrownError);
                            }
                        });
                        
                    }
                    else
                        this.PopupContainer.hide();
                    break;
            }
        }
    this.Select = function(e)
        {
            this.Element.val(e.innerHTML);
            this.PopupContainer.hide();
            // this.SearchButton.click()
            return false;
        }
    this.Update = function(data)
        {
           
            //data = data.d;
          
            var list = this.PopupContainer.find('UL');
            
            list.empty();
            
            if (jQuery("SearchProduct", data).length > 0)
            {
                jQuery("SearchProduct", data).each(function(i) 
                { 
                    var node = jQuery(this);
                    
                    var name_text = node.find('Name').text();
                    var link_text = node.find('Link').text().replace('//','/');
                    var link_description = node.find('Description').text();
                    
                    jQuery("<li><a href='" + escape(link_text) + "'>" + name_text + " - " + link_description + "</a></li>").appendTo("#SearchAutoCompletePopup ul");
                });
                this.PopupContainer.show();
            }else{
                this.PopupContainer.hide();
            }
                         
            this.Results = this.PopupContainer.find('li');
            this.SelectedIndex = -1;
                
        }
    this.UpdateSelectedIndex = function()
        {
            if (this.SelectedIndex > -1)
            {
                this.Results.removeClass('Selected');
                this.Results.slice(this.SelectedIndex, this.SelectedIndex + 1).addClass('Selected');
            }
        }

    this.Element.keyup(CreateDelegate(this, this.HandleKeyPress));
    this.Element.keypress(CreateDelegate(this, this.HandleTabHack));
}

