// JS document
var filterBy = 'month';
var tempDist = "";
var currentMonth = "";
var currentYear = "";

$(document).ready(function(){

	$('ui-datepicker-prev').focus();
	
	//set todays date
	var newDate = $.datepicker.formatDate( 'MM' , new Date() );
	$('#current-date').html('Results shown for ' + newDate);
	
	//Set up the Calendar
	$("#datepicker").datepicker({
		changeMonth: true,
		changeYear: true,
		showOtherMonths: true,
		//set the date shown when the calendar is clicked
		onSelect: function() { 
			updateDateDisplay();
			updateResults(filterBy, null);
		},
		onChangeMonthYear: function(year, month, inst) {
			//alert("im changing the month/year now.");
			currentMonth = month;
			currentYear = year;
			updateResults('month', null, month, year);
		}
	});
	$('#datepicker').datepicker('option', 'firstDay', 1);
	
	//change the intro text
	$('div.calendar-header-text p').html('Select a day from the calendar, or use the menus below for more search options.');
	
	//set up the view by filter buttons
	$('.calendar-controls').toggle();
	$('#calendar-by-day').click(function() {
		filterBy = 'day';
		turnOffButtons();
		updateDateDisplay();
		updateResults('day', null);
		
		$("#calendar-by-week").addClass("calendar-by-week-off");
		$("#calendar-by-month").addClass("calendar-by-month-off");
		$(this).addClass("calendar-by-day-on")
		return false;
	});
	
	$('#calendar-by-week').click(function() {
		filterBy = 'week';
		turnOffButtons();
		updateDateDisplay();
		updateResults('week', null);
	
		$("#calendar-by-day").addClass("calendar-by-day-off");
		$("#calendar-by-month").addClass("calendar-by-month-off");	
		$(this).addClass("calendar-by-week-on");
		return false;
	});
	
	$('#calendar-by-month').click(function() {
		filterBy = 'month';
		turnOffButtons();
		updateDateDisplay();
		updateResults('month', null);
		
		$("#calendar-by-day").addClass("calendar-by-day-off");
		$("#calendar-by-week").addClass("calendar-by-week-off");
		$(this).addClass("calendar-by-month-on");
		return false;
	});
	
});

function updateDateDisplay() {
	if( filterBy == 'day' ) {
		var currDate = $.datepicker.formatDate( 'dd M yy' , $("#datepicker").datepicker('getDate') );
		$('#current-date').html('Results shown for ' + currDate);
	} else if( filterBy == 'week' ) {
		var monthYear = $.datepicker.formatDate( ' M yy' , $("#datepicker").datepicker('getDate') );
		var displayDate = getWeekDates( $("#datepicker").datepicker('getDate') );
		// $('#current-date').html('Results shown for week: ' + displayDate + monthYear );
		$('#current-date').html('Results shown for week commencing ' + displayDate + monthYear );
	} else if( filterBy == 'month' ) {
		var currDate = $.datepicker.formatDate( 'M yy' , $("#datepicker").datepicker('getDate') );
		$('#current-date').html('Results shown for ' + currDate);
	}
}

function turnOffButtons() {
	$("#calendar-by-day").removeClass("calendar-by-day-on");
	$("#calendar-by-week").removeClass("calendar-by-week-on");
	$("#calendar-by-month").removeClass("calendar-by-month-on");
	$("#calendar-by-day").removeClass("calendar-by-day-off");
	$("#calendar-by-week").removeClass("calendar-by-week-off");
	$("#calendar-by-month").removeClass("calendar-by-month-off");
}

function updateResults(disp, startRow, newMonth, newYear)
{
	if(disp==null) 
		disp=filterBy;
	else
		filterBy = disp;
		
	//alert(disp + "," + startRow);
	if(startRow == null) startRow = 1;
	
	//Add code here to update the calendar results
	//var 'theDate' is the current date on the calendar 
	var theDate = $("#datepicker").datepicker('getDate');
	
	if (theDate == null)
		return;
	
	//Get the required date info in its correct form
	var theDay = theDate.getDate();
	
	var theMonth, theYear;
	
	if (newMonth == null)
		theMonth = currentMonth;
	else
		theMonth = newMonth;
		
	if (newYear == null)	
		theYear = currentYear
	else	
		theYear = newYear;	
		
	//var eventType  = document.getElementById('calendar-kew-events').options[document.getElementById('calendar-kew-events').selectedIndex].value;
	var garden			 = $('#calendar-kew-events').val();
	var eventSubType = document.getElementById('calendar-all-events').options[document.getElementById('calendar-all-events').selectedIndex].value;
	
	//Set to 1 to print out debug info on ajax response
	var debug=0;
	
	//Do ajax call to get new calendar results
	var ts = new Date().getTime(); // forces server request in IE
	var submitUrl = "?IdcService=KEW_DISPLAY_INC&incName=kew_calendar_results";
	$.get(
		submitUrl, 
		{date : theDay, month : theMonth, year : theYear, disp : disp, garden : garden, eventType : "Public events", eventSubType : eventSubType, calendarStartRow : startRow, timestamp : ts, isDebug : debug}, 
		function(data){document.getElementById('calendar-results').innerHTML = data;}
	);
}


//date formatting
function getWeekDates( theDate ) {
	var day = theDate.getDate();
	var month = theDate.getMonth() + 1;
	var year = theDate.getYear();
	if (year < 2000)
		year = year + 1900;
	// var offset = theDate.getDay();
	var offset = theDate.getDay() - 1;
	var week;
	var day2;

	if(offset != 0) {
		day = day - offset;
		if ( day < 1) {
			if ( month == 1) 
				day = 31 + day;
			if (month == 2) 
				day = 31 + day;
			if (month == 3) {
				if (( year == 00) || ( year == 04)) {
					day = 29 + day;
				} else {
					day = 28 + day;
				}
			}
			if (month == 4) 
				day = 31 + day;
			if (month == 5) 
				day = 30 + day;
			if (month == 6) 
				day = 31 + day;
			if (month == 7) 
				day = 30 + day;
			if (month == 8) 
				day = 31 + day;
			if (month == 9) 
				day = 31 + day;
			if (month == 10) 
				day = 30 + day;
			if (month == 11) 
				day = 31 + day;
			if (month == 12) 
				day = 30 + day;
		}
	}
	
	
	day2 = day + 6;
	if ( month == 1 && day2 > 31 ) 
		day2 = day2 - 31;
	if (month == 2 && day2 > 28 ) 
		if (( year == 00) || ( year == 04)) {
			day2 = day2 - 29;
		} else {
			day2 = day2 - 28;
		}
	if (month == 3 && day2 > 31 ) {
		day2 = day2 - 31;
	}
	if (month == 4 && day2 > 30 ) 
		day2 = day2 - 30;
	if (month == 5 && day2 > 31 ) 
		day2 = day2 - 31;
	if (month == 6 && day2 > 30 ) 
		day2 = day2 - 30;
	if (month == 7 && day2 > 31 ) 
		day2 = day2 - 31;
	if (month == 8 && day2 > 31 ) 
		day2 = day2 - 31;
	if (month == 9 && day2 > 30 ) 
		day2 = day2 - 30;
	if (month == 10 && day2 > 31 ) 
		day2 = day2 - 31;
	if (month == 11 && day2 > 30 ) 
		day2 = day2 - 30;
	if (month == 12 && day2 > 31 ) 
		day2 = day2 - 31;
	
	//week = month + "-" + day + "-" + year; // i.e. 10-31-99
	week = day + " - " + day2; // i.e. 10-31-99
	// return( week );
	return( day );
}