(function( $ ){

      $.fn.lightbox = function(options) {

            var settings = {
                  'width'           : 'auto',
                  'height'          : 'auto',
                  'close'           : null,
                  'afterClose'      : null
            };
            var background = $('<div class="transparent_black_background"></div>');
            background.appendTo(document.body);

            var box = $('<div class="lightbox"></div>');
            box.append($('<a style="font-size:12px;color:#666666;" href="#" class="lightbox-close">x | zamknij</a><div id="lightbox-content"></div>'));
            box.css('width', settings.width);
            box.css('height', settings.height);

            box.appendTo(document.body);

            function close(){
                  if (typeof settings.close == 'function'){
                        if (!settings.close()) return;
                  } else if (typeof settings.close == 'boolean' && !settings.close){
                        return;
                  }

                  background.fadeOut('fast', function(){
                    //    background.remove();
                  });
                  box.fadeOut('fast', function(){
                      //  box.remove();
                  });
                  $(window).bind('resize', resizeFunction);


            }
            function resizeFunction(){
                  box.css('left', "0");
                  box.css('top', "0");
                
                  box.css('margin-left', parseInt(-box.outerWidth()/2 ) + 'px' );
                  box.css('margin-top', parseInt(-box.outerHeight()/2 ) + 'px' );
                  box.css('left', "50%");
                  box.css('top', "50%");
            }
            

            this.click(function(e){
                  e.preventDefault();
               ///  $(window).resize(resizeFunction);
                  box.find('div').html('');
                  href=$(this).attr('href');
                  
            //box.append($('<a href="#" class="lightbox-close" style="font-size:12px;color:#666666;">x | zamknij</a>'));


                     box.find('div').html('<img src="'+href+'" alt="" />');
                     /* box.find('div').load('/images/lightbox/'+id,function(){*/
                         box.find('div').find('img').load(function() {
                                                   resizeFunction()
                                             });
                                      background.fadeIn('fast',function(){
                                             
                                            
                                      });
                                   box.fadeIn('fast');
                                    
       //               });
                
                  
            });

             this.close = close;

        background.click(function(e){
            e.preventDefault();
            close();
        });
        $(".lightbox-close", box).click(function(e){
            e.preventDefault();
            close();
        });
      };

})( jQuery );

