
$(document).ready(function() {
	$(".tooltip").mousemove(function(e) {
		var child = $(this).find('span').attr('id');
		
		var tooltip = $("#"+child);
		
		var offset = 15;
		
		var left, top;
		
		if( $(window).width() - (offset*2) >= tooltip.width() + e.clientX )
			left = e.clientX + offset;
		else
			left = e.clientX - offset - tooltip.width();
			
		if( $(window).scrollTop() + (offset * 2) >= $(window).scrollTop() + (e.clientY - tooltip.height()) )
			top = e.clientY + offset;
		else
			top = e.clientY - tooltip.height() - offset;
			
		//console.log(($(window).scrollTop() + (offset * 2))+ ">=" + ($(window).scrollTop() + (e.clientY - tooltip.height())));
			
		//console.log(($(window).scrollTop() + (offset * 2)) +":"+(e.clientY - tooltip.height()) )
		
		/*if( left + tooltip.width() >= $('#main-wrap').width() )
			left = e.pageX - $('#main-wrap').width - tooltip.width();
		else
			left = e.pageX + offset;
			
		if( top + tooltip.height() >= $(window).height() )
			top = $(window).height() - tooltip.height() - h;
		else
			top = e.pageY + offset;*/

		/*var border_top = $(window).height();
		var border_right = $("#main-wrap").width();
		var left_pos, top_pos;
		var offset = 15;
		
		if( border_right - ( offset * 2 ) >= tooltip.width() + e.pageX )
			left_pos = e.pageX + offset;
		else 
			left_pos = border_right - tooltip.width() - offset;
		
		if( border_top + (offset * 2) >= e.pageY - tooltip.height() )
			top_pos = border_top + offset;
		else
			top_pos = e.pageY - tooltip.height() - offset;*/
		
		tooltip.css({left:left+'px', top:top+'px'});
	});
});