// JavaScript Document
window.onload = importXML;		// Start the import
var doCreate = true;			// If we don't do this trigger, then the calendar explodes
var currentDate;				// Holder for the date that is hovered over
var SPECIAL_DAYS = new Array(); // All the dates in the event XML
var eventXML;

// Create the calendar
function startCal()
{
	// Setup the calendar
	Calendar.setup(
		{
			flat         : "cal", 			// ID of the parent element
			flatCallback : dateChanged,     // our callback function
			weekNumbers  : false,
			dateStatusFunc : ourDateStatusFunc
		}
	);			
}

// On window load, imports the XML file to find the special days
function importXML()
{
	getEventXML();
}

/**
 * build the calendar function after retrieving events xml successfully
 */
var validStatus = function(e){
	eventXML = e.responseXML;
	toggle("calendar");
	if (doCreate)
	{
		if ((document.implementation && document.implementation.createDocument) || window.ActiveXObject)
		{
			findDates();
			startCal();
		}
		else
		{
			alert('Your browser can\'t handle this script');
			return;
		}
		doCreate = false;
	}
}

/**
 * sends a status text when retrieving the events data failed
 */
var invalidStatus = function(e) {
	//alert(e.statusText);
};

/** 
 * sets call function to call depending and status returned 
 */
var returnStatus = { 
	success: validStatus, 
	failure: invalidStatus,
	argument: {} 
};
	
/**
 * this function calls the jsp to return event data in xml format
 */
function getEventXML() {
	var sUrl = "/events/events_calendar_xml.jsp";
	YAHOO.util.Connect.asyncRequest('GET', sUrl, returnStatus);
}

/**
 * Sets the special dates
 */
function findDates()
{
	var x = eventXML.getElementsByTagName('event');
	for (i = 0; i < x.length; i++)
	{
		var month = x[i].getAttribute("month") - 1;
		var day = x[i].getAttribute("day");
		var year = x[i].getAttribute("year");
		
		if (!SPECIAL_DAYS[month])					
			SPECIAL_DAYS[month] = new Array();
		
		SPECIAL_DAYS[month][day] = day;
	}
	return true;
}
		
/**
 * Checks for a special day
 */
function dateIsSpecial(year, month, day)
{
	var m = SPECIAL_DAYS[month];
	if (!m) return false;
	for (var i in m) if (m[i] == day) return true;
	return false;
};

/**
 * Returns the CSS class of a special day
 */
function ourDateStatusFunc(date, y, m, d)
{
	if (dateIsSpecial(y, m, d))
		return "special";
	else
		return false; 	// other dates are enabled
						// return true if you want to disable other dates
};

/**
 * Imports the XML file and calls the getEvent function
 */
function getXML()
{
	if ((document.implementation && document.implementation.createDocument) || window.ActiveXObject)
	{
		getEvent();
	}
	else
	{
		alert('Your browser can\'t handle this script');
		return;
	}
}

/**
 * Finds the event, then calls the ToolTip methods
 */
function getEvent()
{
	var x = eventXML.getElementsByTagName('event');
	for (i = 0; i < x.length; i++)
	{
		var month = x[i].getAttribute("month");
		var day = x[i].getAttribute("day");
		var year = x[i].getAttribute("year");
		var date = month + "/" + day +"/" + year;
		if ( date == currentDate)
		{
			var thisEvent = document.createElement("DIV");
																		
			var thisInfo = document.createElement("DIV");							
			var theDate = document.createTextNode(date);
			thisInfo.appendChild(theDate);
			
			var titleInfo = document.createElement("DIV");
			var title = document.createTextNode(x[i].getAttribute("title"));
			titleInfo.appendChild(title);
			
			var speakerInfo = document.createElement("DIV");
			var speaker = document.createTextNode(x[i].getAttribute("speaker"));
			speakerInfo.appendChild(speaker);
			
			thisEvent.appendChild(thisInfo);
			thisEvent.appendChild(titleInfo);
			thisEvent.appendChild(speakerInfo);	
			
			// Clear out the event box
			removeAllChildNodes(document.getElementById('eventList'));
			document.getElementById('eventList').appendChild(thisEvent);
			
			// Call the ToolTip, referring to the event box
			TagToTip('eventList');
		}
	}
}

/**
 * Clears out the 'eventList' DIV
 */
function removeAllChildNodes(node)
{
	if (node && node.parentNode && node.parentNode.replaceChild && node.cloneNode)
	{
		node.parentNode.replaceChild(node.cloneNode(false), node);
	}
}

/**
 * This function is called when the user rolls over the calendar
 * It's called from calendar.js
 */
function getTip(date)
{
	var thisDate = new Date(date);
	var m = thisDate.getMonth() + 1;
	var d = thisDate.getDate();
	var y = thisDate.getFullYear();
	currentDate = m + "/" + d + "/" + y;
	if (currentDate != "undefined" && currentDate != null)
	{
		// Retrieve the XML and find the event for the date
		getXML();
	}
}

/**
 * Callback function when the user clicks on the calendar
 */
function dateChanged(calendar) 
{
	// Beware that this function is called even if the end-user only
	// changed the month/year.  In order to determine if a date was
	// clicked you can use the dateClicked property of the calendar:
	
	if (calendar.dateClicked) {
		var y = calendar.date.getFullYear();
		var m = calendar.date.getMonth();     // integer, 0 to 11
		var d = calendar.date.getDate();      // integer, 1 to 31
		var id = (m + 1) + "/" + d + "/" + y;				
		var x = eventXML.getElementsByTagName('event');
		for (i = 0; i < x.length; i++)
		{
			var month = x[i].getAttribute("month");
			var day = x[i].getAttribute("day");
			var year = x[i].getAttribute("year");
			var date = month + "/" + day +"/" + year;
			if ( date == currentDate)
			{
				var id = x[i].getAttribute("id");
				window.location = "/events/ev_calendar.cgi?eid="+id;
			}
		}
	}
}

/**
 * this function toggles back and forth between the event calendar and 
 * event search 
 */
function toggle(toggleType) {
	if(toggleType=="searchEvent") {
		var searchEvent = document.getElementById("searchEvent");
		searchEvent.style.display = "block";
		var calendar = document.getElementById("calendar");
		calendar.style.display = "none";
	}
	else {
		var searchEvent = document.getElementById("searchEvent");
		searchEvent.style.display = "none";
		var calendar = document.getElementById("calendar");
		calendar.style.display = "block";
	}
}
