﻿jQuery(document).ready(function() {
    jQuery('.txtSearch').keyup(function(e) {
        updateAutoSuggestions(e);
    });
});

function lbSeeAll_Click() {
    var webServiceUrl = '/DesktopModules/MDTSearchBarSkin/MDTSearchBarSkinWebService.asmx/GetAutoSuggestions';

    if (jQuery('.txtSearch').val() != "") {
        jQuery.ajax({
            type: 'POST',
            url: webServiceUrl,
            data: "{'SearchTerm': '" + jQuery('.txtSearch').val() + "', 'bSeeAll': 'true', 'TagResultsSetting': '" + jQuery('.hfTagResultsSetting').val() + "', 'FullTextResultsSetting': '" + jQuery('.hfFullTextResultsSetting').val() + "', 'URL': '" + jQuery('.hfURL').val() + "'}",
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function(msg) {
                // Call success function and pass "d"
                // property of the response from the web
                // service. It will be usable like a
                // standard info object
                jQuery('#SearchBarSuggestions').html(msg.d);
            },
            error: function(msg) {
                //alert(msg);
            }
        });
    }
};

function updateAutoSuggestions(e) {
    var KeyId = (window.event) ? event.keyCode : e.keyCode;

    switch (KeyId) {
        case 13:
            //alert('Enter');
            //return false;
            break;
        case 38:
            //alert('Arrow Up');
            var currentItem = jQuery('#SearchBarSuggestions').find('[selected=true]');

            if (currentItem.length > 0) {
                jQuery(currentItem).removeAttr('selected');
                jQuery(currentItem).removeClass("selected");
                currentItem = jQuery(currentItem).prev().prev();
            }

            jQuery(currentItem).attr('selected', 'true');
            jQuery(currentItem).addClass("selected");
            jQuery('.txtSearch').val(jQuery(currentItem).html());
            break;
        case 40:
            //alert('Arrow Down');
            var currentItem = jQuery('#SearchBarSuggestions').find('[selected=true]');
            if (currentItem.length == 0) {
                currentItem = jQuery('#SearchBarSuggestions a:first-child');                
            } else {
                jQuery(currentItem).removeAttr('selected');
                jQuery(currentItem).removeClass("selected");
                currentItem = jQuery(currentItem).next().next();
            }

            jQuery(currentItem).attr('selected', 'true');
            jQuery(currentItem).addClass("selected");
            jQuery('.txtSearch').val(jQuery(currentItem).html());
            break;
        default:
            if (jQuery('.txtSearch').val() != "") {
                jQuery('#SearchBarSuggestions').show();
                //document.getElementById("Suggestions").style.visibility = "visible";

                var webServiceUrl = '/DesktopModules/MDTSearchBarSkin/MDTSearchBarSkinWebService.asmx/GetAutoSuggestions';

                jQuery.ajax({
                    type: 'POST',
                    url: webServiceUrl,
                    data: "{\"SearchTerm\": \"" + jQuery('.txtSearch').val() + "\", \"bSeeAll\": \"false\", \"TagResultsSetting\": \"" + jQuery('.hfTagResultsSetting').val() + "\", \"FullTextResultsSetting\": \"" + jQuery('.hfFullTextResultsSetting').val() + "\", \"URL\": \"" + jQuery('.hfURL').val() + "\"}",
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                    success: function(msg) {
                        // Call success function and pass "d"
                        // property of the response from the web
                        // service. It will be usable like a
                        // standard info object
                        jQuery('#SearchBarSuggestions').html(msg.d);
                    },
                    error: function(msg) {
                        //alert(msg);
                    }
                });
            }
            else {
                jQuery('#SearchBarSuggestions').hide();
                //document.getElementById("Suggestions").style.visibility = "hidden";
                jQuery('#SearchBarSuggestions').html("");
            }

    }
    
};
