(function($)
{
    var Modal = function( element )
    {
        var _overlay = $('<div id="overlay">'),
            _modal   = $(element);

        _overlay.appendTo('body').css({
            display:'none',
            top: 0, left: 0
        }).fadeIn();

        _modal = $(element).css('display', 'none');

        updatePos();

        _modal.fadeIn();

        $(window).resize( updatePos );
        
        $('.close', _modal).click(function(e)
        {
            e.preventDefault();
            _overlay.fadeOut();
            _modal.fadeOut();
        });

        function updatePos()
        {
            _modal.css({
                left: $(window).width() / 2 - _modal.outerWidth() / 2,
                top: $(window).height() / 2 - _modal.outerHeight() / 2
            });
        }
    }

    $.fn.modal = function()
    {
        new Modal( this );return $(this);
    }
})(jQuery);
