$(document).ready(function() {
	
	$('#diary-events').click(function() { 
		
		if($('#calander').length==0) {
			$.ajax({
				url: '/events/?function=load_cal',
				dataType: 'json',
				type: 'GET',
				timeout: 2999,
				global: false,
				cache: false,
				success: function(data) {
					$("body").prepend(data.cal);
					$('#grey-whole-screen').click(function() { 
						hide_all();
					});
					$('#grey-whole-screen').css('height',$(document).height()).fadeIn();
					$('#calander').fadeIn().center();
					position_events();
					
				},
				error: function(data) {
					//cl('error');
				},
				complete: function(data) {
				}
			}); 
		
		} else {
			show_all();
		}
	});
	
	$('#grey-whole-screen').click(function() { 
		hide_all();
	});
	
	$('#dna-lab').next().css('width', '110px');
	
});

function hide_all() {
	$('#grey-whole-screen').fadeOut();
	$('#calander').fadeOut();
	$('#tooltips').fadeOut();
}

function show_all() {
	$('#grey-whole-screen').fadeIn();
	$('#calander').fadeIn().center();
	$('#tooltips div').hide();
	$('#tooltips').show();
}

$.fn.center = function () {
	
	var top = ((($(window).height() - 810) / 2)+$(window).scrollTop());
	
	if(top<0) top = 10;
	
	this.css({
		"position": "absolute",
		"top": top + "px",
		"left": ((($(window).width() - this.width()) / 2)+$(window).scrollLeft())-23 + "px"
	});
	return this;
} 
