/*
 *	Author : Cassel Guillaume @Photon
 */
jQuery.fn.extend({
   findPos : function() {
       obj = jQuery(this).get(0);
       var curleft = obj.offsetLeft || 0;
       var curtop = obj.offsetTop || 0;
       while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
       }
       return {x:curleft,y:curtop};
   }
});

function removeCurrentTooltip(id){
	$("#" + id).remove();
}

(function($) {
	$.fn.toolTip = function(options){
		
		// clean DOM from previous tooltip calls
		$('.tooltipTable').remove();
	  
		// default configuration properties
		var defaults = {
			xOffset: 10,
			yOffset: 50,
			toolTipId: "toolTip",
			content: "",
			useElement: "",
			ElementClassId: "", // select the class. Id will be find
			fadeTime : 5
		}; 
			
		var options = $.extend(defaults, options);
		var content;
		var toolTipWidth = 150;
		var screenWidth = document.documentElement.clientWidth;
		
		this.each(function() {
			$(this).hover(function(e){
				$('.tooltipTable').remove();
				content = (options.content != "") ? options.content : $(this).attr("title");
				content = (options.useElement != "") ? $("#" + options.useElement).html() : content;
				content = (options.ElementClassId != "") ? $("#" + options.ElementClassId + $(this).attr('id')).html() : content;
				if (content != "" && content != undefined){
				$("body").append('<table class="tooltipTable" id="' + options.toolTipId + '"><tbody><tr><td class="corner topleft"/><td class="top"/><td class="corner topright" onclick="removeCurrentTooltip(\'' + options.toolTipId + '\');"/></tr><tr><td class="left"/><td id="toolTip_content">' + content + '</td><td class="right"/></tr><tr><td class="corner bottomleft"/><td class="bottom"></td><td class="corner bottomright"/></tr></tbody></table>');
					
					$("#" + options.toolTipId)
						.css("position","absolute")
						.css("top",(e.pageY - options.yOffset) + "px")
						.css("left",(e.pageX + options.xOffset) + "px")						
						.css("display","none")
						.fadeIn(options.fadeTime)
						.show();
					toolTipWidth = $("#" + options.toolTipId).width() + 23;
				}
			}, function(){ // mouse Out
				$("#" + options.toolTipId).remove();
			});
			$(this).mousemove(function(e){
				if(e.pageX > screenWidth - toolTipWidth ){
					$("#" + options.toolTipId)
					.css("top",(e.pageY - options.yOffset) + "px")
					.css("left",(e.pageX - toolTipWidth + options.xOffset) + "px");
				}
				else {
					$("#" + options.toolTipId)
					.css("top",(e.pageY - options.yOffset) + "px")
					.css("left",(e.pageX + options.xOffset) + "px");	
				}
			});
		})
	}
})(jQuery);
