$(document).ready(function () {

    $.fn.extend({
    
        popup: function () {
            
            var $this, $popupMask, $popupWindow, $popupClose;
            
            $this = $(this).clone();
            $popupMask = $('<div />').addClass('popup-mask').appendTo('body');
            $popupWindow = $('<div />').addClass('popup-window').appendTo('body');
            $popupClose = $('<div />').addClass('popup-close').text('x Close').appendTo($popupWindow);
            
            $this.appendTo($popupWindow);
        
            $this.extend({
            
                render: function () {
                    
                    //Center in window
                    $popupWindow.css({
                        left: (($(window).width() / 2) - ($popupWindow.outerWidth() / 2)),
                        top: (($(window).height() / 2) - ($popupWindow.outerHeight() / 2))
                    });
                    
                    //Fill window
                    $popupMask.css({
                        width: $(window).width(),
                        height: $(window).height()
                    });
                    
                    return $this;
                },
                
                close: function () {
                    
                    $this.remove();
                    $popupWindow.remove();
                    $popupMask.remove();
                    
                }
            
            });
            
            $popupWindow.find('.popup').show();
                        
            $this.render(); //Initial center of popup window
            $(window).resize($this.render); //Center when the browser is resized    
                        
            $popupMask.click($this.close);
            $popupClose.click($this.close);
                                    
            return $this;        
        }
        
    });


	/*
    $('.popup').each(function () {
    
        var $popup, $link, popupId;
        
        //Define the popup
        $popup = $(this).appendTo('body');
        popupId = $popup.attr('id');
        
        //Define the link to activate the popup
        $link = $('a[rel=' + popupId + ']');
    
        //Show popup
        $link.click(function () {

            $popup.popup();
            
        });
    
    });
    */

});
