﻿
$(document).ready(function () {

    
    //
    // GOOGLE SEARCH STUFF
    //
  /*  $("#txtSearch").keyup(function (e) { googleSuggestJsonp(e) }); // use googleSuggestJsonp(e) for JSON
    $("#googleSuggest").css("left", $("#txtSearch").offset().left - (($("#page").length > 0) ? $("#page").offset().left : 0)).width($("#txtSearch").innerWidth());
    $("#googleSuggest").css("top", $("#txtSearch").offset().top - $("#googleSuggest").parent().offset().top + $("#txtSearch").outerHeight());
    $("#txtSearch").blur(function () { setTimeout(function () { $("#googleSuggest").empty().hide(); }, 1000); });  // setTimeout to cause delay so we can click on an item without triggering the blur actions first.
    $("#txtSearch").css("border", "solid 3px #49F"); // 0bf
    */

    //
    // SET INITIAL FOCUS ON SEARCH BOX
    //
   // $("#txtSearch").focus();

});


/****************************************************/
/*               GOOGLE SUGGEST                     */
/****************************************************/

function selectSuggestOption(c) {
    if ($("#googleSuggest").length == 0)
        return;

    var current;
    var next;
    var isUp = false;

    if (c == 38)
        isUp = true;

    current = $(".selectedSuggestion");

    if (current.length == 0) {
        if (isUp)
            next = $(".suggestOption:last");
        else
            next = $(".suggestOption:first");
    }
    else {
        if (isUp)
            next = $(".suggestOption").filter(".selectedSuggestion").prev();
        else
            next = $(".selectedSuggestion + .suggestOption");
    }

    $(".suggestOption").removeClass("selectedSuggestion");
    if ($(next).length > 0) {
        $(next).addClass("selectedSuggestion");
        $("#txtSearch").attr("value", ($(next).text()));
    }

}



/*  JSONP REQUEST
*****************/
function googleSuggestJsonp(e) {
    // sample search = // sample search =	 http://google.com/complete/search?callback=googleCallback&q=microsoft

    var c = e.keyCode;
    if (c == 8 || c == 27 || c == 32 || c == 43 || c == 46 || (c >= 48 && c <= 111) || c >= 186) {

        if (c == 27)
            $("#txtSearch").attr("value", "");
        var url = "http://google.com/complete/search?callback=googleSuggestCallback&q=" + encodeURIComponent($("#txtSearch").attr("value"));
        var script = document.createElement("script");
        script.setAttribute("src", url);
        script.setAttribute("type", "text/javascript");
        document.body.appendChild(script);
    } if (c == 40 || c == 38) {
        selectSuggestOption(c);
    }
}

/*
*  JSONP CALLBACK
*****************/
function googleSuggestCallback(results) {

    var out = "";
    if (results.length == 2 && results[1].length > 0) {
        for (var i = 0; i <= results[1].length - 1; i++) {
            var result = results[1][i][0];
            out += "<div class='suggestOption'>" + result + "</div>";
        }
    } else {

    }

    if (out != "") {
        var current;
        $("#googleSuggest").show().html(out);
        $(".suggestOption").hover(function () { $(".suggestOption").removeClass("selectedSuggestion"); $(this).addClass("selectedSuggestion"); }, function () { })
.click(function () { $("#txtSearch").attr("value", $(this).text()); $("#btnSearch").click(); });
    }
    else {
        $("#googleSuggest").empty().hide();
    }
}

/*
* XML REQUEST
**************/
function googleSuggestXml(e) {
    // sample search =	 http://google.com/complete/search?output=toolbar&q=microsoft
    var url = '/Proxy/GoogleSuggestXml/?q=' + encodeURIComponent($("#txtSearch").attr("value")); // or escape(str)

    var c = e.keyCode;
    if (c == 8 || c == 27 || c == 32 || c == 43 || c == 46 || (c >= 48 && c <= 111) || c >= 186) {

        if (c == 27)
            $("#txtSearch").attr("value", "");

        $.ajax({
            type: 'GET',
            url: url,
            dataType: "xml",
            success: function (xml, status) {
                var t = "";
                $(xml).find("CompleteSuggestion").each(function () {
                    t += "<div class='suggestOption'>"
+ $(this).find("suggestion").attr("data")
+ "</div>";
                });
                if (t != "") {
                    $("#googleSuggest").show().html(t);
                    $(".suggestOption").hover(function () { $(".suggestOption").removeClass("selectedSuggestion"); $(this).addClass("selectedSuggestion"); }, function () { })
.click(function () { $("#txtSearch").attr("value", $(this).text()); $("#btnSearch").click(); });
                }
                else {
                    $("#googleSuggest").empty().hide();
                }
            },
            error: function (a, b, c) {
                $("#googleSuggest").empty().hide();
            }
        });
    }
    if (c == 40 || c == 38) {
        selectSuggestOption(c);
    }
}



/****************************************************/
/*               Helper Functions                   */
/****************************************************/

function isLoadingLoaded(selector) {
    return (($(selector).find(".loaded").length > 0));
}

function loadHtml(selector, content, isAppend) {
    if (isAppend)
        $(selector).append(content);
    else
        $(selector).html(content);

    $(selector).removeClass("loading");

}


