$(document).ready(function() {

    // Activate the magnifier
    $(window).load(function() {
        var imageWidth = $('#centerpiece img').width();
        var imageHeight = $('#centerpiece img').height();
        $('#centerpiece img').magnify().height( ( parseInt( imageHeight * ( 390 / imageWidth ))) + 'px').width('390px').show();
    });
    
    // Consolodate the Picture List
    var hashes = new Array();
    var extraViewCount = 0;
    $('ul#picturelist li').each(function() {
        if ($(this).hasClass('currentGroup')) {
            $(this).clone().appendTo('ul#extraViews');
            extraViewCount++;
        }
        var hash = $(this).contents('a').attr('class');
        if ($.inArray(hash,hashes) > -1) {
            $(this).remove();
        }
        hashes.push(hash);
        if ($(this).is(':last') && extraViewCount > 1) { showExtraViews(); }
    });
    
    // Show the extra views, when they're ready
    function showExtraViews() {
        $('ul#extraViews li a img').each(function() {
            $(this).parent().parent().removeClass('currentGroup');
            var imageWidth = $(this).width();
            var imageHeight = $(this).height();
            $(this).removeAttr('width').removeAttr('height').height( ( parseInt( imageHeight * ( 35 / imageWidth ))) + 'px').width('35px');
            if ($(this).is(':last')) { 
                $('ul#extraViews').slideDown('fast').contents('li').fadeIn('fast');
            }
       });
    }
    
    // Use the description of the first element in the current group as the description for the current image
    if (!$('.comment').length) {
        var comment = $('ul#picturelist li.currentGroup span').html();
        $('#left h2').after('<div class="comment">'+comment+'</div>');
        $('ul#picturelist li.currentGroup span').remove();
    }
    
    // Hover Effect for thumbnails
    $('.thumb').each(function() {
        if (!$(this).hasClass('current') && !$(this).hasClass('currentGroup')) { $(this).css('opacity','0.4'); }
        });
    $('.thumb').hover(function() {
        $(this).animate({ opacity: 1 });
    }, function() {
        if (!$(this).hasClass('current') && !$(this).hasClass('currentGroup')) { $(this).animate({ opacity: 0.4 }); }
    });
    
    // Activate the Goose's Carousel
    $('ul#picturelist').gooseCarousel({ numberOfRows: 2, scrollInterval: 2 });

});