



jQuery(document).ready(function() {

    jQuerySiteSearch  = jQuery("#p_lt_PlaceHolder_PagePlaceholder_PagePlaceholder_lt_Sidebar_SearchBox_txtWord");
    jQueryPlantSearch = jQuery("#p_lt_PlaceHolder_PagePlaceholder_PagePlaceholder_lt_Search_SearchBox_txtWord");



 // http://www.pengoworks.com/workshop/jquery/autocomplete.htm
    var findValue = function(li) {
        if( li == null ) return true;
        // if coming from an AJAX call, let's use the CityId as the value
        if( !!li.extra ) var sValue = li.extra[0];
        // otherwise, let's just display the value in the text box
        else var sValue = li.selectValue;
        //alert("The value you selected was: " + sValue);
        if (sValue.length > 0 ) {
            //alert(location.href);
            location.href = sValue;
            return true;
        } else {
            return false;
        }
    }

    var selectItem = function(li) {
        findValue(li);
    }

    var formatItemPlantSearch = function(row) {
        var search = jQueryPlantSearch.val();
        return row[0].replace(search, "<b>"+ search +"</b>");
    }

    var formatItemSiteSearch = function(row) {
        var search = jQuerySiteSearch.val();
        return row[0].replace(search, "<b>"+ search +"</b>");
    }

    function lookupAjax(jQuerySearchBox){        var oSuggest = jQuerySearchBox[0].autocompleter;	    oSuggest.findValue()	    return false;    }
    jQuerySiteSearch.autocomplete(
        '/scripts/AutoCompleteHandler.ashx',
        {
            delay:10,
            minChars:1,
            matchSubset:1,
            matchContains:1,
            cacheLength:10,
            onItemSelect:selectItem,
            onFindValue:findValue,
            formatItem:formatItemSiteSearch,
            sortResults:false,
            autoFill:false,
            extraParams: {area: "all"}
        }
    );

    jQueryPlantSearch.autocomplete(
        '/scripts/AutoCompleteHandler.ashx',
        {
            delay:10,
            minChars:1,
            matchSubset:1,
            matchContains:1,
            cacheLength:10,
            onItemSelect:selectItem,
            onFindValue:findValue,
            formatItem:formatItemPlantSearch,
            sortResults:false,
            autoFill:false,
            extraParams: {area: "plants"}
        }
    );

    jQuerySiteSearch.keyup(function(e) {
        if(e.keyCode == 13) {
            lookupAjax(jQuerySiteSearch);
            e.stopPropagation();
        }
    });    

    jQueryPlantSearch.keyup(function(e) {
        if(e.keyCode == 13) {
            lookupAjax(jQueryPlantSearch);
            e.stopPropagation();
        }
    });    
});
