$(document).ready(function() {

    $("body").prepend("<div id='overlay' class='ebox_active' title='click to return'></div>");
    $("div#overlay").animate({
        opacity: 0
    },0);                                                                                     
    $("a.ebox_thumb").prepend("<div class='spinner'></div>");
    $(".spinner").animate({ opacity: 0 },0);                                                  
    $("a.ebox_thumb").click(function(){
        var myThumb = $(this).children("img");
        var myImage = $(this).attr("href");                                                           
        var topDistance = $(window).scrollTop();
        
        thumbWidth = myThumb.width();
        thumbHeight = myThumb.height();
        var thumbX = myThumb.offset().left;
        var thumbY = myThumb.offset().top;
        var bigWidth = $(this).attr("rev");
        var bigHeight = $(this).attr("rel");
        var windowWidth = $(window).width();
        var windowHeight = $(window).height();
        var docWidth = $(document).width();
        var docHeight = $(document).height(); 

        bigWidth *= 1;
        bigHeight *= 1;
        
        bigX = windowWidth - bigWidth;
        bigX /= 2;
        bigY = windowHeight - bigHeight;
        bigY /= 2;

        $(this).find(".spinner").addClass("active_spin").animate({ opacity: .5 });

        $("div#overlay").width(docWidth).height(docHeight).animate({
            opacity: .6
        });

        $("body").prepend("<img src='"+myImage+"' id='current_pic' class='ebox_active' title='click to return' />");
        $("img#current_pic").width(thumbWidth).height(thumbHeight).css({
            left: thumbX, top: thumbY
        }).animate({
            opacity: .01
        },0).bind('load', function(){
                $(this).animate({
                    width: bigWidth, height: bigHeight, left: bigX, top: bigY+topDistance, opacity: 1
                }, 'fast', function(){
                    $(".active_spin").animate({ opacity: 0 }).removeClass("active_spin");
                });                                  
                $(".ebox_active").click(function(){
                    $("div#overlay").animate({
                        opacity: 0 
                    },'normal').width(1).height(1);
                    $("#current_pic").animate({ 
                        width: thumbWidth, height: thumbHeight, left: thumbX, top: thumbY, opacity: 'hide' 
                    },function(){
                    $("#current_pic").remove();
                });                  
            });
        });                                  
        return false;
    });                                  
});                                  