$(document).ready(function() {
	$('#main').fadeIn(1000);
	var tip;
	$(".tooltip").hover(function(){
		tip = $(this).find('.tip').remove();
		$('body').append(tip);
		tip.fadeIn(); 
	}, function() {
		tip.hide().remove(); 
		$(this).append(tip);
	}).mousemove(function(e) {
		  var mousex = e.pageX + 20; //Get X coodrinates
		  var mousey = e.pageY + 20; //Get Y coordinates
		  var tipWidth = tip.width(); //Find width of tooltip
		  var tipHeight = tip.height(); //Find height of tooltip
		  var tipVisX = $(window).width() - (mousex + tipWidth);
		  var tipVisY = $(window).height() - (mousey + tipHeight);
		if ( tipVisX < 20 ) { 
			mousex = e.pageX - tipWidth - 20;
			$(this).find('.tip').css({  top: mousey, left: mousex });
		} if ( tipVisY < 20 ) { 
			mousey = e.pageY - tipHeight - 20;
			tip.css({  top: mousey, left: mousex });
		} else {
			tip.css({  top: mousey, left: mousex });
		}
	});
});
