﻿$(document).ready(function() {

    $("#SearchBox").click(function() {
        if ($("#SearchBox").val() == "Search") $("#SearchBox").val('');
    });

    var SearchResults, BlogSearchResults = "";
    var p_count, b_count = 0;

    


//  $('#SearchBox').keyup(function(e) {

   //     var code = (e.keyCode ? e.keyCode : e.which);


//        if ($('#SearchBox').val().length > 0) {
//            $.getJSON("/Home/Search", { "SearchString": $('#SearchBox').val() }, function(data) {
//                SearchResults = ""; p_count = 0;
//                $.each(data, function(S_index, item) {
//                    SearchResults = SearchResults + '<a class="SearchResultLink" id="Link' + S_index + '" href="/Portfolio/Detail/' + item.ArticleId + '">' + item.ArticleTitle + '</a>';
//                    p_count = p_count + 1;
//                });
//            });

//            $.getJSON("/Blog/Search", { "SearchString": $('#SearchBox').val() }, function(data) {
//                BlogSearchResults = ""; b_count = 0;
//                var B_LinkId;
//                $.each(data, function(B_index, item) {
//                    B_LinkId = p_count + B_index;
//                    BlogSearchResults = BlogSearchResults + '<a class="SearchResultLink" id="Link' + B_LinkId + '" href="/Blog/Detail/' + item.BlogId + '">' + item.BlogTitle + '</a>';
//                    b_count = b_count + 1;
//                });
//            });
//        }

//        jQuery().ajaxStop(function() {
//            $('#SearchResults').html('');
//            $('#SearchResults').append($('#SearchResultsCloseHTML').html());

//            if (b_count > 0 || p_count > 0) {
//                $('#SearchResults').append('<a class="AllResultsLink" id="Link-1" href="/Search/' + $('#SearchBox').val() + '">Show all results (' + (p_count + b_count) + ')</a>');
//            }

//            if (p_count > 0) {
//                $('#SearchResults').append('<div class="SearchResultsSectionTitle">portfolio (' + p_count + ')</div>')
//                .append(SearchResults);
//            }

//            if (b_count > 0) {
//                $('#SearchResults').append('<div class="SearchResultsSectionTitle">blog (' + b_count + ')</div>')
//                .append(BlogSearchResults);
//            }

//            if (b_count == 0 && p_count == 0) {
//                $('#SearchResults').append('<div class="SearchResultsNoResults">No results found</div>');
//            }

//            $('#SearchResults').show();

//            $(".SearchResultsCloseBtn").click(function() {
//                $('#SearchResults').hide();
//                $("#SearchBox").val('Search');
//            });

//            $("#SearchResults").mouseleave(function() {
//                $('#SearchResults').hide();
//                $("#SearchBox").val('Search');
//            });

//        });

 //  });

  $('#SearchBox').keydown(function(e) {

    var code2 = (e.keyCode ? e.keyCode : e.which);

//        if (code2 == 27) {
//            $('#SearchResults').hide(); $("#SearchBox").val('Search');
//            $('body').focus(); return false;
//        }

      if (code2 == 13) {
           document.location = "/Search/?SearchTerm=" + encodeURI($("#SearchBox").val());
      }

//        if (code2 == 40) {

//            $('#SearchResults').find('a:first').next().focus();

//            $('#SearchResults a').keydown(function(e) {
//                var code3 = (e.keyCode ? e.keyCode : e.which);
//                if (code3 == 40) {
//                    e.preventDefault();
//                    var FocussedLink = $(this).attr('id').substr(4);
//                    FocussedLink++;
//                    $('#Link' + FocussedLink).focus();
//                    return false;
//                }
//                else if (code3 == 38) {
//                    var FocussedLink = $(this).attr('id').substr(4);
//                    FocussedLink--;
//                    $('#Link' + FocussedLink).focus();
//                    e.preventDefault(); return false;
//                }
//                else if (code3 == 27) {
//                    $('#SearchResults').hide(); $("#SearchBox").val('Search');
//                    $('body').focus(); return false;
//                }
              else {
                   $('#SearchBox').focus();
             }
//            });
//           return false;
//        }
 });
    
});
