<!--
var currentSelection = -1;
var currentUrl = "";

$(document).ready(function ()
{
    BindSearch();
    //var prm = Sys.WebForms.PageRequestManager.getInstance();
    //prm.add_endRequest(BindSearch);

    // Username validation logic
    var results = $('#results');
    var spinner = $('#spinner');

    
    $('#search').keyup(function (event)
    {
        //check for up/down arrow
        if( event.keyCode == '40' )
        {
            SearchNavigate('down');                       
        }
        else if( event.keyCode == '38' )
        {
            SearchNavigate('up');                       
        }   
        else if( event.keyCode == '13' )
        {
            if( currentSelection != -1 )
            {
                if(currentUrl != '') 
                {
                    window.location = currentUrl;
                }                
            }
        }  

        // cache the 'this' instance as we need access to it within a setTimeout, where 'this' is set to 'window'
        var t = this; 
        
        // only run the check if the username has actually changed - also means we skip meta keys
        if (this.value != this.lastValue)
        {
            // the timeout logic means the ajax doesn't fire with *every* key press, i.e. if the user holds down
            // a particular key, it will only fire when the release the key.
                            
            if (this.timer) clearTimeout(this.timer);
            if (this.value == "")
            {
                results.stop().fadeTo(400, 0, function() { $('#results').hide() });
                results.html("");
                spinner.html("");

                //clear navigation index and current url
                currentSelection = -1;
                currentUrl = "";
            }
            else
            {
                // show our holding text in the validation message space
                spinner.html('<img src="' + BasePath + 'jq/ajax-loader.gif" height="16" width="16" alt="Loading..." />');
                // fire an ajax request in 1/5 of a second
                this.timer = setTimeout(function () {
                    $.ajax({
                        url: BasePath + 'search_function.aspx',
                        data: 'action=search&value=' + t.value,
                        type: 'post',
                        cache: false,
                        success: function (html) {
                            results.stop().show().fadeTo(250, 1);
                            results.html(html);
                            spinner.html("");
                            BindResultsHover();  //bind our results hover listener to the new results
                        }
                    });
                }, 200);
            }
            
            // copy the latest value to avoid sending requests when we don't need to
            this.lastValue = this.value;

        }
    });
});

function SearchNavigate(direction) 
{         
    //remove hover styles in case they were hovering with their mouse
    $("#results ul li").removeClass("resultsSelect");

    if(direction == 'up' && currentSelection != -1) 
    {
        if(currentSelection != 0) 
        {
            currentSelection--;
        }
    } 
    else if (direction == 'down') 
    {
        if(currentSelection != $("#results ul li").size() -1) 
        {
            currentSelection++;
        }
    }
    setSearchItemSelected(currentSelection);
}

function setSearchItemSelected(selectedIndex)
{
    //remove selected class from all list items
    $("#results ul li").removeClass("resultsSelect");

    //now set the selected class on the selected one
    $("#results ul li").eq( selectedIndex ).addClass("resultsSelect");

    currentUrl = $("#results ul li").eq(selectedIndex).find("a").attr("href");

}

function BindSearch()
{
    $("#results").hover(
        function(){
            $(this).stop().show().fadeTo(250, 1);
        },
        function(){
            $(this).stop().fadeTo(900, 0, function() { 
                $(this).hide();                
                currentSelection = -1;
                currentUrl = ""; });
        }); 
    
}

function BindResultsHover()
{
    //add a data element called number so that when they hover with the mouse we can determine which one their hovering over
    for(var i = 0; i < $("#results ul li").size(); i++) {
        $("#results ul li").eq(i).data("number", i);
    }

    //add hover listener to list items so that we can change class/style when hovering with mouse
    //we do this because we need to remove that class when using the arrow keys and
    //can't remove hover subclass in javascript
    $("#results ul li").hover(
        function(){
             currentSelection = $(this).data("number");
             setSearchItemSelected(currentSelection);
        },
        function(){
            $("#results ul li").removeClass("resultsSelect");
            currentUrl = "";
        }); 
}


//-->

