// JavaScript Document


var RSSRequestObject = false; // XMLHttpRequest Object
var Backend = 'http://'+document.domain+'/eventrss.php'; // Backend url
window.setInterval("update_timer()", 1200000); // update the data every 20 mins

if (window.XMLHttpRequest) // try to create XMLHttpRequest
	RSSRequestObject = new XMLHttpRequest();

if (window.ActiveXObject)	// if ActiveXObject use the Microsoft.XMLHTTP
	RSSRequestObject = new ActiveXObject("Microsoft.XMLHTTP");


/*
* onreadystatechange function
*/
function ReqChange() {

	// If data received correctly
	if (RSSRequestObject.readyState==4) {
	
		// if data is valid
		if (RSSRequestObject.responseText.indexOf('invalid') == -1) 
		{ 	
			// Parsing Feeds
			var node = RSSRequestObject.responseXML.documentElement; 
			
			// Get the calendar title
			var title = node.getElementsByTagName('title').item(0).firstChild.data;
			
			var content = '';
			var madeFirstEvent = 0;
		
			// Browse events
			var items = node.getElementsByTagName('entry');
			if (items.length == 0) {
				content += '<ul><li><div class="eventrsserror">No events</div></li></ul>';
			} else {
				for (var n=0; n < items.length; n++)
				{
					try {
						
						// DEFINE THE VARIABLES
						var itemTitle = items[n].getElementsByTagName('title').item(0).firstChild.data;
						var summary = items[n].getElementsByTagName('summary').item(0).firstChild.data;
						var itemLink = items[n].getElementsByTagName('link').item(0).getAttribute("href");
											
						// PREP FOR PRINTING
						itemTitle = truncate(itemTitle, 100);
						var dateStart = summary.indexOf('When: ',0)+6;
						var dateEnd = summary.indexOf('2010',0)+4;
						if (dateEnd <= dateStart) { dateEnd = summary.indexOf('2011',0)+4; }
						if (dateEnd <= dateStart) { dateEnd = summary.indexOf('2012',0)+4; }
						if (dateEnd <= dateStart) { dateEnd = summary.indexOf('2013',0)+4; }
						if (dateEnd <= dateStart) { dateEnd = summary.indexOf('2014',0)+4; }
						if (dateEnd <= dateStart) { dateEnd = summary.indexOf('2015',0)+4; }
						if (dateEnd <= dateStart) { dateEnd = summary.indexOf('2016',0)+4; }
						if (dateEnd <= dateStart) { dateEnd = summary.indexOf('2017',0)+4; }
						if (dateEnd <= dateStart) { dateEnd = summary.indexOf('2018',0)+4; }
						if (dateEnd <= dateStart) { dateEnd = summary.indexOf('2019',0)+4; }
						if (dateEnd <= dateStart) { dateEnd = summary.indexOf('2020',0)+4; }
						if (dateEnd <= dateStart) { dateEnd = summary.indexOf('2021',0)+4; }
						if (dateEnd <= dateStart) { dateEnd = summary.indexOf('2022',0)+4; }
						if (dateEnd <= dateStart) { dateEnd = summary.indexOf('2023',0)+4; }
						if (dateEnd <= dateStart) { dateEnd = summary.indexOf('2024',0)+4; }
						if (dateEnd <= dateStart) { dateEnd = summary.indexOf('2025',0)+4; }
						if (dateEnd <= dateStart) { dateEnd = summary.indexOf('2026',0)+4; }
							// Shame on us if we go for 15 years without updating
						var date = summary.substring(dateStart,dateEnd);
						
						var timeStart = dateEnd+1;
						var timeEnd = summary.indexOf('EDT',0)-1;
						if (timeEnd <= 0) { 
							timeEnd = summary.indexOf('EST',0)-1;
						}
						if (timeEnd <= 0) {  // IF NO EDT OR EST, THEN IT'S ALL-DAY, THUS NO TIME
							var time = ''
						}
						else { 	var time = summary.substring(timeStart,timeEnd); }
						
						var whereStart = summary.indexOf('Where: ',0)+7;
						if (whereStart <= 6) {  // IF IT COULDN'T FIND 'Where:', THERE'S NO PLACE
							var where = '';
						}
						else {
							var whereEnd = summary.indexOf('Event Status:',0);
							var where = summary.substring(whereStart,whereEnd);
							where = truncate(where, 30);
						}
						
						if (madeFirstEvent == 0) {content += '<ul>'; madeFirstEvent = 1;}
						content += '<li><a href="'+ 
								   itemLink+
								   '&ctz=America/New_York" target="_blank" onclick="open_win(\''+
								   itemLink+  
								   '&ctz=America/New_York\'); return false;">'+itemTitle+' </a>'+
						'<ul>'+
						  '<li>'+date+'</li>';
						if (!time) {} else { content +=  // UNLESS THERE'S NO TIME, DISPLAY THE TIME
						  '<li>'+time+'</li>'; }
						if (!where) {} else { content += // UNLESS THERE'S NO PLACE, DISPLAY THE PLACE
						  '<li>'+where+'</li>'; }
						content += '</ul></li>';
						if (madeFirstEvent == 1 && n == (items.length-1)) {content += '</ul>';}
					}
					catch(err) {
						document.getElementById("status").innerHTML = '<div class="eventrsserror">Error requesting data. Click the Event Calendar link above to access the calendar directly.<div>';}
				}

			}
			
			// Display the result
			document.getElementById("ajaxreader").innerHTML = content;

			// Tell the reader the everything is done
			document.getElementById("status").innerHTML = "Done.";
			
		}
		else {
			// Tell the reader that there was error requesting data
			document.getElementById("status").innerHTML = "<div class=\"eventrsserror\"> Error requesting data. Click the Event Calendar link above to access the calendar directly.<div>";
			HideShow('status');
		}
		
		HideShow('status');
	}
	
}


// Input parameters:
// String text, [Number length, String ellipsis]
//
// Returns:
// String text
function truncate(text, length) {    

	var ellipsis = '...'

    // Set length and ellipsis to defaults if not defined
    if (typeof length == 'undefined') var length = 100;
    if (typeof ellipsis == 'undefined') var ellipsis = '...';

    // Return if the text is already lower than the cutoff
    if (text.length < length) return text;

    // Otherwise, check if the last character is a space.
    // If not, keep counting down from the last character
    // until we find a character that is a space
    for (var i = length-1; text.charAt(i) != ' '; i--) {
        length--;
    }

    // The for() loop ends when it finds a space, and the length var
    // has been updated so it doesn't cut in the middle of a word.
    return text.substr(0, length) + ellipsis;
}


/*
* Main AJAX RSS reader request
*/
function RSSRequest() {

	// change the status to requesting data
	HideShow('status');
	document.getElementById("status").innerHTML = "Requesting data ...";
	
	// Prepare the request
	try {
		RSSRequestObject.open("GET", Backend , true);
		// Set the onreadystatechange function
		RSSRequestObject.onreadystatechange = ReqChange;
		// Send
		RSSRequestObject.send(null); 
	}
	catch(err) {
		document.getElementById("status").innerHTML = "<div class=\"eventrsserror\"> Error requesting data. Click the Event Calendar link above to access the calendar directly.<div>";
		HideShow('status');
	}
}

/*
* Timer
*/
function update_timer() {
	RSSRequest();
}

function HideShow(id){
	var el = GetObject(id);
	if(el.style.display=="none")
	el.style.display='';
	else
	el.style.display='none';
}

function GetObject(id){
	var el = document.getElementById(id);
	return(el);
}


