﻿//
//  Simple Search (Header) for BDK
//

$(document).ready(function() {
    $('.search_header_text').click(function() {                     // When you click in the box, get rid of "Search..."
        if ($('.search_header_text').val() == 'Search...') $('.search_header_text').val('');
    });

    $("body").click(function() {                                    // Method to hide search results.
        $("ul#searchFlyout").hide();                                
    });

    $('.search_header_select').click(function() {                   // When the 'Submit' button is clicked.
        searchFly();
    });

    $('.search_header_text').bind('keypress', function(event) {     // When the 'Enter' key is pressed while in the search text box.
        var code = event.charCode || event.keyCode;
        if (code && code == 13) {
            searchFly();
            event.preventDefault(); 
        };
    });

    function searchFly() {                                          // Search results routine.
        var searchFly = '';
        var sString = encodeURI($('.search_header_text').val());
        if ($('.search_header_text').val().length > 0) {
            $.ajax({
                type: "post",
                url: "/WebServices/SiteSearch.asmx/GetSearchResults",
                data: "{ 'culture':'" + culture + "', 'currentPage':'1', 'limit':'5', 'itemsPerPage':'5', 'filterList':'BDK_2010', 'searchQuery':'" + $('.search_header_text').val() + "' }",
                dataType: "json",
                cache: true,
                contentType: "application/json; charset=utf-8",
                success: function(json) {
                    //alert(json.d);
                    var jObj = jQuery.parseJSON(json.d);
                    $('#headerSearchBar').append("<ul id=\"searchFlyout\"></ul>").html();
                    if (jObj[1].length > 0) {
                        var menu = jObj[1];
                        i = 0;
                        $(menu).each(function() {
                            var img = '';
                            var cls = '';
                            if (menu[i].ItemImg) img = "<img src='" + menu[i].ItemImg + "' class='srchImg'/>";
                            if (i % 2 == 1) cls = 'class="odd"';
                            searchFly += "<li "+cls+"><a href='" + menu[i].Url + "'>" + img + "<h4>" + menu[i].Title + "</h4><p>" + menu[i].Summary + "</p></a></li>";
                            i++;
                        });
                        searchFly += "<li><p><a href='/Search.aspx?terms=" + sString + "'>View more results</a> &raquo;</p></li>";
                    } else {
                        searchFly += "<li><p>No items found.</p></li>";
                    }
                    searchFly += "<li class='botList'></li>";
                    $("ul#searchFlyout").html(searchFly);
                },
                error: function(response) {
                    alert("JSON Error Response:\r\n" + response.responseText);
                }
            });
        } else {
            alert("Please enter search terms and try again.");
        }
    }
});
