

this.tooltip = function(){
	/* CONFIG */
				
		xOffset = 10;
		yOffset = 20;
		flag = 0;
		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result		
	/* END CONFIG */
	$("img.tooltip").hover(function(e){
		this.t = this.alt;
		this.alt = "";
		$("body").append("<p id='tooltip'>"+ this.t +"</p>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");
	
		/* ウィンドウサイズ */
		var inW = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
		var ichi = inW - e.pageX;
		var boxwidth = $("#tooltip").width();
		
		
		if(ichi <= boxwidth) {
		
		if(flag == 0){
			xOffset = 10;
			yOffset = -240;
			flag = 1;
		}
		
		}else if(ichi >= boxwidth){
			Offset = 10;
			yOffset = 20;
			flag = 0;
		}
	
    },
	function(){
		this.alt = this.t;
		$("#tooltip").remove();
    });
	$("img.tooltip").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
			
	});
};



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