$(function () {
    
    /**
     * Navigation menu
     */
    $("ul.menu a").hover(function () {
        $(this).closest("li").addClass("over");
    }, function () {
        $(this).closest("li").removeClass("over");
    });
    
    
    /**
     * Scrollable panel
     */
    $("div.scroller").each(function () {
        var nav = $("em.nav", this),
            items = $("li", this),
            scroll = $("ul", this),
            width = 0;
        
        items.each(function () {
            width = width + $(this).outerWidth(true);
        });
        
        scroll.width(width).css("left", 0);

        nav.bind("mousedown", function () {

            var dir, pos, pWidth;

            // block multiple triggers while animating
            if (scroll.filter(":animated").length > 0) {
                return;
            }

            dir = $(this).hasClass("nav-next") ? -1 : $(this).hasClass("nav-prev") ? 1 : 0;

            pWidth = scroll.parent().width();

            pos = parseInt(scroll.css("left"), 10) + dir * items.eq(0).outerWidth(true);

            if (pos + width < pWidth) {
                pos = pWidth - width;
            }
            if (pos > 0) {
                pos = 0;
            }
            
            scroll.animate({"left": pos});

            /** show/hide buttons **/
            nav.eq(0)[pos < 0 ? "removeClass" : "addClass"]("hidden");
            nav.eq(1)[pos + width > pWidth ? "removeClass" : "addClass"]("hidden");

        }).eq(0).triggerHandler("mousedown");
    });
    
    
    /**
     * Overlay
     */
    $("div.misc div.more a").bind("click", function () {
        $("#overlay").removeClass("hidden");
        return false;
    });
    $("#overlay div.close").bind("click", function () {
        // fix layout issue in Opera
        $("#overlay").css({"position": "static", "height": 0});
        setTimeout(function () {
            $("#overlay").css({"position": "", "height": ""}).addClass("hidden");
        }, 0);
    });
    
});
