/**
 * 
 * aOverlay - ajTools, a jQuery Toolkit
 * http://dev.andreafailli.it/ajtools
 *
 * Copyright 2011, Andrea Failli
 *
 * Date: 19/09/2011
 * 
 */


(function($) {
	jQuery.fn.aOverlay = function(method) {
		
		// plugin settings
		var defaults = {
			debug : false,
			modal : false,
			duration : 300,
			class: ""
		};
		
		// events
		/*
		 * - opening
		 * - opened
		 * - closing
		 * - closed
		 */
		
		// plugin methods
		var methods = {
				
			init : function(options){
				var settings = defaults;				
				if (options) { 
					settings = $.extend({},defaults,options);
				}
				return this.each(function() {        
					var $this = $(this);
					
					$this.bind("click.aOverlay",function(evt) {
						
						var url = $this.attr("rel") ? $this.attr("rel") : $this.attr("href");
						$.ajax({
							url : url,
							success : function(data) {
								$this.aOverlay("open",data);
							}
						});
						
						evt.preventDefault();
					});
					
					$this.data("aOverlay",{
						// local data storage
						settings : settings,
						container : null
					});
					
				});
			},
			
			
			open : function(html) {
				
				var $this = $(this);
				var data = $this.data("aOverlay");
				
				var optionalClassString = data.settings.class?" "+data.settings.class:""
				
				var overlay = $("div.aOverlay.aOverlay-overlay");
				if (overlay.size()==0) {
					overlay = $("<div class='aOverlay aOverlay-overlay"+optionalClassString+"'></div>");
					$("body").append(overlay);
					overlay.fadeOut(0);
				}
				
				var container = $("<div class='aOverlay aOverlay-container"+optionalClassString+"'></div>");
				$("body").append(container);
				container.fadeOut(0);
				
				var content = $("<div class='aOverlay aOverlay-content"+optionalClassString+"'></div>");
				content.html(html);
				
				var x = $("<a class='aOverlay aOverlay-close"+optionalClassString+"' href='javascript:void(0);'></a>");
				x.click(function(){
					$this.aOverlay("close");
				});
				
				container.append(x);
				container.append(content);
				
				
				data.container = container;
				
				$this.trigger("opening.aOverlay");
				overlay.fadeTo(data.settings.duration,.7,function(){
					container.fadeIn(data.settings.duration,function(){
						$this.trigger("opened.aOverlay");
					});
				});
				
				
				
				if ( ! $this.data("aOverlay").settings.modal) {
					overlay.click(function(){
						$this.aOverlay("close");
					});
					container.click(function(evt){
						evt.stopPropagation();
					});
					
					$(document).bind("keyup.aOverlay",function(evt){
						if ( evt.which == 27 ) {
							$this.aOverlay("close");
						}
					});
				}
				
				// save data
				 $this.data("aOverlay",data);
				
			},
			
			close : function() {
				var $this = $(this);
				var data = $this.data("aOverlay");
				
				$this.trigger("closing.aOverlay");
				data.container.fadeOut(data.settings.duration,function(){
					$("div.aOverlay.aOverlay-overlay").fadeOut(data.settings.duration,function(){
						$this.trigger("closed.aOverlay");
					});
				});
			},
			
			log : function(x) {
				if ($(this).data("aOverlay").settings.debug)
					if (typeof window.console != 'undefined')
						if (window.console.log) {
							if (typeof x == "string")
								window.console.log("aOverlay: "+x);
							else
								window.console.log(x);
						}	
			},
			
			destroy : function() {
				
				$(document).bind(".aOverlay"); /// FIXME! bind --> UNbind?
				
				return this.each(function() {  
					var $this = $(this);
					$this.unbind(".aOverlay");
					$this.removeData("aOverlay");
				});
				
			}
			
		
		};

		// method calling logic: $("xx").plugin("method",args)
		if ( methods[method] ) {
			return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} else if ( typeof method === 'object' || ! method ) {
			return methods.init.apply( this, arguments );
		} else {
			$.error( 'Method ' +  method + ' does not exist on jQuery.aOverlay' );
		}  
		
		
	};	
})(jQuery);
