 (function($) {
 
   $.fn.lightbox = function(div,settings,callback) {
		if(typeof settings == 'function'){
			callback = settings
		}
		var config = {
			'background': '#000',
			'close' : 'true',
			'opacity' : '75',
			'zindex' : '999',
			'top' : '50',
			'position' : 'absolute',
			'style' : 'none',
			'animate' : false,
			'speed' : '5000',
			'nolight' : false
		};
		if (settings) $.extend(config, settings);

		$(div).hide();

		if ($(div + ' .close').length == 0 && config.close == 'true') {
			$('<div class="close"></div>').appendTo(div);
		};

		
		
		this.each(function(){

			$(this).css('cursor','pointer');
			var zindex = parseInt($(this).index());
			if (config.nolight == 'true') {
				return false;
			};

			$(this).click(function(e){
				e.preventDefault()

				var left = ($('body').width() - $(div).width())/2;
				var cssObj = {
					'left' : left,
      			'z-index' : (parseInt(config.zindex) + zindex + 1	),
      			'top' : config.top + 'px',
      			'position' : config.position
    			}


				if (!config.animate){
				$(div)
					.css({
						'left' : left,
      				'z-index' : (parseInt(config.zindex) + zindex + 1	),
      				'top' : config.top + 'px',
      				'position' : config.position
    				})
    			if (config.style == 'fade') {
					$(div).fadeIn(config.speed,function(){
						if(typeof callback == 'function'){
							callback.call(this);
						}
					});    				
    			} else if (config.style == 'slide'){
					$(div).slideDown(config.speed,function(){
						if(typeof callback == 'function'){
							callback.call(this);
						}
					});
    			} else {
  					$(div).show(0,function(){
  						if(typeof callback == 'function'){
  							callback.call(this);
  						}
  					}); 
    			};


				} else {
					var offset = $(this).offset();
					$(div)
						.data("width", $(div).width())
						.data("height", $(div).height())
						.css({
							'width' : '0px',
							'height' : '0px',
							'z-index' : (parseInt(config.zindex) + zindex + 1	),
							'top' : offset.top + 'px',
							'left' : offset.left + 'px',
							'position' : config.position
						})
						.show()
						.animate({
							'width' : $(div).data("width"),
							'height' : $(div).data("height"),
							'top' : config.top + 'px',
							'left' : left
						}, 300, function(){
							if(typeof callback == 'function'){
							callback.call(this);
							}
						})
				}

					$('<div />',{
						'id' : 'dark' + zindex,
						'class' : 'lightboxOpen',
						'css' : {
							'position' : 'fixed',
							'top' : 0,
							'left' : 0,
							'width' : '100%',
							'height' : '100%',
							'z-index' : (parseInt(config.zindex) + zindex),
							'background-color' : config.background,
							'opacity' : "0." + config.opacity
						}					
					}).appendTo('body');

					$('#dark'+zindex+', '+div+' .close').click(function(){ 
						$(div).stop().css('opacity','1');
						if (config.style == 'fade') {
							$('#dark'+zindex+'').fadeOut(function(){ $('#dark'+zindex+'').remove(); });
							$(div).fadeOut(); 
						} else if (config.style == 'slide') {
							$(div).slideUp(function(){
								$('#dark'+zindex+'').remove();
							});
						} else {
						  $('#dark'+zindex+'').remove();
						  $(div).hide();
						}
					});

				$(document).keyup(function(e) {
				  if (e.keyCode == 27) { $('.close').click(); }   // esc
				});

	     });
   
		});


     return this;
 
   };
 
 })(jQuery);

