﻿var clickcount = 0;
var maxcount = 3;

$(document).ready(function () {

    //
    // Browser specific
    //
    if (jQuery.browser.msie && parseInt(jQuery.browser.version) < 7) {
        $("#divFooter").css("position", "static");
    } else {
        $("#divFooter").css("bottom", "1px").css("bottom", "0px");
        $(window).resize(function (s) { $("#divFooter").css("bottom", "1px").css("bottom", "0px"); });
    }

    $("#chkEbay").click(function () {
        if ($("#chkEbay").attr("checked") || $("#chkEbay").attr("checked") == "checked") {
            $("#cse-search-box").attr("action", "/Search");
            $("#cse-search-box").attr("method", "POST");
            //$("#txtSearch").attr("name", "query");
        }
    });

    $("#chkGoogle").click(function () {
        if ($("#chkGoogle").attr("checked") || $("#chkGoogle").attr("checked") == "checked") {
            $("#cse-search-box").attr("action", "http://www.google.com/cse");
            $("#cse-search-box").attr("method", "GET");
            // $("#txtSearch").attr("name", "q");

        }
    });

    //
    // EBAY STUFF
    //
    if (!$("#noads").hasClass("hidden")) { setTimeout(function () { $("#noads").slideUp(); }, 4000); }
    $("#main").css("min-height", ($("#leftContent").outerHeight() > 500) ? $("#leftContent").outerHeight() : 500);
    $("a.ebay").click(function () { var href = $(this).attr("href"); clickcount += 1; if (clickcount > maxcount) { href = 'http://cgi.ebay.com/' + $(this).attr("itemid"); } var width = $(window).width(); var height = $(window).height(); var win = window.open(href, '_' + new Date().valueOf(), 'scrollbars=1,directories=0,resizable=1,menubar=0,left=' + ((width > 1000) ? ((window.screenLeft) ? window.screenLeft : window.screenX) + ((width - 1000) / 2) : '') + ',width=' + ((width > 1000) ? 1000 : width) + ', height=' + height); return false; });
    $("a.news").click(function () { var width = $(window).width(); var height = $(window).height(); var win = window.open($(this).attr("href"), '_' + new Date().valueOf(), 'scrollbars=1,directories=0,resizable=1,menubar=0,left=' + ((width > 1040) ? ((window.screenLeft) ? window.screenLeft : window.screenX) + ((width - 1040) / 2) : '') + ',width=' + ((width > 1040) ? 1040 : width) + ', height=' + height); return false; });
    $("a.rover").click(function () { var href = $(this).attr("href"); clickcount += 1; if (clickcount <= maxcount) { href = $(this).attr('data-rover'); } var width = $(window).width(); var height = $(window).height(); var win = window.open(href, '_' + new Date().valueOf(), 'scrollbars=1,directories=0,resizable=1,menubar=0,left=' + ((width > 1000) ? ((window.screenLeft) ? window.screenLeft : window.screenX) + ((width - 1000) / 2) : '') + ',width=' + ((width > 1000) ? 1000 : width) + ', height=' + height); return false; } );


    //
    // GOOGLE SEARCH STUFF
    //
    $(".txtSearch").keyup(function (e) { googleSuggestXml(e) }); // use googleSuggestJsonp(e) for JSON
    //$("#googleSuggest").css("left", $(".txtSearch").offset().left).width($(".txtSearch").outerWidth() - 2);
    // changed the above line to the below linebecause page is auto-margined.
    $("#googleSuggest").css("left", $(".txtSearch").offset().left - (($("#page").length > 0) ? $("#page").offset().left : 0)).width($(".txtSearch").outerWidth() - 2);
    $("#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.
    // OVERRIDE GOOGLE COLOR
    $(".txtSearch").css("border", "solid 3px #49F"); // 0bf

    //
    // ERROR DISPLAY AT TOP
    //
    // use bullets if more than one error
    $(".validation-summary-errors ul").css("list-style-type", function () { if ($(this).find("li").length > 1) { return 'disc'; } else { return 'none'; } });
    // if it's a browser that doesn't center the ul correctl by the css file's margin: auto, we'll center it ourselves.
    if ($(".validation-summary-errors ul").length > 0 && $(".validation-summary-errors ul").offset().left < 20) { $(".validation-summary-errors ul").css("text-align", "center"); }
    // show the error
    var patt = /\S/g;
    if (patt.test($("#master_error").text())) { $("#master_error").show(); setTimeout(function () { $("#master_error").slideUp(400); }, 15000); }


    function getCookie(c_name) {
        if (document.cookie.length > 0) {
            c_start = document.cookie.indexOf(c_name + "=");
            if (c_start != -1) {
                c_start = c_start + c_name.length + 1;
                c_end = document.cookie.indexOf(";", c_start);
                if (c_end == -1) c_end = document.cookie.length;
                return unescape(document.cookie.substring(c_start, c_end));
            }
        }
        return "";
    }

    //
    // SHOW ERROR
    //
    function showError(error, interval) {
        $("#master_error").text(error).slideDown(200); if (interval && interval > 0) { setTimeout(function () { $("#master_error").slideUp(400); }, interval); }
    }


    /****************************************************/
    /*               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);
        }
    }






});
