var dateToday = new Date();
var month = dateToday.getMonth() + 1;
var year = dateToday.getFullYear();
var justLoaded = true;

$(document).ready( function() {
	loadFontSize();
	//Load events calendar if the div is valid (This way we can load the cal on any page we want)
	if($("#events-calender").length > 0){
		loadCalendar();
	}
	
	loadToolTips();
});
var fontCurrent = 0;
var cookieName = "fontsize";
function loadFontSize(){
	//read cookie
	fontCurrent = parseInt($.cookie(cookieName));
	if(isNaN(fontCurrent)){
		fontCurrent = 0;
	}
	//Overwrite fontCurrent;
	setFontSize();
	
	//bind some controls
	$(".enlarge").bind("click",function(){setFontSize(1,1);});
	$(".decrease").bind("click",function(){setFontSize(-1,1);});
}

function setFontSize(font,save){
	var normal = 1;
	var step = 0.18;	
	var limit = 1;
	
	if(font){
		if(font == 1){
			if(font+fontCurrent <= limit){
				fontCurrent += font;
			}
		}else if(font == -1){
			if(font+fontCurrent >= "-"+limit){
				fontCurrent += font;
			}
		}
	}
	
	size = (step * fontCurrent) + normal;
	$('#container').css("font-size",size + "em");
	
	//Save font
	if(!save){
		return true;
	}
	
	$.cookie(cookieName, fontCurrent);
}

function loadToolTips(){
	$("#bubble ul li a, #bubble2 h3 a").tooltip({ 
	    track: true, 
	    delay: 100, 
	    showURL: false, 
	    opacity: .95, 
	    showBody: " | ",
		fade: 250
	});
}

function loadCalendar(){
	$.ajax({
		url: "/blog_events.php?month=" + month + "&year=" + year,
		type: 'post',
		success: function(j){
			if(justLoaded == false){
				$('#cal-table > tbody').fadeOut('slow', function(){
					$("#events-calender").html(j);
					bindElements();
				});	
			}else{
				$("#events-calender").html(j);
				bindElements();
			}
		}
	}); 
}

function bindElements(){
	$("#event-nextMonth").bind("click",nextMonth);
	$("#event-prevMonth").bind("click",prevMonth);
	$("#event-month").bind("change",changeMonth);
	$("#event-year").bind("change",changeYear);
	
	$("#cal-table a").tooltip({ 
		track: true, 
		delay: 200, 
		showURL: false, 
		opacity: 1, 
		showBody: " | ",
		top: -28, 
		left: 20 
	});
	justLoaded = false;
}

function nextMonth(){
	//Check to see if the year is out of bounds
	if(month == 12 && year == dateToday.getFullYear()+2){
		return true;
	}
	
	//Check to see if we need to start a new year
	if(month == 12){
		year++;
		month = 1;
	}else{
		month++;	
	}
	
	loadCalendar();
}

function prevMonth(){
	//Check to see if the year is out of bounds
	if(month == 1 && year == dateToday.getFullYear()-2){
		return true;
	}
	
	//Check to see if we need to go back a year
	if(month == 1){
		year--;
		month = 12;
	}else{
		month--;	
	}
	
	loadCalendar();
}

function changeMonth(m){
	month = m.target.value;
	loadCalendar();
}

function changeYear(y){
	year = y.target.value;
	loadCalendar();
}