$(document).ready(function(){
    var hiding_nav = {};
    $("#header ul.filter > li[class]").click(function() {
       var target = $(this).find("ul");
       if (target.is(":visible")) {
            target.slideUp(120);
        } else {
            target.slideDown(200);
        }
    }).mouseleave(function() {
        var target = $(this).find("ul");
        hiding_nav[$(this).attr("class")] = setTimeout(function() {
            target.slideUp(100);
        }, 800);
    }).mouseenter(function() {
        if (hiding_nav[$(this).attr("class")]) { clearTimeout(hiding_nav[$(this).attr("class")]); }
    });
    
    $("#header ul.filter > li > ul").mouseleave(function() {
        var target = $(this);
        hiding_nav[$(this).parent().attr("class")] = setTimeout(function() {
            target.slideUp(100);
        }, 800);
    }).mouseenter(function() {
        if (hiding_nav[$(this).parent().attr("class")]) { clearTimeout(hiding_nav[$(this).parent().attr("class")]); }
    });
    
    
    $("#header ul.filter li input.search").focus(function() {
        if ($(this).val() == "") {
            $(this).animate({'width': '150px'}, 300).next("input").delay(300).fadeIn(800);
        }
    }).blur(function() {
        if ($(this).val() == "") {
            var $this = $(this);
            $(this).next("input").fadeOut(300, function() {
                $this.animate({'width': '220px'}, 300);
            });
        }
    });
    
    $(document).keydown(function(e) {
        var target = e.target || e.srcElement;
        var nodeName = target.nodeName.toLowerCase();
        // if key is pressed while focussed in an input, don't do anything
        if (nodeName == "input" || nodeName == "textarea") { return; }
        switch(e.which) {
            // user presses the "p"
            case 65:
                if($(".west a").attr("href")) { window.location = $(".west a").attr("href") };
                break;
            // user presses the "n" key
            case 68:
                if($(".east a").attr("href")) { window.location = $(".east a").attr("href") };
                break;
            // user presses the "u" key
            case 87:
                if($(".north a").attr("href")) { window.location = $(".north a").attr("href") };
                break;
            // user presses the "d" key
            case 83:
                if($(".south a").attr("href")) { window.location = $(".south a").attr("href") };
                break;
        }
    });
});
