/*
 * Accordingly jQuery Plugin v1.0
 *
 * Copyright (c) 2011 Brian J. Bouril
 *
 * Dual licensed under the MIT and GPL licenses.
 *
 * Date: Tues Jan 13 2011 20:24:34 GMT-0500
 */

(function($){
$.fn.modalit = function(args){
	var methods = {
		ns: 'modalit',
		
		st: {
			classname: 'modal'
		},
		
		launch: function(e){
			var _this = methods;
			
				var url = $(this).attr("href"),
					image = $('<img />');
	  			
	  			$('<div id="modalit-curtain"></div><div id="modalit" class="loading"><span class="modalit-close"></span></div>').appendTo('body');
	  			
	  			$('#modalit-curtain').fadeIn();
	  			$('#modalit').fadeIn();
	  			$('#modalit .modalit-close').click(function(){
	  				$('#modalit-curtain').remove();
	  				$('#modalit').remove();
	  			});
	  			
				  image.attr('src', url)
				  	.load(function () {
				    	$('#modalit').removeClass('loading').append($(this));
				    })
				    .error(function () {
				      // notify the user that the image could not be loaded
				      console.log('not loaded');
				    });
				 
				    e.preventDefault();
		},
		
		init: function(opts){
			var _this = methods;
			
			$.extend(_this.st, opts || {});
			
			this.each(function(){
				if(!$.data(this, _this.ns)){
					$(this).bind('click.' + _this.ns, _this.launch);
					$.data(this, _this.ns, true);
				}
			});
		},
		
		destroy: function(){
			var _this = methods;
			
			this.each(function(){
				$(this).unbind('.' + _this.ns);
				$.removeData(this, _this.ns);
			});
		}
	};

	if(arguments.length && args === 'destroy'){
		methods.destroy.call(this);
	}else{
		methods.init.call(this, args);
	}
	
	return this;
};
})(jQuery);
