/**
* ------------------------------------------------------------------------------------------------
* @author 	Diego Tres me@diegotres.com
* @version 	0.1
* ------------------------------------------------------------------------------------------------
*/


(function($)
{
	$.fn.d3_tooltip = function(options)
	{
		var defaults = 
		{
			duration	: 200,
			easing		: 'easeOutQuad',
			opacity		: .4,
			text		: undefined,
			klass		: 'd3-tooltip',
			direction	: 'bottom',
			offset		: [ 0 , 0 ],
			distance	: 20,
			width		: 'auto',

			shouldHover		: true
		};
		
		var options = $.extend(defaults, options);

        create = function(what)
		{
            return what.each(function() {
                                
			var $this 		= $(this),
				text		= options.text || $this.attr('title') || $this.attr('alt') || $this.attr('value') || $this.text() || undefined,
				distance	= options.distance,
				tipLeft		= $this.offset().left,
				tipTop		= $this.offset().top,
				tipKlass	= (jQuery.browser.mozilla ? "tip" : "webkit-tip"),

				tipHtml		= '<div class="' + options.klass + ' ' + options.direction + '">\
							   		<span class="text" style="width:'+ options.width +'">' + text + '</span>\
							   		<span class="' + tipKlass + '"></span>\
							   </div>';				
			var $tip 	= $(tipHtml).fadeTo(0,0).hide().appendTo('body');
	
			this.show = function(){
				$tip.show();
				$tip.css({
					left		: tipLeft + options.offset[1] + (options.direction.match(/left|right/g) ? distance : 0 ),
					top			: tipTop + options.offset[0] + (options.direction.match(/top|bottom/g) ? distance : 0 )
				}).animate({
					left		: tipLeft + options.offset[1], 
					top			: tipTop + options.offset[0],
					opacity		: 1
				},{ duration	: options.duration , easing: options.easing, queue: false, complete: function(){} });
			};
                switch(options.direction){
                        case 'top'		: tipTop 	= $this.offset().top + options.offset[0]; distance = -distance; 					break;
                        case 'bottom'	: tipTop 	= $this.offset().top + $this.height() + 10 + options.offset[0]; 					break;
                        case 'left'		: tipLeft	= $this.offset().left + options.offset[1]; distance = -distance; 					break;
                        case 'right'	: tipLeft	= $this.offset().left + $this.width() - ( $tip.width() ) + options.offset[1]; 		break;
                        case 'center'	: tipLeft	= $this.offset().left + ($this.width()/2) - ( $tip.width()/2 ) + options.offset[1]; break;
			    }

			this.hide = function(){
				$tip.animate({
					left: '+=' + (options.direction.match(/left|right/g) ? distance : 0 ),
					top: '+=' + (options.direction.match(/top|bottom/g) ? distance : 0 ),
					opacity: 0
				},{ duration: options.duration , easing: options.easing, queue: false, complete: function(){ $(this).hide() } });
			};

            this.update = function(){
				tipLeft		= $this.offset().left;
            }
            $(window).bind('resize', this.update);
			
			if(options.shouldHover)
				$this.hover( this.show, this.hide );

			return $tip;
		});
        }
		return create(this);

	};

})(jQuery);
