$(document).ready(function() {
	function clearDateTime() {
		$('#timepick li').fadeOut('fast', function() {
			$('#timepick a').removeClass('pick');
		});
		$('#month, #day, #year, #time').val('');
		$('#timepick').removeClass('none').attr('title', '');
	}
	
	clearDateTime();

	$('a#nextMonth, a#prevMonth').live('click', function() {
		var url = $(this).attr('href');
		$('#cal').load(url);
		$('#calContainer').removeClass('arrow');
		clearDateTime();
		return false;
	});
   
	$('table.cal td.day').live('click', function() {
		$('table.cal td').removeClass('pick');
		$('#timepick li a').removeClass('taken').removeClass('pick');
		$('#timepick li').fadeOut('fast');
		$('#timepick').addClass('loading');
		$('#timepick').removeClass('none').attr('title', '');
		clearDateTime();
		if (!$('#calContainer').hasClass('arrow')) {
			$('#calContainer').addClass('arrow');
		}

		$(this).addClass('pick');
		var month = $('span.date .month').html();
		switch (month) {
			case 'January':
				month = 1;
				break;
			case 'February':
				month = 2;
				break;
			case 'March':
				month = 3;
				break;
			case 'April':
				month = 4;
				break;
			case 'May':
				month = 5;
				break;
			case 'June':
				month = 6;
				break;
			case 'July':
				month = 7;
				break;
			case 'August':
				month = 8;
				break;
			case 'September':
				month = 9;
				break;
			case 'October':
				month = 10;
				break;
			case 'November':
				month = 11;
				break;
			case 'December':
				month = 12;
				break;
		}
		
		var day = $(this).html();
		var year = $('span.date .year').html();
		
		// Update available times
		$.post('resources/php/timeFilter.php', { 
			month: month,
			day: day,
			year: year
		},
		function(data){
			// Create a clean JSON object
			var jsonObject = !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
			data.replace(/"(\\.|[^"\\])*"/g, ''))) &&
			eval('(' + data + ')');
			
			for (var i in jsonObject) {
				$('#timepick li a[title="' + jsonObject[i] + '"]').addClass('taken');
				//alert(jsonObject[i]);
			}
			
			// Check to see if any elements are visible
			if ($('#timepick li a').css('visibility') == 'hidden') {
				$('#timepick').addClass('none').attr('title', 'No timeslots available for this day');
			}
			
			
			$('#timepick li').fadeIn('fast', function() {
				$('#timepick').removeClass('loading');
			});
		});
		
		// Update values in hidden input fields
		$('#month').val(month);
		$('#day').val(day);
		$('#year').val(year);
	});
	
	$('#timepick a').click(function() {
		var time = $(this).attr('title');
		$('#time').val(time);
		$('#timepick a').removeClass('pick');
		$(this).addClass('pick');
	});
	
	// make sure day, month and year are selected
	$('#submit').click(function() {
		
		var errorMessage = '<h4>There have been the following errors:</h4></ul>';
		var hasError = false;
		
		var day = $('#day').val();
		var month = $('#month').val();
		var year = $('#year').val();
		if (day == '' || month == '' || year == '') {
			errorMessage += '<li>No day selected</li>';
			hasError = true;
		}
		
		var time = $('#time').val();
		if (time == '') {
			errorMessage += '<li>No time selected</li>';
			hasError = true;
		}
		
		if (hasError == false) {
			return true;
		}
		else {
			$('#results').html(errorMessage + '</ul>').addClass('error').slideDown('slow');
			return false;
		}
		
	});
});