(function($)
{
	$.fn.extend(
	{ 
		
		ssModalBox: function(options) 
		{
		
    		// Create our overlay object 
		    var overlay = $("<div id=\"mb_overlay\"></div>");
		    overlay.css({
		        "position": "fixed", 
		    	"z-index":100, 
		    	"top": "0px", 
		    	"left": "0px", 
		    	"height":"100%", 
		    	"width":"100%", 
		    	"background": "#000000", 
		    	"display": "none"
			});
    		
    		return this.each(function() 
    		{

				//Listen for clicks on objects passed to the plugin
				$(this).click(function(e) 
				{
					//Append the overlay to the document body
					$("body").append(overlay);
						
					//Set the css and fade in our overlay
					overlay.css("opacity", 0.8);
					overlay.fadeIn(150);
	
					// Display modal box
					$('#'+options.container).fadeIn(150);
							
					// Make draggable
					$('#'+options.container).draggable({ 
						handle: '#'+options.handle, 
						cursor: 'move', 
						containment: 'window'
					});
					
					// Center box on initialization
					$('#'+options.container).css({
						
						"left": ($(window).width() / 2) - ($('#'+options.container).width() / 2) + 'px', 
						"top": ($(window).height() / 2) - ($('#'+options.container).height() / 2) + 'px' 

					});
					
					// Center box on window resize
					$(window).resize(function(){
					  	$('#'+options.container).css({
						
							"left": ($(window).width() / 2) - ($('#'+options.container).width() / 2) + 'px', 
							"top": ($(window).height() / 2) - ($('#'+options.container).height() / 2) + 'px' 
	
						});
						
					});
						    
					//Prevent the anchor link from loading
					e.preventDefault();
							
					//Activate a listener for escape key
					$(document).keydown(function(e)
					{
						if (e.keyCode == 27) 
						{  
       						$('#'+options.close).trigger("click");
						}
					});	
						
					// Listen for modal box closing
					$('#'+options.close).click(function(e) 
					{ 
						e.preventDefault();
						
						$(document).unbind("keydown"); 
       					$('#'+options.container).hide(); 
      					overlay.fadeOut(150, function() { overlay.remove(); } ); 

					});
							
				});
				
	    	});
		}		
	});
})(jQuery);