// JavaScript Document
var monthXMLs = new Object();
			
function getXML(month, year) {
	var response = null;

	// Our AJAX request.
	var targetParams =  'year=' + year + '&month=' + month;
	//var t = prompt(targetParams,targetParams);
	var myXHR = new XHR({method: 'get', async:false}).send('/xmlFiles/getDaysWithEventsXML.cfm', targetParams);
	
	return parseMonth(myXHR.transport.responseText);
}

function getDayXML(month, year, day, totalEvents) {
	var response = null;

	// Our AJAX request.
	var targetParams =  'year=' + year + '&month=' + month + '&day=' + day + '&totalEventsToShow=' + totalEvents;
	var myXHR = new XHR({method: 'get', async:false}).send('/xmlFiles/getEventsForDayXML.cfm', targetParams);
	
	return parseXML(myXHR.transport.responseText);
}

function parseXML(someXMLText) {
	var doc = "";
	if (document.implementation.createDocument) { // mozilla
		var parser = new DOMParser();
		doc = parser.parseFromString(someXMLText, 'text/xml');
	} else if (window.ActiveXObject) { // ie
		doc = new ActiveXObject('Microsoft.XMLDOM');
		doc.async = 'false';
		doc.loadXML(someXMLText);
	}
	
	return doc;
}


function getXMLPath(somePattern, someXML) {
	try { //mozilla
		results = someXML.evaluate( somePattern, someXML, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
	} catch (e) { // IE
		results = someXML.selectNodes( somePattern );
	}
	
	return results;
}

function getXMLNode(nodeList, listIndex) {
	try { //mozilla
		result = nodeList.snapshotItem(listIndex)
	} catch (e) { // IE
		result = nodeList[listIndex];
	}
	
	return result;
}

function parseMonth(someXML) {
	var xml = parseXML(someXML);
	
	var findPattern = "//day";
	var resultLinks = getXMLPath(findPattern,xml);
	
	var activeDaysList = ",";
	// loop through all the days
	var i=0; 
	while ( (res = getXMLNode(resultLinks,i) ) !=null ){ 
		//do something to the link
		activeDaysList = activeDaysList + res.getAttribute("dayOfMonth") + ",";
		i++;
	}
	
	return activeDaysList;
	
}

function getTheMonth(date) {
	return date.getMonth() + 1;
	/*if ( ! document.all) {// IE
		return (1*date.getMonth()) + 1;
	} else {
		return date.getMonth();
	}*/
	
}

function getTheYear(date) {
	var thisYear = date.getYear();
	if ( (thisYear > 99) && (thisYear < 1900) ){
		return thisYear + 1900;
	} else {
		if (thisYear < 100) {
			return thisYear + 2000;
		}
	}
	
	return thisYear;
}

function checkMonth(date) {
	month = getTheMonth(date);
	year = getTheYear(date);
	
	if (! monthXMLs["" + month + year]) {
		// if we dont have the month yet, then lets go get it
		monthXMLs["" + month + year] = getXML(month, year);
	}
	
	var thisMonthsList = monthXMLs["" + month + year];
	//alert("{" + month + year + "}..." + date.getDate() + "..." + thisMonthsList + ": "  + thisMonthsList.indexOf(',' + date.getDate() + ','));
	if (thisMonthsList.indexOf(',' + date.getDate() + ',') > -1) {
		return false;
	} else {
		return true;
	}
}

function setClass(someElement, someClass) {
	//alert(someElement + "-" + someClass);
	if (! someClass) {
		var targetClass = someElement.getAttribute("originalClassName");
	} else {
		someElement.setAttribute("originalClassName", someElement.className);
		var targetClass = someClass;
	}
	
	someElement.className = targetClass;
}

var calendarContainerClosed = true;
var calendarContainer = "";
var calendarFx = false;

function showHideCalendar() {
	calendarContainer = $("calendarContainer");

	if (! calendarFx) {
		calendarFx = new Fx.Styles(calendarContainer, {duration: 300, wait: false, transition: Fx.Transitions.Sine.easeInOut});
	}

	if (calendarContainerClosed) {
		// if its closed then we need to open it
		calendarContainerClosed = false;
		calendarFx.start({'height':'185px'}).chain(function(){
			//calendarContainer.setStyle("overflow","");
		});
	} else {
		// if its open then we need to close it
		calendarContainerClosed = true;
		//calendarContainer.setStyle("overflow","hidden");
		//alert(5);
		calendarFx.start({'height':'6px'});
	}
}


function isDisabled(date) {
	return checkMonth(date);
}

			
function getRowHTML(someTitle, someDate, someLink) {
	var div = new Element('div').addClass("row").setStyles({"width":"180px"});
		var p = new Element('p').addClass("list-el");
			var a = new Element('a').appendText(someTitle);
			a.setProperties({"href":someLink});
				a.injectInside(p);
				p.appendText(someDate);
				p.injectInside(div);
	
		
	return div;
}

function getEventHeader(headerText) {
	/*var aTable = new Element('table');
	var aTR = new Element('tr');
	var aTD = new Element('td').addClass("homepageEventHeader").appendText(headerText);
	aTD.injectInside(aTR);
	aTR.injectInside(aTable);
	
	*/
	var div = new Element('div').addClass("homepageEventHeader").appendText(headerText);
	return div ;
}

function getDayText(someDate) {
	var day = someDate.getDate();
	var month= monthNames[someDate.getMonth()];
	var year = getTheYear(someDate);
	
	return month + " " + day + ", " + year;
}

function getEventsForDay(specificDate) {
	// clear out the old events
		var standardEventsSlider = new Fx.Styles('standardCalendarEvents', {duration: 500, wait: false, transition: Fx.Transitions.Sine.easeInOut});
		
		//$("standardCalendarEvents").setStyle("display","none");
	
	// load in the new events
		var thisDaysXML = getDayXML(getTheMonth(specificDate), getTheYear(specificDate), specificDate.getDate(), 7);
		var todaysEvents = getXMLPath("//requestedDay/event", thisDaysXML);
		
		$("standardCalendarEvents").setStyles({"height":0,"overflow":"hidden","padding":0, "display":"none"});
		//standardEventsSlider.start({"height":0,"padding":0}); //toggle the slider up and down.
		
		// clear out any existing items
			$("todaysCalendarEvents").empty();
			$("otherCalendarEvents").empty();
			
		// show the header
			//and put in the new events
			var i=0; 
			while ( (res = getXMLNode(todaysEvents,i) ) !=null ){ 
				
				if (i == 0) {
					// if this is the first item
					getEventHeader("Events for " + getDayText(specificDate)).injectInside( 
						$("todaysCalendarEvents")
					);
				}
				//do something to the link
				getRowHTML( res.getAttribute("title"), res.getAttribute("date"), res.getAttribute("target") ).injectInside( 
					$("todaysCalendarEvents")
				);
				i++;
			}
			
			var todayObject = $("todaysCalendarEvents")
			todayObject.setStyles({"padding":10,"height":"auto","opacity":0, "display":"block"});
			var todayEventsSlider = new Fx.Styles('todaysCalendarEvents', {duration: 500});
			todayEventsSlider.start({"opacity":[0,1]});
			
			
		// if there are not enough events
			var thisDaysXML = getDayXML(getTheMonth(specificDate), getTheYear(specificDate), specificDate.getDate(), 7);
			var todaysEvents = getXMLPath("//otherEvents/event", thisDaysXML);
			
			// show the other header
				//and put in the other events
				var i=0; 
				while ( (res = getXMLNode(todaysEvents,i) ) !=null ){ 
					if (i == 0) {
						// if this is the first item
						getEventHeader("Other Events").injectInside( 
							$("otherCalendarEvents")
						);
					}
				
					//do something to the link
					getRowHTML( res.getAttribute("title"), res.getAttribute("date"), res.getAttribute("target") ).injectInside( 
						$("otherCalendarEvents")
					);
					i++;
					
				}
				
				if (i > 0) {
					// if there were any non-today events then show the other header
					//$("otherCalendarEvents").setStyle("padding","");
					//$("otherCalendarEvents").setStyle("height","auto");
					//var otherEventsSlider = new Fx.Slide('otherCalendarEvents', {duration: 500});
					//otherEventsSlider.slideIn();
					var otherObject = $("otherCalendarEvents");
					otherObject.setStyles({"display":"block"});
					if (document.all) {
						otherObject.setStyles({"padding":10,"height":otherObject.scrollHeight + 1,"opacity":0});
					} else {
						otherObject.setStyles({"padding":10,"height":"auto","opacity":0});
					}
					var otherEventsSlider = new Fx.Styles('otherCalendarEvents', {duration: 500});
					otherEventsSlider.start({"opacity":[0,1]});
					
					
				}
	
}

var monthNames = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];

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 newDateString = monthNames[calendar.date.getMonth()] + " " + calendar.date.getDate() + ", " + getTheYear(calendar.date);
		$("homepageCalendarDate").setHTML(newDateString);
		//alert(newDateString);
		getEventsForDay(calendar.date);
	}
}
