﻿function submitSearch() {
    if ($(".tbHeaderSearchBox").val() != "Enter Broogle Search Term") {
        
        //document.location.href = "http://www.byreferralonly.com/memSearch.aspx?q=" + $(".tbHeaderSearchBox").val();
        return true;
    }
    else {
        return false;
    }
};


$(document).ready(function () {
//    $(".tbHeaderSearchBox").keypress(function (event) {
//        if (event.keyCode == 13) {
//            //document.location.href = "http://www.byreferralonly.com/memSearch.aspx?q=" + $(".tbHeaderSearchBox").val();
//            return true;
//        }
//        else {
//            return true;
//        }
//    });
    
    $(".tbHeaderSearchBox").keyup(function (event) {
        if (event.keyCode == 40) { // key 40 down
            if ($(".ItemHover").attr("class") == "ItemHover") {
                var item = $(".ItemHover");

                if ($(item).attr("class") == $(".Itemlist li:last").attr("class")) {
                    $(".Itemlist li:first").removeClass().addClass("ItemHover");
                    $(item).removeClass().addClass("Item");
                }
                else {
                    $(item).next().removeClass().addClass("ItemHover");
                    $(item).removeClass().addClass("Item");
                }
            }
            else {
                $(".Itemlist .Item:first").removeClass().addClass("ItemHover");
            }
            $(this).val($(".ItemHover").html());
            $(this).focus();
        }
        else if (event.keyCode == 38) { //key 38: up
            if ($(".ItemHover").attr("class") == "ItemHover") {
                var item = $(".ItemHover");

                if ($(item).attr("class") == $(".Itemlist li:first").attr("class")) {
                    $(".Itemlist li:last").removeClass().addClass("ItemHover");
                    $(item).removeClass().addClass("Item");
                }
                else {
                    $(item).prev().removeClass().addClass("ItemHover");
                    $(item).removeClass().addClass("Item");
                }
            }
            else {
                $(".Itemlist .Item:last").removeClass().addClass("ItemHover");
            }
            $(this).val($(".ItemHover").html());
            $(this).focus();
        }
        else if (event.keyCode == 13) { //key
           // document.location.href = "http://www.byreferralonly.com/memSearch.aspx?q=" + $(".tbHeaderSearchBox").val();
            return submitSearch();
        }
        else {
            //google.Suggestions($(this).val(), SucceededCallback, FailCallback, "")

            $.ajax({
                type: "POST",
                url: "/webservices/google.asmx/Suggestions",
                contentType: "application/json; charset=utf-8",

                data: "{'text':'" + $(this).val() + "'}",
                success: function (msg) {
                    SucceededCallback(msg.d);
                }
            });
        }

    });
});

function SucceededCallback(e) {
    $(".suggestionHeaderBox .Itemlist").html("");

    var url = "";
    var html = "";

    var data = eval("(" + e + ")");

    if (data.results.length > 0) {
        $.each(data.results, function(count, item) {
            var dontAdd = false;
            for (var x = count; x >= 0; x--) {
                if (data.results[x].name == item.name.replace("daterange:2003-01-01..", "")) {
                    dontAdd = true;
                    break;
                }
            }
            if (!dontAdd) {
                $("<li/>").html(item.name.replace("daterange:2003-01-01..", "")).attr("class", "Item").appendTo(".suggestionHeaderBox .Itemlist");
            }
        });

        $(".suggestionHeaderBox").css("display", "block");
        $(".Item").mouseenter(function() {
            $(".ItemHover").removeClass().addClass("Item");
            $(this).removeClass().addClass("ItemHover");
            $(".tbHeaderSearchBox").val($(".ItemHover").html());
            $(".tbHeaderSearchBox").focus();
        });
        $(".suggestionHeaderBox .Itemlist li").click(function() {
            //alert("hi");
        $("#btnHeaderSearch").click();
        });
    }
}
function FailCallback(result) {
    // alert(result);
}
