(function($){
	
	// Setup Global Variables
	var opts;
	var overlay;
	var elements;
	var dim = 0;
	
	$.fn.dimfocus = function(options){
	
		// Build Options
		opts = $.extend({}, $.fn.dimfocus.defaults, options);
		
		elements = this;
		
		$(opts.close).live('click',function(){
			hide();
		});
		
		$.each(opts.trigger,function(selector,trigger){
			$(selector).bind(trigger,function(){
				if(dim == 0){
					show();
				}
				return false;
			});
		});
	};
	
	//* Defaults
	$.fn.dimfocus.defaults = {
		trigger : {
			'.dim-trigger' : 'click'
		},
		close   : '.close',
		opacity : 80,
		parent  : 'body'
	};
	// */
	
	//* Show Focus
	function show(){
		dim = 1;
		
		var browserPosition = typeof(document.body.style.maxHeight) === 'undefined' ? 'absolute' : 'fixed';

		// Create Overlay
		overlay = document.createElement('div');
		$(overlay).attr({
			id : 'mf-dimfocus-overlay'
		}).css({
			position   : browserPosition,
			zIndex     : 9997,
			top        : 0,
			left       : 0,
			bottom     : 0,
			right      : 0,
			background : '#000',
			opacity    : 0
		}).click(function(){
			hide();
		});
		
		if(typeof(document.body.style.maxHeight) === 'undefined'){
			$(overlay).css({
				width: $(window).width(),
				height: typeof(document.body.style.maxHeight) === 'undefined' ? $(document).height() : $(window).height()
			});
		}
		
		//Append The Overlay
		$(opts.parent).append(overlay);
		$(overlay).animate({
			opacity: (opts.opacity/100)
		});
		
		elements.each(function(){
			if($(this).attr('rel') != 'dim'){
				var el = this;
				var spacer = document.createElement('div');
				$(this).clone().empty().css({
					height:$(el).innerHeight(),
					width:$(el).innerWidth()
				})
				.attr('id',$(this).attr('id') + '-dup')
				.insertAfter(this);
		
				$(this).css({
					top: $(this).offset().top,
					position: 'absolute',
					zIndex: 9999,
					width: $(this).innerWidth()
				})
				.appendTo(opts.parent)
				.attr('rel','dim')
				.addClass('dimfocus');
			}
		});

		//Activate Escape Key Listener
		$(document).keyup(function(event){
			if(event.keyCode == 27){
				hide();
			}
		});
	}
	// */
	
	//* Hide Focus
	function hide(){
		dim = 0;
		
		$(overlay).animate({
			opacity : 0
		},
		500,
		function(){
			$(overlay).remove();
		});
	};
	// */
	
	//* Debug
	function debug($obj){
		if(window.console && window.console.log){
			window.console.log($obj);
		}
	};
	// */
})(jQuery);
