$(function(){
    $("body").addClass("js-on");

    // A - Z Sorting
    $(".alpha-sort a").click(function(e){
        e.preventDefault();
        var term = $(this).text();
        var $sites = $(".sites section");

        $sites.show().filter(function() {
            var text = $(this).find(".site-header").text().replace(/\s+/g, " ").toLowerCase().substr(1,1);
            return !~text.indexOf(term);
        }).hide();

        if ( ! $(".site-block").is(":visible")) {
            $(".no-results").show();
        }
    });

    // Difficulty sorting
    $(".diff-sort a").click(function(e){
        e.preventDefault();
        var term = $(this).text().toLowerCase();
        var $sites = $(".sites section");

        $sites.show().filter(function() {
            var text = $(this).find(".site-difficulty").text().replace(/\s+/g, " ").toLowerCase();
            return !~text.indexOf(term);
        }).hide();

        if ( ! $(".site-block").is(":visible")) {
            $(".no-results").show();
        }
    });

    // Popular sorting
    $("button.popular").click(function(e){
        e.preventDefault();
        var term = "popular";
        var $sites = $(".sites section");

        $sites.show().filter(function() {
            var text = $(this).find(".meta").text().replace(/\s+/g, " ").toLowerCase();
            return !~text.indexOf(term);
        }).hide();

        if ( ! $(".site-block").is(":visible")) {
            $(".no-results").show();
        }
    });

    // Clear search and sorting
    $("button.reset").click(function(e){
        var $sites = $(".sites section");
        $sites.show();
        $(".no-results").hide();
        $("input").val("");
    });

    // When the search field changes, update the hash
    var hashUpdateTimer;
    $("input").on("input", function(){
        window.clearTimeout(hashUpdateTimer);
        hashUpdateTimer = setTimeout(setWindowHash, 250, $(this).val());
    });

    function setWindowHash(value) {
        window.location.hash = value;
    }

    function getDecodedWindowHash() {
        return decodeURIComponent(window.location.hash);
    }

    // Call updateSearch when hash changes
    $(window).on("hashchange", function() {
        updateSearch();
    });

    // Search function
    function updateSearch() {
        if ($(".no-results").is(":visible")) {
            $(".no-results").hide();
        }

        var hash = getDecodedWindowHash();
        var term = hash.substr(1);
        var $sites = $(".sites section");

        $sites.show().filter(function() {
            var siteHeader = $(this).find(".site-header")[0];
            var siteTitle = siteHeader.innerText.trim().toLowerCase();
            var siteUrl = siteHeader.href.toLowerCase();
            var lowerTerm = term.toLowerCase();

            // returns true if lowerTerm isn't found in site title or URL
            return Math.max(siteTitle.indexOf(lowerTerm), siteUrl.indexOf(lowerTerm)) == -1;
        }).hide();

        if ( ! $(".site-block").is(":visible")) {
            $(".no-results").show();
        }

        // Insert the term into the search field
        // (sometimes this is missed if the hash is changed directly)
        $("#search").val(term);
    }

    // Update search results on page load (if there is a hash)
    if (getDecodedWindowHash() && getDecodedWindowHash() !== "#") {
        // Insert the term into the field
        $("#search").val(getDecodedWindowHash().substr(1));

        // Update the results
        updateSearch();
    }

    $(".site a").prop("title", "");

    // jQuery ScrollTo plugin from http://lions-mark.com/jquery/scrollTo/

    $.fn.scrollTo = function( target, options, callback ){
        if(typeof options == "function" && arguments.length == 2){ callback = options; options = target; }
        var settings = $.extend({
            scrollTarget  : target,
            offsetTop     : 50,
            duration      : 500,
            easing        : "swing"
        }, options);
        return this.each(function(){
            var scrollPane = $(this);
            var scrollTarget = (typeof settings.scrollTarget == "number") ? settings.scrollTarget : $(settings.scrollTarget);
            var scrollY = (typeof scrollTarget == "number") ? scrollTarget : scrollTarget.offset().top + scrollPane.scrollTop() - parseInt(settings.offsetTop);
            scrollPane.animate({scrollTop : scrollY }, parseInt(settings.duration), settings.easing, function(){
                if (typeof callback == "function") { callback.call(this); }
            });
        });
    }

    // Banner scroll to bottom

    $(".banner").click(function(e) {
        e.preventDefault();
        $("body,html").scrollTo(".banner-block");
    });

    $(".info").click(function(e) {
        e.preventDefault();
        $("body,html").scrollTo(".about");
    });

    // create the keys and konami variables
    var keys = [],
    konami = "38,38,40,40,37,39,37,39,66,65";

    $(document).keydown(function(e){
        keys.push(e.keyCode);

        // and check to see if the user has entered the Konami code
        if (keys.toString().indexOf(konami) >= 0) {

            // do something such as:
            (function(){
                $(".site-block:first").after("<section class=\"site-block impossible\"><a class=\"site-header\" href=\"http://www.nsa.gov/\">NSA</a><p class=\"site-difficulty\">Difficulty: impossible</p><p class=\"tooltip-toggle\">No Info Available</p></section>"
                    +"<section class=\"site-block impossible\"><a class=\"site-header\" href=\"http://www.gchq.gov.uk/Pages/homepage.aspx\">GCHQ</a><p class=\"site-difficulty\">Difficulty: impossible</p><p class=\tooltip-toggle\">No Info Available</p></section>");
            })();

            // and finally clean up the keys array
            keys = [];
        }
    });
});

// Load Facebook after page load
window.onload = function() {

    $("#search").focus();

    (function(d, s, id) {
        var js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)) return;
        js = d.createElement(s); js.id = id;
        js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1";
        fjs.parentNode.insertBefore(js, fjs);
    }(document, "script", "facebook-jssdk"));

    console.log("Welcome to JustDeleteMe. We currently have " + $(".site-block").length + " services listed.");
};

@@ -4858,1010 +4858,8 @@
  %7D);
-%0A%0A    // create the keys and konami variables%0A    var keys = %5B%5D,%0A    konami = %2238,38,40,40,37,39,37,39,66,65%22;%0A%0A    $(document).keydown(function(e)%7B%0A        keys.push(e.keyCode);%0A%0A        // and check to see if the user has entered the Konami code%0A        if (keys.toString().indexOf(konami) %3E= 0) %7B%0A%0A            // do something such as:%0A            (function()%7B%0A                $(%22.site-block:first%22).after(%22%3Csection class=%5C%22site-block impossible%5C%22%3E%3Ca class=%5C%22site-header%5C%22 href=%5C%22http://www.nsa.gov/%5C%22%3ENSA%3C/a%3E%3Cp class=%5C%22site-difficulty%5C%22%3EDifficulty: impossible%3C/p%3E%3Cp class=%5C%22tooltip-toggle%5C%22%3ENo Info Available%3C/p%3E%3C/section%3E%22%0A                    +%22%3Csection class=%5C%22site-block impossible%5C%22%3E%3Ca class=%5C%22site-header%5C%22 href=%5C%22http://www.gchq.gov.uk/Pages/homepage.aspx%5C%22%3EGCHQ%3C/a%3E%3Cp class=%5C%22site-difficulty%5C%22%3EDifficulty: impossible%3C/p%3E%3Cp class=%5Ctooltip-toggle%5C%22%3ENo Info Available%3C/p%3E%3C/section%3E%22);%0A            %7D)();%0A%0A            // and finally clean up the keys array%0A            keys = %5B%5D;%0A        %7D%0A    %7D);
 %0A%7D);

Remove Konami code easter egg (#175)