(function($){
		  
$.smfbox = {
	defaults: {
		afterRender: null,
		beforeLoad: null,
		beforeRender: null,
		boxClass: 'default',
		content: null,
		data: null,
		onClose: null,
		overlayOpacity: 0.75
	}
};

$.fn.extend({
	smfbox:function(settings){	
		
		var config = $.extend({}, $.smfbox.defaults, settings);
		config.$this = this;
		
		var cachebuster = new Date().getTime();
		cachebuster = cachebuster+'-'+Math.floor(Math.random()*1000000);
		
		this.each(function(index,$this){
			$this = $($this);
			
			var constants = {
				box: '#smfbox',
				close: '#smfclose',
				content: '#smfcontent',
				html: '<div id="smfoverlay" style="display:none"></div><div id="smfwrapper"><div id="smfbox" style="display:none"><a id="smfclose" href="#">Close</a><div id="smfcontent"></div></div></div>',
				trigger: $(this),
				overlay: '#smfoverlay',
				wrapper: '#smfwrapper'	
			};
			
			
		function boxExistsCheck() {
			var overlay = $(constants.overlay, 'body'),
				wrapper = $(constants.wrapper, 'body');
	
			if (overlay.length > 0) {
				wrapper.remove();
				overlay.remove();
			}
		}
		
		function boxClose(overlay, wrapper) {
			overlay.fadeOut();
			wrapper.fadeOut('fast',boxExistsCheck);
	
			if (defaultcontent) {
				config.content = null;
			}
	
			if(config.onClose) {
				config.onClose();
			}
		}
		
		function boxLaunch() {
			if (!config.content) {
				alert('Missing parameter \'content\'');
				return this;
			}
			
			boxExistsCheck();
			$('body').append(constants.html);
			
			
			var smfoverlay = $(constants.overlay, 'body'),
				smfwrapper = $(constants.wrapper),
				smfbox = $(constants.box, smfwrapper),
				smfcontent = $(constants.content, smfbox),
				smfclose = $(constants.close, smfbox);
			
			smfcontent.load(config.content,config.data,function(){
			
				if(config.beforeRender) {
					config.beforeRender();
				}
	
				smfbox.addClass(config.boxClass);	
				smfoverlay.css('opacity',config.overlayOpacity);
				smfoverlay.fadeIn('fast',function(){
					smfbox.fadeIn('fast');
	
					if(config.afterRender) {
						config.afterRender();
					}
					
				});
			});
			
			smfclose.click(function(){
				boxClose(smfoverlay, smfwrapper);
				return false;
			});
			
			smfoverlay.click(function(){
				boxClose(smfoverlay, smfwrapper);
			});
		}//boxLaunch
		
		constants.trigger.click(function(){
			$this = $(this);
			
			if (!config.content) {
				config.content = $this.attr('href');
				defaultcontent = true;
			}
			
			if (config.content.indexOf('?')>0) {
				config.content = config.content+'&nocache='+cachebuster;
			} else {
				config.content = config.content+'?nocache='+cachebuster;
			}
			
			
			if(config.beforeLoad) {
				config.beforeLoad();
			}
			
			boxLaunch();
			return false;
		});
			
			
		});
		return this;
	}
});

})(jQuery);
