(function ($) {
    $.fn.smartSuggest = function (n) {

var o = {
            boxId: "%-suggestions", 
            classPrefix: "ss-", 
            timeoutLength: 500, 
            src: "", 
            resultsText: "$ z % ", 
            noResultsText: "Brak wyników.", 
            showEmptyCategories: false, 
            fillBox: false, 
            fillBoxWith: "primary", 
            executeCode: true, 
            showImages: true, 
            minChars: 2
        };
        
        var n = $.extend(o, n);
        n.boxId = n.boxId.replace("%", $(this).attr("id"));
        var p = "";
        var r;
        $(this).wrap("<div class=\"" + n.classPrefix + "wrap\"></div>");
        $(this).attr("autocomplete", "off");
        $(this).after("<ul class=\"" + n.classPrefix + "box\" id=\"" + n.boxId + "\" style=\"display: none;\"></ul>");
        var s = $(this);
        var t = $("#" + n.boxId);
        var u = null;
        s.keyup(function (m) {
            if (m.keyCode != 13 && m.keyCode != 9) {
                var q = s.val();
                if (q == "" || q.length < n.minChars) {
                    t.fadeOut();
                    unsetTimeout(u);
                } else {
                    if (u != null) {
                        unsetTimeout(u);
                    }
                    u = setTimeout(function () {
                        s.addClass(n.classPrefix + "input-thinking");
                        p = q;
                        $.getJSON(n.src + "?a=doSearch&q=" + q, function (g, h) {
                            if (h == "success") {
                                var k = "";
                                var l = false;
                                $.each(g, function (i, a) {
                                    if (a.data.length > 0) {
                                        l = true;
                                    }
                                });
                            if (!l) {
                                k += "<li class=\"" + n.classPrefix + "header\">" + "\n";
                                k += "<p class=\"" + n.classPrefix + "header-text\">" + n.noResultsText + "</p>" + "\n";
                                k += "<p class=\"" + n.classPrefix + "header-limit\">0 wyników</p>" + "\n";
                                k += "</li>";
                            } else {
                                $.each(g, function (i, c) {
                                    if (n.showEmptyCategories || !n.showEmptyCategories && c.data.length != 0) {
                                        var d = c.header.limit;
                                        var e = 0;
                                        k += "<li class=\"" + n.classPrefix + "header\">" + "\n";
                                        k += "<p class=\"" + n.classPrefix + "header-text\">" + c.header.title + "</p>" + "\n";
                                        k += "<p class=\"" + n.classPrefix + "header-limit\">" + n.resultsText.replace("%", c.header.num).replace("$", c.header.limit < c.data.length ? c.header.limit : c.data.length) + "</p>" + "\n";
                                        k += "</li>";
                                        var f = n.fillBox ? "document.getElementById('" + s.attr("id") + "').value = '%';" : "";
                                        $.each(c.data, function (j, a) {
                                            if (e < d) {
                                                var b = "<a href=\"";
                                                b += a.url != undefined ? a.url : "javascript: void(0);";
                                                b += "\" ";
                                                b += a.onclick != undefined ? " onclick=\"" + f.replace("%", a[n.fillBoxWith]) + (n.executeCode ? a.onclick : "") + "\" " : "";
                                                b += ">";
                                                k += "<li class=\"" + n.classPrefix + "result\">" + b + "\n";
                                                k += "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\"><tr>";
                                                k += a.image != undefined && n.showImages ? "<td width=\"40\"><img src=\"" + a.image + "\" /></td>" + "\n" : "";
                                                k += "<td>";
                                                k += "<p>";
                                                k += a.primary != undefined ? "<span class=\"" + n.classPrefix + "result-title\">" + a.primary + "</span><br />\n" : "";
                                                k += a.secondary != undefined ? "" + a.secondary + "" + "\n" : "";
                                                k += "</p>" + "\n";
                                                k += "</td>";
                                                k += "</tr></table>";
                                                k += "</a></li>" + "\n";
                                            }
                                            e++;
                                        });
                                    }
                                });
                        }
                        t.html(k);
                            t.css("position", "absolute");
                            t.css("top", s.offset().top + s.outerHeight());
                            t.css("left", s.offset().left);
                            t.fadeIn();
                            s.removeClass(n.classPrefix + "input-thinking");
                        }
                    });
            }, n.timeoutLength);
        }
    }
});
s.blur(function () {
    t.fadeOut();
});
s.focus(function () {
    if (s.val() == p && s.val() != "") {
        t.fadeIn();
    }
});
};
function unsetTimeout(a) {
    clearTimeout(a);
    a = null;
}
}(jQuery));

$(document).ready(function () {
    $("#search").smartSuggest({
        src: "/tools.php", 
        minChars: 1
    });   
        
    $(".show").click(function () {
        if ($(this).html() == "Show code") {
            $("pre." + $(this).attr("class").split(" ").slice(-1)).show();
            $(this).html("Hide code");
        } else {
            $("pre." + $(this).attr("class").split(" ").slice(-1)).hide();
            $(this).html("Show code");
        }
        $(this).blur();
    });
});
