/**
 * Integrate fancybox plugin into the page.
 */
$(function() {
    $("img.preview-image").each(function() {
        var img = $(this);
        
        var src = img.attr("src").replace(/\/preview/, "");
        var a = $('<a href="' + src + '"></a>');
        
        img.wrap(a);
        
        if ($(this).parents(".image-group").length == 0) {
            $(this).parent().fancybox();
        }
    });

    var i = 0;
    
    $(".image-group").each(function() {
        $(this)
            .find("a > img.preview-image")
            .parent()
            .attr("rel", "image-group-" + i++)
            .fancybox();
    });
});