var currentInfo;
function installTooltips() {
	$(".item").unbind('mouseover');
	$(".item").unbind('mousemove');
	$(".item").unbind('mouseout');
	$(".item").mouseover(function() {
		var my_id = $(this).attr('id');
		currentInfo = $("#info-" + my_id);
		
		currentInfo.show();
	});
	$(".item").mousemove(function (mouse) {
		if(currentInfo == null) {
			return;
		}

		var docWidth = document.documentElement.clientWidth - 62;
		var toolWidth = currentInfo.width();
		var toolHeight = currentInfo.height();
		var newLeft = mouse.pageX - ((toolWidth+22) / 2);
		var newTop = mouse.pageY + 30;
		// Right limit
		if(newLeft + toolWidth - $(document).scrollLeft()  > docWidth)
		{
			newLeft = docWidth - toolWidth + $(document).scrollLeft();
		}
		// Left limit
		if(newLeft < $(document).scrollLeft())
		{
			newLeft = $(document).scrollLeft();
		}
		if(newTop + toolHeight - $(document).scrollTop() + 65 > document.documentElement.clientHeight)
		{
			newTop = mouse.pageY - toolHeight - 52;
		}
		if(newTop < $(document).scrollTop())
		{
			newTop = $(document).scrollTop();
		}
		currentInfo.css({ left: newLeft, top: newTop });
	});
	$(".item").mouseout(function() {
		currentInfo.hide();
		$(this).parent().removeClass("cellHover");
	});
}
$(installTooltips);

function installPageHandlers()
{	
	function updatePage(page) {
		if(page <= 1) {
			$("#prev").addClass('disabled');
			page = 1;
		} else {
			$("#prev").removeClass('disabled');
		}
		
		if(page >= 4) {
			$("#next").addClass('disabled');
			page = 4;
		} else {
			$("#next").removeClass('disabled');
		}		
		
		$(".page").each(function() {
			$(this).hide();
		});
		$("#page-"+page).show();
		
		$('.navigation-page').css('background-position', ('0 '+ ((page-1)*(-43)) +'px'));	
		$('.navigation-page').attr('id', 'cpage'+page);	
	}
	
	$(".navigation-button").click(function() {
		var type = $(this).attr('id');
		var currPage = parseInt($('.navigation-page').attr('id').substr(5));
		
		if(type == 'next') {
			updatePage(currPage + 1);
		}
		else if(type == 'prev')	{
			updatePage(currPage - 1);
		}		
	});
	
	updatePage(1);
}

$(installPageHandlers);
