$( function(){
	
	// add alternate row colouring to timetabel tables 
	$('#timetables table tbody tr:odd, .content table tbody tr:odd').addClass('odd');
	
	// insert the calendar (small)
	//$("#calendar").datepicker({ 
	//   onSelect: function(date) { },
	//	dayNamesMin: ['S', 'M', 'T', 'W', 'T', 'F', 'S'],
	//	firstDay: 1
	//});
	
	//var count  = 0;           // keep record of the event index
	//var events = new Array(); // store the events 
	
	// for each event microformat, insert it in the events array
	//$('li.vevent').each(function() {
	//	events[count]                = new Array();
	//	events[count]['summary']     = $(this).find('h3.summary').text();
	//	events[count]['dtstart']     = $(this).find('abbr.dtstart').attr('title');
	//	events[count]['dtend']       = $(this).find('abbr.dtend').attr('title');
	//	events[count]['location']    = $(this).find('span.location').text();
	//	events[count]['description'] = $(this).find('p.description').text();
	//	events[count]['url']         = $(this).find('a.url').attr('href');
	//	count++;
	//});
	
	// replace the listing with an event calendar container
	//$('#event_calendar_page #listing').replaceWith(
	//	'<div id="events_container">'+
	//	'	<h2>Events 2008 - 2009</h2>'+
	//	'	<form id="control">'+
	//	'		<label for="month">Select month: </label>'+
	//	'		<select id="month" name="month">'+
	//	'			<option>August</option>'+
	//	'			<option>September</option>'+
	//	'			<option>October</option>'+
	//	'		</select>'+
	//	'	</form>'+
	//	'	<div id="events"></div>'+
	//	'	<p>Rollover for details</p>'+
	//	'</div>');
	
	// put the calendar in the events calendar container
	//$("#events").datepicker({ 
	//    onSelect: function(date) { },
	//	dayNamesMin: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
	//	changeYear: false,
	//	firstDay: 1
	//});
	
	// add behaviours to timetable dropdown
	if ($('#timetable_go').length > 0) {
		//console.log('uri: '+window.location.pathname);
		//$("select[@id='date_range'] option[@value="+window.location.pathname).attr('selected','selected');
		$('#timetable_go').click(function() {
			var ob = this.form.date_range;
			if (ob.options[ob.selectedIndex].value == 'select') {return false;}
			window.location.href = ob.options[ob.selectedIndex].value;
			return false;
		});
	}
	swapMonths();
	
	// form behaviours
	$('#quick_booking').submit(function(){ return validForm() });
	$('input[@name=vehicle]').click(function(){ return validRoute() });
});

function validForm() {
	if (document.quick_booking.outRouteId.value == 0) {
		alert ('Please select a route')
		return false
	} 
	else {
		if (document.quick_booking.outSelectMonth.value == 0 || document.quick_booking.outSelectDay.value == 0) {
			alert ('Please select a date')
			return false
		}
		else {
			if (document.quick_booking.outRouteId.value == 2) { document.quick_booking.landmassId.value = 2 }
			if (document.quick_booking.outRouteId.value == 10) { document.quick_booking.landmassId.value = 2 }
			if (document.quick_booking.outRouteId.value == 12) { document.quick_booking.landmassId.value = 2 }
			return true
		}
	}
}

function validRoute() {
	if (document.quick_booking.vehicle[0].checked) {
		document.quick_booking.outRouteId.options[6] = null
		document.quick_booking.outRouteId.options[5] = null
	}
	else {
		var option = new Option("Portsmouth Harbour - Ryde",9)
		document.quick_booking.outRouteId.options[5] = option
		var option = new Option("Ryde - Portsmouth Harbour",10)
		document.quick_booking.outRouteId.options[6] = option
	}
}

function swapMonths() {
	var m_names    = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
	var d          = new Date();
	var curr_date  = d.getDate();
	var curr_month = d.getMonth();
	var curr_year  = d.getFullYear();
	var next_year  = d.getFullYear() + 1;
	
	for (i = 0; i < 12; i++) {
		if (i + curr_month < 12) {
			document.quick_booking.outSelectMonth.options[i + 1].text = m_names[curr_month + i] + ' ' + curr_year
		}
		else {
			document.quick_booking.outSelectMonth.options[i + 1].text = m_names[curr_month + i - 12] + ' ' + next_year
		}
	}
}
