/**
 * 
 * aTip - ajTools, a jQuery Toolkit
 * http://dev.andreafailli.it/ajtools
 *
 * Copyright 2011, Andrea Failli
 *
 * Date: 11/09/2011
 * 
 */


(function($) {
	jQuery.fn.aTip = function(method) {
		
		// plugin settings
		var defaults = {
			debug : false,
			position : "right", // top | left | bottom | right
			trigger : "hover",	// hover | focus | manual
			offset : 0
		};
		
		// plugin methods
		var methods = {
				
			init : function(options){
				var settings = defaults;				
				if (options) { 
					settings = $.extend({},defaults,options);
				}
				return this.each(function() {        
					var $this = $(this);
					
					// plugin init code here
					
					if ($this.attr("title")) {
						
						// create tooltip
						var tooltip = $("<div class='aTip aTip-hidden'></div>");
						tooltip.addClass("aTip-"+settings.position);
						var tooltipContent = $("<div class='aTip-content'></div>");
						tooltipContent.html($this.attr("title"));
						$this.attr("original-title",$this.attr("title"));
						$this.removeAttr("title");
						tooltip.append(tooltipContent);
						
						$this.addClass("aTip-hook");
						
						tooltip.fadeTo(0,0);
						
						// append tooltip
						$("body").append(tooltip);
						
						// save data
						$this.data("aTip",{
							settings : settings,
							tooltip : tooltip,
							tmrHide : null
						});
						
						// bind reposition event
						$(window).bind("resize.aTip",function(){
							$this.aTip("reposition");
						});
						$("body").bind("scroll.aTip",function(){
							$this.aTip("reposition");
						});
						
						// now, position the tooltip
						$this.aTip("reposition");
						
						// triggers
						var showFunction = function(){
							$this.aTip("show");
						};
						var hideFunction = function(){
							$this.aTip("hide");
						};
						
						switch(settings.trigger) {
						case "hover" :
							$this.bind("mouseover.aTip",showFunction);
							$this.bind("mouseout.aTip",hideFunction);
							break;
						case "focus" :
							$this.bind("focus.aTip",showFunction);
							$this.bind("blur.aTip",hideFunction);
							break;
							
						}
						
						
					}
					
					
				});
			},
			
			
			reposition : function() {
				var $this = $(this);
				var tooltip = $this.data("aTip").tooltip;
				var settings = $this.data("aTip").settings;
				
				
				switch(settings.position) {
				case "right" :
					tooltip.css({
						left: $this.offset().left + $this[0].offsetWidth + settings.offset,
						top : $this.offset().top + $this[0].offsetHeight/2 - tooltip[0].offsetHeight/2
					});
					break;
				case "left" :
					tooltip.css({
						left: $this.offset().left - tooltip[0].offsetWidth - settings.offset,
						top : $this.offset().top + $this[0].offsetHeight/2 - tooltip[0].offsetHeight/2
					});
					break;
				case "top" :
					tooltip.css({
						left: $this.offset().left + $this[0].offsetHeight/2 - tooltip[0].offsetWidth/2,
						top : $this.offset().top - tooltip[0].offsetHeight - settings.offset
					});
					break;
				case "bottom" :
					tooltip.css({
						left: $this.offset().left + $this[0].offsetHeight/2 - tooltip[0].offsetWidth/2,
						top : $this.offset().top + $this[0].offsetHeight + settings.offset
					});
					break;
				}
				
				
			},
			
			show : function(time) {
				var $this = $(this);
				var tooltip = $this.data("aTip").tooltip;
				var data = $this.data("aTip");
				
				$this.aTip("reposition"); // FIXME : non ci dovrebbe essere bisogno di questo...
				
				tooltip.removeClass("aTip-hidden");
				tooltip.animate({
					opacity:1
				},300);
				
				if (time>0) {
					data.tmrHide = setTimeout(function(){
						data.tmrHide=null;
						$this.data("aTip",data);
						$this.aTip("hide");
						
					},time);
					$this.data("aTip",data);
				}
					
				
			},
			hide : function() {
				var $this = $(this);
				var data = $this.data("aTip");
				var tooltip = $(this).data("aTip").tooltip;
				
				
				tooltip.animate({
					opacity:0
				},300,function(){tooltip.addClass("aTip-hidden");});
				
				if (data.tmrHide!=null) {
					clearTimeout(data.tmrHide);
					data.tmrHide=null;
				}
				$this.data("aTip",data);
			},
			
			log : function(x) {
				if ($(this).data("aTip").settings.debug)
					if (typeof window.console != 'undefined')
						if (window.console.log) {
							if (typeof x == "string")
								window.console.log("aTip: "+x);
							else
								window.console.log(x);
						}	
			},
			
			destroy : function() {
				
				$(window).unbind(".aTip");
				$("body").unbind(".aTip");
				
				return this.each(function() {
					var data = $(this).data("aTip")
					if (data) {
						if (data.tmrHide!=null)
							clearTimeout(data.tmrHide);
						data.tooltip.remove();
						$(this)
							.unbind(".aTip")
							.removeData("aTip");
					}
					
				});
				
			}
			
		
		};

		// 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.aTip' );
		}  
		
		
	};	
})(jQuery);
