jQuery(function ($) {
    var popup = $("#popup"),
        popupScreen = $("#popup, #screen"),
        screen = $("#screen"),
        $doc = $(document),
        $win = $(window);
        
    function hideAll() {
        popupScreen.stop().fadeTo(200, 0, function(){
            popupScreen.css({ display : 'none' });
        });
    }

    popupScreen.click(function(e){
        e.preventDefault();
        hideAll();
    });
    
    $('.image-thumb').live('click', function(e){
        var $this = $(this),
            pos = $this.offset(),
            tgtwidth = $this.data('width'),
            tgthref = $this.attr('href'),
            tgtheight = $this.data('height'),
            img = $('<img>'),
            tgttop = ($win.height() - tgtheight) / 2;
        popup.html('').css({ 
            top : pos.top, 
            left : pos.left,
            width : $this.width(),
            height : $this.height(),
            'margin-left' : 0,
            display : 'block'
        });
        popup.stop().animate({ 
            top : tgttop + $doc.scrollTop(), 
            left : $win.width() / 2,
            'margin-left' : (-tgtwidth / 2) - 10,
            width : tgtwidth,
            height : tgtheight,
            opacity : 1
        }, 200, 'linear', function(){
            popup.append(img);
            img.attr('src', tgthref).css({ opacity : 0 }).animate({ opacity : 1 });
        });
        screen.stop().css({ display : 'block' }).animate({ opacity : 1 }, 200);
        e.preventDefault();
    });
});;

