(function ($) {
    function positionIt() {
        $('#popit').css({'top': Math.round($(window).height()/2 - $("#popit").height()/2) + 'px', 'left': Math.round($(window).width()/2 - $("#popit").width()/2) + 'px'});
    }
    
    $.fn.popit = function () {
        $(this).click(function () {
            var url = $(this).attr('href');
            $("#popit, #popit_background").remove();
            $('body').append('<div id="popit"></div><div id="popit_background"></div>');
            $('#popit_background').css({'position': 'absolute', 'top': 0, 'left': 0, 'width': $("body").outerWidth() + 'px', 'height': $("body").outerHeight() + 'px', 'background': '#000', 'opacity': 0.7, 'z-index': 1000});
            $('#popit').html('<a href="#" id="close_popit">close [x]</a><img src="' + url + '" />');
            $("#close_popit").css({'display': 'block', 'position': 'absolute', 'right': '20px', 'clear': 'both'});
            $('#close_popit, #popit_background').click(function () { $("#popit, #popit_background").fadeOut('normal', function () { $("#popit, #popit_background").remove(); });});
            $('#popit').css({'position': 'absolute', 'z-index': 1001, 'padding': '20px', 'border': '2px #343232 solid', 'background': '#fff'});
            positionIt();
            $('#popit img').load(function () { positionIt(); });
            return false;
        });
    };
})(jQuery);
