/*@cc_on @if (@_win32 && @_jscript_version >= 5) if (!window.XMLHttpRequest)
window.XMLHttpRequest = function() { return new ActiveXObject('Microsoft.XMLHTTP') }
@end @*/

var gYear = 0;
var gMonth = 0;

function buildCalendar(month, year)
{
	var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
	var date = new Date();
	if(year && month)
		date.setFullYear(year, month-1, 1);
	else
		date.setFullYear(date.getFullYear(), date.getMonth(), 1);
	
	var daysInMonth = getDaysInMonth(date);
	var d = 0 - date.getDay() + 1;
	var tbody = document.getElementById("calendarBody");

	gYear = date.getFullYear();
	gMonth = date.getMonth()+1;
	
	setElementText(document.getElementById("calendarMonth"), months[date.getMonth()] + " " + date.getFullYear());

	clearElement(tbody);

	while(d <= daysInMonth)
	{
		tr = document.createElement("tr");
		for(var i = 0; i < 7; i++, d++)
		{
			td = document.createElement("td");
			if(d > 0 && d <= daysInMonth)
			{
				var a = document.createElement("a");
				a.href = "#";
				a.day = d;
				a.onclick = dateClicked;
				setElementText(a, d);
				td.appendChild(a);
			}
			else
			{
				setElementText(td, "\u00a0");
			}
			tr.appendChild(td);
		}
		tbody.appendChild(tr);
	}
	
	hideFloat();
}

function previousMonth()
{
	if(gMonth > 1)
		buildCalendar(gMonth-1, gYear);
	else
		buildCalendar(12, gYear-1);
}

function nextMonth()
{
	if(gMonth < 12)
		buildCalendar(gMonth+1, gYear);
	else
		buildCalendar(1, gYear+1);
}

function setElementText(o, t)
{
	clearElement(o);
	o.appendChild(document.createTextNode(t));
}
	  
function clearElement(o)
{
	while(o.firstChild)
		o.removeChild(o.firstChild);
}

function dateClicked()
{
	showFloat(gYear, gMonth, this.day);
	return false;
}

function hideFloat()
{
	document.getElementById("calendarFloat").style.display = "none";
}

function showFloat(year, month, day)
{
	var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
	var ext, digit;
	digit = day % 10;
	if(digit == 1 && day != 11)
		ext = "st";
	else if(digit == 2 && day != 12)
		ext = "nd";
	else if(digit == 3 && day != 13)
		ext = "rd";
	else
		ext = "th";

	var layer = document.getElementById("calendarFloat");
	
	clearElement(layer);
	
	var div = document.createElement("div");
	var h3 = document.createElement("h3");
	h3.appendChild(document.createTextNode(months[month-1] + " " + day + ext + ", " + year));
	div.appendChild(h3);
	
	var httpRequest = new XMLHttpRequest();
	httpRequest.onreadystatechange = function()
	{
		if(httpRequest.readyState == 4 && httpRequest.status != 200)
			alert("Unable to load data.");
		else if(httpRequest.readyState == 4)
		{
			if(!httpRequest.responseXML)
			{
				alert(httpRequest.responseText);
				return;
			}

			if(httpRequest.responseXML.firstChild.nodeName != "content" || httpRequest.responseXML.firstChild.firstChild.nodeName != "url" || !httpRequest.responseXML.firstChild.firstChild.firstChild)
			{
				alert("Malformed XML response");
				return;
			}

			var url = httpRequest.responseXML.firstChild.firstChild.firstChild.nodeValue;

			for(var i = 1; i < httpRequest.responseXML.firstChild.childNodes.length; i++)
			{
				if(
					httpRequest.responseXML.firstChild.childNodes[i].childNodes.length != 3 ||
					!httpRequest.responseXML.firstChild.childNodes[i].childNodes[0].firstChild ||
					!httpRequest.responseXML.firstChild.childNodes[i].childNodes[1].firstChild ||
					!httpRequest.responseXML.firstChild.childNodes[i].childNodes[2].firstChild
				)
				{
					alert("Missing XML entry");
					return;
				}
				
				if(
					httpRequest.responseXML.firstChild.childNodes[i].childNodes[0].nodeName != 'url' ||
					httpRequest.responseXML.firstChild.childNodes[i].childNodes[1].nodeName != 'title' ||
					httpRequest.responseXML.firstChild.childNodes[i].childNodes[2].nodeName != 'teaser'
				)
				{
					alert("Malformed XML entry");
					return;
				}
				
				var p = document.createElement("p");
				if(i == 1)
					p.className = "first";
				
				var a = document.createElement("a");
				a.href = httpRequest.responseXML.firstChild.childNodes[i].childNodes[0].firstChild.nodeValue;
				a.appendChild(document.createTextNode(httpRequest.responseXML.firstChild.childNodes[i].childNodes[1].firstChild.nodeValue));
				p.appendChild(a);
				
				p.appendChild(document.createTextNode(" " + httpRequest.responseXML.firstChild.childNodes[i].childNodes[2].firstChild.nodeValue));
				
				div.appendChild(p);
			}

			var ul2 = document.createElement("ul");
			ul2.className = "btns clearing";
			ul2.style.cssFloat = "right";
			ul2.style.styleFloat = "right";
			var li2 = document.createElement("li");
			var a2 = document.createElement("a");
			a2.className = "btns";
			a2.href = "#";
			a2.onclick = function() { hideFloat(); return false; };
			a2.appendChild(document.createTextNode("CLOSE"));
			li2.appendChild(a2);
			ul2.appendChild(li2);
			div.appendChild(ul2);
		
			var ul = document.createElement("ul");
			ul.className = "btns clearing";
			var li = document.createElement("li");
			var a = document.createElement("a");
			a.className = "btns";
			a.href = url;
			a.appendChild(document.createTextNode("VIEW ALL EVENTS"));
			li.appendChild(a);
			ul.appendChild(li);
			div.appendChild(ul);

			return;
		}
	};
	httpRequest.open("GET", "events-" + year + "-" + month + "-" + day + ".xml", true);
	httpRequest.send(null);
	
	layer.appendChild(div);

	layer.style.display = "block";
}

function getDaysInMonth(d)
{
	var date = new Date(d);
	date.setFullYear(date.getFullYear(), date.getMonth() + 1, 0);
	return date.getDate();
}