function setMonth(m, y) {
	while (m > 11) {
		m -= 12;
		y++;
	}
	while (m < 0) {
		m += 12;
		y--;
	}
	month = m;
	year = y;
}
function nextMonth() {
	setMonth(month+1, year);
	updateCalendar();
}
function prevMonth() {
	setMonth(month-1, year);
	updateCalendar();
}
function showRow(row, choice) {
	if (choice == 'full') {
		$(row+ '_teaser').style.display = 'none';
		$(row+ '_full').style.display = 'block';
	} else {
		$(row+ '_full').style.display = 'none';
		$(row+ '_teaser').style.display = 'block';
	}
	return false;
}
function updateCalendar() {
	var region = '';
	if ($('atlanta_filter').checked && $('charlotte_filter').checked) {
		region = '';
	} else if ($('atlanta_filter').checked) {
		region='atlanta';
	} else if ($('charlotte_filter').checked) {
		region='charlotte';
	}
	
	//Update Flash
	try {
		$('calendar').jsUpdate(month+1, year, region);
	} catch(ex) {
		try {
			var embed = $('calendar_flash').getElementsByTagName('embed')[0];
			embed.jsUpdate(month+1, year, region);
		} catch (ex) {
			// Failed
		}
	}
	
	//Update JS
	new Ajax.Request('/calendar/dates.xml', {
			method: 'get',
			parameters: {
				month: month+1,
				year: year,
				region: region,
				unique: new Date().getTime()
			},
			onSuccess: function(transport) {
				if (transport.responseXML == null) {
					alert('Unable to retrieve calendar information.');
					return;
				}
				var day_names = new Array('Sun', 'Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat');
				var month_names = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

				var tree = transport.responseXML.getElementsByTagName('calendar')[0];
				var table = $('events');
				//Remove old rows
				while (table.rows.length > 1) {	table.deleteRow(1);	}
				$('calendar_month_name').innerHTML = month_names[month];
				$('calendar_year').innerHTML = year;
				
				var d = new Date();
				var currentRow = 1;
				var tr, td, ev, tmp;
				var rowClass;
				var events = tree.getElementsByTagName('event');

				for (var i=0; i < events.length; i++) {
					ev = events[i];
					
					tr = table.insertRow(currentRow++);
					tr.setAttribute('class', 'spacer');
					tr.setAttribute('className', 'spacer');
					td = tr.insertCell(0);
					td.setAttribute('colSpan', '4');
					td.innerHTML = '&nbsp;';
					
				try {

					if (ev.getElementsByTagName('all_locations')[0].firstChild.nodeValue == '1') {
						rowClass = 'filter_all';
					} else if (ev.getElementsByTagName('locations')[0].getElementsByTagName('location')[0].getElementsByTagName('region')[0].firstChild.nodeValue == 'atlanta') {
						rowClass = 'filter1';
					} else if (ev.getElementsByTagName('locations')[0].getElementsByTagName('location')[0].getElementsByTagName('region')[0].firstChild.nodeValue == 'charlotte') {
						rowClass = 'filter2';
					} else {
						rowClass = 'filter_all';
					}
				} catch (e) {
					rowClass = 'filter_all';
				}
					tr = table.insertRow(currentRow++);
					tr.setAttribute('class', rowClass);
					tr.setAttribute('className', rowClass);
					td = tr.insertCell(0);
					d.setTime(Date.parse(ev.getElementsByTagName('date')[0].firstChild.nodeValue));
					td.innerHTML = '<a name="'+ d.getDate()+ '"></a>'+ 
									'&nbsp;&nbsp;'+
									'<strong>'+ 
										day_names[d.getDay()]+ ', '+ month_names[d.getMonth()]+ ' '+ d.getDate()+
									'</strong>';
					td = tr.insertCell(1);
					td.innerHTML = '<strong>'+ ev.getElementsByTagName('title')[0].firstChild.nodeValue+ '</strong>';
					td = tr.insertCell(2);
					td.setAttribute('rowSpan', '2');
					td.innerHTML = '&nbsp;';
					td = tr.insertCell(3);
					td.setAttribute('rowSpan', '2');
					if (ev.getElementsByTagName('all_locations')[0].firstChild.nodeValue == '1') {
						td.innerHTML = '<strong>All Locations</strong>';
					} else {
						tmp = '';
						for (var j=0; j < ev.getElementsByTagName('location').length; j++) {
							tmp += ev.getElementsByTagName('location')[j].getElementsByTagName('name')[0].firstChild.nodeValue + ', ';
						}
						tmp = tmp.substr(0, tmp.length-2);
						td.innerHTML = '<strong>'+ tmp+ '</strong>';
					}
					
					tr = table.insertRow(currentRow++);
					tr.setAttribute('class', rowClass);
					tr.setAttribute('className', rowClass);
					
					td = tr.insertCell(0);
					td.setAttribute('colSpan', '2');
					td.setAttribute('style', 'padding-bottom:0.5em;');
					if (ev.getElementsByTagName('description')[0].firstChild.nodeValue.length < 500) {
						td.innerHTML = '<div class="description">'+ 
											ev.getElementsByTagName('description')[0].firstChild.nodeValue +
										'</div>';
					} else {
						td.innerHTML = '<div id="'+ i+ '_teaser" class="description">'+
											'<a href="#" onclick="return showRow(\''+ i+ '\', \'full\')" class="more_less">'+
												'<img src="/images/plus.gif" alt="More" />'+
											'</a>'+ ev.getElementsByTagName('teaser')[0].firstChild.nodeValue+ '</div>'+ 
										'<div id="'+ i+ '_full" style="display:none;" class="description">'+ 
											'<a href="#" onclick="return showRow(\''+ i+ '\', \'teaser\')" class="more_less">'+
												'<img src="/images/minus.gif" alt="Less" />'+
											'</a>'+ ev.getElementsByTagName('description')[0].firstChild.nodeValue+ '</div>';
					}
				}
			},
			onFailure: function() {
				alert('Unable to load calendar information');
			}
	});
}

