function forceRecalc() {
    // If we're in IE 6, force a recalc to position the background
    // graphic appropriately.
    if ($.browser.msie && $.browser.version == "6.0") {
        $("#main").css("height", "auto");
    }
}

function initSidebar() {
    // Have the Fancybox emerge from the Carousel, even if we launch
    // from the sidebar navigation. This may look bad if there are
    // multiple links in the Carousel with the same href.
    $("#left .fancybox-trigger a").click(function() {
        $("#main .fancybox a[href='" + $(this).attr("href") + "']").click();
        return false;
    });

    // Show the carousel
    $("#wrap").css('visibility', 'visible');
}

function onCarouselInit(carousel) {
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
}

function openDefaultAnswer() {
    if (window.location.hash) {
        var answer = window.location.hash.substr(1);
        $("#main .fancybox a[href='" + answer + "']").click();
    }
}

function startCarousel() {
    var config = {
        auto: 3,
        wrap: 'last',
        scroll: 1,
        animation: 400,
        initCallback: onCarouselInit
    };
    $('#mycarousel').jcarousel(config);
}
    
function startFancybox() {
    // Override FancyBox close method to redisplay the welcome graphic
    // when the box is closed.
    var fbClose = $.fn.fancybox.close;
    $.fn.fancybox.close = function() {
        fbClose();
        $(".welcome").css("background-image", "url(img/finn/welcome.jpg)");
        $(".welcome").css("width", 713);
    };

    $("li.fancybox a").fancybox({
        'hideOnContentClick': true
    }).click(function() {
        // Pick a background image at random. Change the 3 below to
        // whatever the maximum number of answer images is.
        var n = Math.floor(Math.random() * 3 + 1);
        $(".welcome").css("background-image", "url(img/finn/answer-" + n + ".jpg)");
        $(".welcome").css("width", 300);
        $("#fancy_nav").append("<a title='Permanent link to this answer' href='#" + escape($(this).attr("href")) + "'></a>");

        // Browser specific zindex fix
        jQuery.each(jQuery.browser, function(id, val) {
            if(id != jQuery.browser.msie) {
                $('#fancy_content').css('zIndex', 0);
            }
        });

        return true;
    });
}

$(document).ready(function() {
    startCarousel();
    startFancybox();
    initSidebar();
    forceRecalc();
    openDefaultAnswer();
});

// vim: set et ts=4 sw=4 cin nopaste ft=javascript:
