/*
 *  --------------------------------
 *  | Plugin tooltip Pour Jquery |
 *  --------------------------------
 *
 *  Affiche une tooltip pour avec le contenu textuel d'une balise
 *  Cible.
 */

(function($){
	$.fn.tooltips = function (params) {
		return this.each(function(){
			new $.tooltips($(this), params);
		});
		
	}
	
	$.tooltips = function (elem,p) {
		if (p){this.params = $.extend(this.params,p);}
		this.init(elem);
	}	

	$.tooltips.prototype = {
		
		params: {
			mode:"text",
			startOpacity:0,
			stopOpacity:0.95,
			effectSpeedIn:200,
			effectSpeedOut:100,
			offsetTop:-3,
			animationOffset:16,
		},

		tooltip:{},
		element:{},
		elementGeometry:{},
		//tooltipGeometry:{},

		init: function (e){
			// on garde trace de l'element en cours
			this.element = e;
			// et de sa géométrie
			this.elementGeometry = this.getGeometry(this.element);
			var This = this;
			this.tooltip=this.setTooltip(this.element);
			this.element.hover(
				function(){
					This.showTooltip();
				},
				function(){
					This.hideTooltip();
				}
			);
		},

		setTooltip:function(e){
			switch(this.params.mode){
				case 'text':
				var html = $("<div class='tooltip'><p class='tooltip-inner'>" + e.text() + "</p></div>");
				break;

				case 'html':
				var html =  $("<div class='tooltip'>"+e.html()+ "</div>");
				html.children().addClass("tooltip-inner");
				break;
			}
			return html;
		},

		showTooltip: function(){
			this.tooltip.prependTo(this.element);					
			this.tooltip.css({
				position:'absolute',
				zIndex:99,
				opacity:this.params.startOpacity,
			});
			this.tooltip.css({
				marginTop:-this.tooltip.innerHeight() + this.params.animationOffset,
				marginLeft:- this.tooltip.innerWidth()/2 + this.elementGeometry.width/2
			}).animate({
				opacity:this.params.stopOpacity,
				marginTop:-this.tooltip.innerHeight()
			},
				this.params.effectSpeedIn
			);
		},

		getGeometry:function(element) {
			var pos = element.offset();
			return ({top:pos.top, left:pos.left, width:element.innerWidth(), height:element.innerHeight()});
		},

		setPosition:function(){
		},

		hideTooltip:function(){
			this.tooltip.animate({
				opacity:this.params.startOpacity,
				marginTop:-this.tooltip.innerHeight() + this.params.animationOffset,
			},
				this.params.effectSpeedIn,
				function(){$(this).remove();}
			);
		}		
	};
})(jQuery)

