// JavaScript Document
window.addEvent('domready', function() {
	SetupCalendar(); 
});

function SetupCalendar() {
	 $$('#previousMonthButton').addEvent('click', function(event) {
		event.stop();
		GetPreviousMonth(this.href);
	});
	
	$$('#nextMonthButton').addEvent('click', function(event) {
		event.stop();
		GetNextMonth(this.href);
	});
	
};

function GetPreviousMonth(targ) {
	targ = window.location.protocol + '//' + window.location.hostname + targ.substring(targ.indexOf('?url=')+5) + '&xml=True';
	var xml = new Request({
		url:targ,
		autoCancel: true,
		onSuccess: function(html) {
			$$('#calendarWrapper').set('html',html);
			$$('#previousMonthButton').addEvent('click', function(event) {
				event.stop();
				GetPreviousMonth(this.href);
			});
			$$('#nextMonthButton').addEvent('click', function(event) {
				event.stop();
				GetNextMonth(this.href);
			});
		},
		onFailure: function(error) {
			alert('There is currently a problem with the calendar please try again later');
		}
	}).send(); //Ends xml request
};

function GetNextMonth(targ) {
	targ = window.location.protocol + '//' + window.location.hostname + targ.substring(targ.indexOf('?url=')+5) + '&xml=True';
	var xml = new Request({
		url:targ,
		autoCancel: true,
		onSuccess: function(html) {
			$$('#calendarWrapper').set('html',html);
			$$('#previousMonthButton').addEvent('click', function(event) {
				event.stop();
				GetPreviousMonth(this.href);
			});
			$$('#nextMonthButton').addEvent('click', function(event) {
				event.stop();
				GetNextMonth(this.href);
			});
		},
		onFailure: function(error) {
			alert('There is currently a problem with the calendar please try again later');
		}
	}).send(); //Ends xml request
};
