$(document).ready(function() {
	//Tooltips
	$(".tip_trigger").hover(function(){
		$(this).find('.tip').show(); //Show tooltip
	}, function() {
		$(this).find('.tip').hide(); //Hide tooltip		  
	}).mousemove(function(e) {
		  var mousex = e.pageX + 10; //Get X coodrinates
		  var mousey = e.pageY + 10; //Get Y coordinates
		  var tipWidth = $(this).find('.tip').width(); //Find width of tooltip
		  var tipHeight = $(this).find('.tip').height(); //Find height of tooltip
		 
		 //Distance of element from the right edge of viewport
		  var tipVisX = $(window).width() - (mousex + tipWidth); 
		  var tipVisY = $(window).height() - (mousey + tipHeight);
		  
		if ( tipVisX < 20 ) { //If tooltip exceeds the X coordinate of viewport
			mousex = e.pageX - tipWidth - 20;
			$(this).find('.tip').css({  top: mousey, left: mousex });
		} if ( tipVisY < 20 ) { //If tooltip exceeds the Y coordinate of viewport
			mousey = e.pageY - tipHeight - 20;
			$(this).find('.tip').css({  top: mousey, left: mousex });
		} else {
			$(this).find('.tip').css({  top: mousey, left: mousex });
		}
	});
});

