this.tooltip = function(){	
	/* CONFIG */		
		yOffset_temp = 10;
		xOffset_temp = 20;
		yOffset = 0;
		xOffset = 0;
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result		
	/* END CONFIG */		
	
	windowSize = function(){
		if (self.innerHeight)
		{
			height = self.innerHeight;
			width = self.innerWidth;
		}
		else if (document.documentElement && document.documentElement.clientHeight)
		{
			height = document.documentElement.clientHeight;
			width = document.documentElement.clientWidth;
		}
		else if (document.body)
		{
			height = document.body.clientHeight;
			width = document.body.clientWidth;
		}				 
	};
	
	windowSize();
	
	window.onresize = windowSize;
	
	
	jQuery('.tooltip').each(function(e){
		if (this.title)
		{
			this.rel = this.title;
			this.title = "";
		}
	});
	
	
	$(".tooltip").hover(function(e){
		$("#tooltip").remove();
		
		$("body").append("<div id='tooltip'><div class='header'></div><div class='center'>"+ this.rel +"</div><div class='footer'></div></div>");
		
		divHeight = document.getElementById('tooltip').offsetHeight;
		divWidth = document.getElementById('tooltip').offsetWidth;
		
		if (height  < divHeight+e.clientY+yOffset_temp)
		{
			if (height > divHeight+height-e.clientY+yOffset_temp) yOffset = -(divHeight+yOffset_temp/2);
			else yOffset = -(divHeight-((divHeight+height-e.clientY+yOffset_temp)-height)+yOffset_temp/2);
		}
		else yOffset = yOffset_temp;
		
		if (width  < divWidth+e.clientX+xOffset_temp) xOffset = -(divWidth+xOffset_temp);
		else xOffset = xOffset_temp;
		
		$("#tooltip")
			.css("top",(e.pageY - yOffset) + "px")
			.css("left",(e.pageX + xOffset) + "px")
			.fadeIn("fast");
		},
		function(){	
			$("#tooltip").fadeOut("fast");
    	}
	);	
	
	$(".tooltip").mousemove(function(e){
		divHeight = document.getElementById('tooltip').offsetHeight;
		divWidth = document.getElementById('tooltip').offsetWidth;
		
		if (height  < divHeight+e.clientY+yOffset_temp)
		{
			if (height > divHeight+height-e.clientY+yOffset_temp) yOffset = -(divHeight+yOffset_temp/2);
			else yOffset = -(divHeight-((divHeight+height-e.clientY+yOffset_temp)-height)+yOffset_temp/2);
		}
		else yOffset = yOffset_temp;
		
		if (width  < divWidth+e.clientX+xOffset_temp) xOffset = -(divWidth+xOffset_temp);
		else xOffset = xOffset_temp;
		
		$("#tooltip")
			.css("top",(e.pageY + yOffset) + "px")
			.css("left",(e.pageX + xOffset) + "px");
	});			
};



// starting the script on page load
$(document).ready(function(){
	tooltip();
});