/*------------------------------------------------------------------ 

[JavaScript tooltip.js] 

Project:    		We Ain't Plastic 
Version:    		1.0 
Last change:    	
Autor:		    	Roland Loesslein (rl) 
Primary use:    	jQuery Plugin 

-------------------------------------------------------------------*/

(function($){

	$.fn.extend({
	    tooltip: function( givenOptions ){
	        
	        return this.each(function(){
	            
	            var $this 	= $(this);
	            var $tip	= $(".tip", $this);
	            var $tiplink	= $("a:eq(0)", $this);
	            
	            var isShown	= false;
	            var showPosition;
	            var hidePosition;
	            
	            // reset tip css
	            $tip.css({
	            	opacity: 0,
	            	bottom: 5
	            });
	            
	            var options	= $.extend({
	            	showTime: 			300,
	            	hideTime: 			300,
	            	horizontalAlign:	'center',
	            	verticalAlign:		'bottom'
	            }, givenOptions )
	            
	            switch( options.horizontalAlign ){
	            
	            	case "center":
	            		$tip.css({ 
	            			left: -parseInt($tip.width())/2 + parseInt($tiplink.width())/2,
	            			backgroundPosition: 'center bottom'	
	            		})
	            		break;
	            		
	            	case "left":
	            		$tip.css({
	            			left: -5,
	            			backgroundPosition: '5px bottom'
	            		});
	            		break;
	            		
	            	case "right":
	            		$tip.css({
	            			left: -parseInt($tip.width()) + parseInt($tiplink.width()) + 5,
	            			backgroundPosition: parseInt($tip.width())-10- parseInt($tiplink.width())/2 + 'px bottom'
	            		});
	            		break;
	            
	            }
	            
	            switch( options.verticalAlign ){
	            
	            	case "top":
	            		showPosition = -26;
	            		hidePosition = -10;
	            		break;
	            		
	            	case "bottom":
	            		showPosition = 10;
	            		hidePosition = 20;
	            		break;
	            
	            }
	            
	            $this.hover(
	            	function(){
	            		if( !isShown ){
	            			$tip.css('visibility', 'visible');
		            		$tip.animate(
		            			{
	          					opacity: 1,
	          					bottom: showPosition
	        					},
	        					options.showTime,
	        					"swing",
	        					function(){
	        						isShown = true;
	        					}
	        				);
	            		}
	            		
	            	},
	            	function(){
     					$tip.stop();
            			$tip.animate(
            				{
      						opacity: 0,
      						bottom: hidePosition
    						},
    						options.showTime,
    						"swing",
    						function(){
    							isShown = false;
    							$tip.css({
					            	opacity: 0,
					            	bottom: 5,
					            	visibility: 'hidden'
					            });
    						}
    					);
	            		
	            	}
	            );
	           
	        });
	
		}
	});
	
})(jQuery);