var dateToday = new Date();
var month = dateToday.getMonth() + 1;
var year = dateToday.getFullYear();
var justLoaded = true;

$(document).ready( function() {
	//Set back to top links
	ss.fixAllLinks();
	
	//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();
	}
	
    var tabs = $('.tabs').tabs({ fx: { opacity: 'toggle' }});
    if(tabs){
    	tabs.tabs('rotate', 6000);
    	$('div.ui-tabs-panel').mouseover(function() {tabs.tabs('rotate', false);});
    	$('div.ui-tabs-panel').mouseout(function() {tabs.tabs('rotate', 6000);});
    }
});

function checkNewsletter(){
	if($('#newsletter-name').val() != "" && $('#newsletter-email').val() != ""){
		return true;	
	}else{
		alert("Please check you've entered both your name and email address");
		return false;
	}
}

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();
}

function checkCommentForm(){
	if(!($('#frmname').val() && $('#frmemail').val() && $('#frmcontent').val())){
		alert("Please make sure you fill in your name, email and a comment");
		return false;
	}
	
	return true;
}
