$(function() {

    var siteUrl = "";

    /* Fix for full bredde på bakgrunnsbilde
     * @see  */
    var theWindow   = $(window),
    $bg             = $("#background"),
    aspectRatio     = $bg.width() / $bg.height();
    function resizeBg() {
        if ( (theWindow.width() / theWindow.height()) < aspectRatio ) {
            $bg
            .removeClass()
            .addClass('bgheight');
        } else {
            $bg
            .removeClass()
            .addClass('bgwidth');
        }
    }
    theWindow.resize(function() {
        resizeBg();
    }).trigger("resize");

    /*
     * Kobler menyen */
    var $menu = $("#menu"),
    $panelContainer = $("#panel-container"),
    lastActive = "start";

    //
    // Setter opp neste konsert
    //    $("#nextconsertplaceholder").load(siteUrl + "/wp-admin/admin-ajax.php", {action: "async_fetch_next_consert"});
    $.get(siteUrl + "/wp-admin/admin-ajax.php", {action: "async_fetch_next_consert"}, function( htmlResponse ) {
        $("#nextconsertplaceholder").replaceWith(htmlResponse);
    });

    //
    // Setter opp menyene
    $("#menu > li > a").click(function( event ) {

        var targetPanelId = $(this).attr('href'),
        $panel = $(targetPanelId),
        postId = $(this).attr("rel"),
        $placeHolder = $( targetPanelId ).children("div.content-placeholder");


        //
        // Hvis tom --> hent
        if( $placeHolder.length ) {
            $.get(siteUrl + '/wp-admin/admin-ajax.php', {
                action: "async_fetch",
                postId: postId
            }, function( htmlResponse ) {
                $placeHolder.replaceWith(htmlResponse);
                
                //
                // Setter opp lightbox på eventuelle bilder (uavhenig av seksjon)
                $panel.find('a[rel^="lightbox"]').lightbox({
                    fitToScreen: true,
                    resizeSpeed: 250,
                    imageClickClose: true
                });

            });
        }

        $panelContainer.children(":visible").hide();

        if( lastActive != targetPanelId ) {
            $( targetPanelId ).slideDown();
            lastActive = targetPanelId
        } else {
            $panel.slideUp();
            lastActive = "";
        }
        return false;
    });

});

