/*
	Upcoming Chapter Events
	Author:			John W. Hill - jowhill -at- indiana.edu
	Creation Date:	11/30/2007
	Last Updated:	12/07/2007
	
	Updated to move from Prototype to jQuery: 07/09/2010 by Orion Day
	
	This script reads chapter events from the proper XML file, parses it, and then displays events
	
	(!) Before this script is included two javascript variables must be defined in the referencing document:
		chapter, and numEvents
		Example:
			var calendar		= "monroe";
			var numEvents 		= 3;
*/

function myCalendars() {
	this.international = "GRP1960";
	this.asheville = "GRP2280"
    this.asr = "GRP1362";
    this.bartholomew = "GRP1370";
    this.birmingham = "GRP1374";
    this.boise = "GRP1376";
    this.boone = "GRP1381";
    this.boston = "GRP1384";
    this.carolina = "GRP1391";
    this.cleveland = "GRP1411";
    this.centralflorida = "GRP1394";
    this.centralillinois = "GRP1397";
    this.indy = "GRP1399";
    this.iowa = "GRP1402";
    this.cass = "GRP1388";
    this.charlotte = "GRP1404";
    this.chicago = "GRP1406";
    this.connecticut = "GRP1413";
    this.dallas = "GRP1416";
	this.daneco = "GRP1418";
    this.denver = "GRP1421";
    this.dc = "GRP1359";
    this.eastIndiana = "GRP1423";
    this.elkhart = "GRP1425";
	this.england = "GRP1428";
    this.sunCoast = "GRP1431";
    this.cincinnati = "GRP1435";
    this.evansville = "GRP1437";
    this.greensboro = "GRP1439";
    this.greenvilleSC = "GRP1441";
    this.hilton = "GRP1442";	
    this.howard = "GRP1448";
    this.houston = "GRP1446";
    this.jacksonville = "GRP1477";
    this.johnson = "GRP1480";
    this.kosciusko = "GRP1482";
    this.kokomo = "GRP1466";
    this.lakeshore = "GRP1486";
    this.lexington = "GRP1493";
    this.losAngeles = "GRP1484";
    this.louisville = "GRP1495";
    this.madison = "GRP1497";
    this.manasota = "GRP1431";
    this.marshall = "GRP1499";
	this.miami = "GRP1660";
    this.nashville = "GRP1801";
    this.monroe = "GRP1503";
    this.newYork = "GRP1506";
    this.northeastIndiana = "GRP1508";
    this.orange = "GRP1510";
    this.palmBeach = "GRP1533";
    this.pittsburgh = "GRP1516";
    this.phoenix = "GRP1514";
    this.philadelphia = "GRP1512";
    this.portland = "GRP1518";
	this.sacramento = "GRP1522";
    this.sanAntonio = "GRP1523";
    this.sanDiego = "GRP1525";
    this.seattle = "GRP1531";
    this.southSanFrancisco = "";
    this.SanFrancisco = "GRP1527";
    this.southwestFlorida = "GRP1534";
    this.southwestIndiana = "GRP1535";
    this.stJoseph = "GRP1536";
    this.stLouis = "GRP1537";
    this.tampaBay = "GRP1539";
    this.thailand = "GRP1540";
    this.toledo = "GRP1541";
	this.tucson = "GRP1661";
    this.twinCities = "GRP1542";
    this.westCentralIndiana = "GRP1544";
    this.vegas = "GRP1488";
	this.vincennes = "GRP1927";
    this.asian = "GRP1365";
    this.nealMarshall = "GRP1505";
    this.glbtaa = "GRP1433";
    this.hollywood = "GRP1444";
    this.latino = "GRP1491";
    this.saa = "GRP1460";
    this.singingHoosiers = "GRP1532";
    this.education = "";
	this.iuaa = "GRP1358";
    this.iub = "GRP1462";
    this.iupuc = "GRP1471";
    this.iueast = "GRP1464";
    this.ipfw = "GRP1456";
	this.iupui = "GRP1473";
    this.iuk = "GRP1466";
    this.iunw = "GRP1468";
    this.iusb = "GRP1475";
    this.iuse = "GRP1458";
    this.iassociation = "GRP1449";
    this.iassoc = "GRP1449";
    this.austin = "GRP1368";
	this.rhaa = "GRP1520";

}

$(document).ready(function() {
if (typeof(calendar) != 'undefined' ) {
var myCal = new myCalendars();
// create some variables to use
var url = "/inc/events/" + myCal[calendar] + ".xml";
var eventsDiv		= $('#events');
var calendarLink	= $('#calendarLink');
var titles			= [];
var links			= [];
var descriptions	= [];
var month			= new Array( 12 );
month[0]			= "Jan";
month[1]			= "Feb";
month[2]			= "Mar";
month[3]			= "Apr";
month[4]			= "May";
month[5]			= "Jun";
month[6]			= "Jul";
month[7]			= "Aug";
month[8]			= "Sep";
month[9]			= "Oct";
month[10]			= "Nov";
month[11]			= "Dec";

var feedDate		= new Date();
var feedYear		= feedDate.getFullYear();
var feedMonth		= feedDate.getMonth() + 1;
var feedDay			= feedDate.getDate();

var endDate			= new Date();
endDate.setDate(endDate.getDate()+180);
var endYear			= endDate.getFullYear();
var endMonth		= endDate.getMonth() + 1;
var endDay			= endDate.getDate();

var calID			= myCal[calendar];
var feedURL			= "https://onestart.iu.edu/ccl-prd/SyndicationWriter.do?pubCalId=";
feedURL				+= calID + "&startDate="+feedYear+"-"+feedMonth+"-"+feedDay + "&endDate="+endYear+"-"+endMonth+"-"+endDay;
//Add the RSS Feed link within the Calendar
$("<a id=\"eventsWidgetRSSIcon\" href=\"" + feedURL + "\" title=\"Subscribe to the upcoming events RSS feed\"><img src=\"/img/rss.gif\" alt=\"RSS\" /></a>").prependTo(eventsDiv);
//Update the Full Calendar URL
var calURL = "https://onestart.iu.edu/ccl-prd/GroupCalendar.do?methodToCall=publicCalendar&pubCalId=" + calID;
calendarLink.attr('href', calURL);

// add the loading image div just below the #events h2
$('<div style="margin:5px 55px 5px 55px;" id="loadingBox"><img src="/img/icons/loading.gif" alt="Loading Image" /></div>').insertAfter('#events h2');
$loadingBox		= $('#loadingBox'); // grab it for later use

$.ajax({
    url : url,
    method : 'GET',
    dataType : 'xml',
    success : function(xml) {
		eventsFeedParser(xml);
    },
    error : function(xhr, status) {
		printEventError("Error loading events, please check the calendar.", "exclamation.png");
		//sendFeedError();
    }/*,

    // code to run regardless of success or failure
    complete : function(xhr, status) {

    }*/
});

}
});

// load the xml (comes as a string) into an actual xml document, browser side
// then parse out the events, show the upcoming events, and print any errors if they occur
function eventsFeedParser(xml)
{
	var events			= xml.getElementsByTagName("item"); // get all of the events, called item(s)
	var maxEvents		= Math.min(numEvents, events.length); // get the lower of numEvents or the number of events in the feed
	if (events.length > 0)
	{
		$loadingBox.hide();
		i = 0;
		$(xml).find("item").each(function() {
			if (i < maxEvents) {
				eDescription = $(this).find("description").text().split("&lt;br /&gt;&lt;delim/&gt;");
				eLink = $(this).find("event_link").text();
				if (!eLink) {
					eLink = $(this).find("link").text();
				}
				displayEvent($(this).find("title").text(), eLink, eDescription);
				i++;
			}
			else {
				return false;
			}
		});
	}
	else // there are no events in the calendar
	{
		printEventError("No upcoming events found, please check the calendar.", "events/noCalendar.gif");
		//sendFeedError();
	}
}
// this function takes in the event information, creates the HTML nodes for it, and shows the event
function displayEvent(title, eLink, desc)
{
	// first create the necessary HTML nodes, and the text for them
	var eventDiv 		= document.createElement( "div" );
	eventDiv.className	= "event";
	
	var dateDiv			= document.createElement( "div" );
	dateDiv.className	= "eventDate";
	
	var spanMonth		= document.createElement( "span" );
	spanMonth.className = "month";
	var month	= desc[0].split("Date:");
	/*emonth		= month[1].substring(6,9).trim();*/
	month		= month[1].substring(6,9);
	var monthText		= document.createTextNode( month );

	
	var spanDay			= document.createElement( "span" );
	spanDay.className	= "day";
	var day	= desc[0].split("Date:");
	/*day		= day[1].substring(10,12).trim();*/
	day		= day[1].substring(10,12);
	var dayText			= document.createTextNode( day );

		
	var spanTime		= document.createElement( "span" );
	spanTime.className	= "time";
	var actualTime	= desc[1].split("Time:");
	if(actualTime[1] == " 12:00 AM - 1:00 AM")
	{
		actualTime = "All Day";
	}
	else {
		actualTime	= $.trim(actualTime[1].substring(0,9));
		
		/*if (actualTime.lastIndexOf(-2) != "P" && actualTime.lastIndexOf(-2) != "A")
		{
			actualTime	= actualTime.substring(0,5);
		}
		*/
	}
	actualTime = $.trim(actualTime);
	
	var timeText		= document.createTextNode( actualTime );
	
	
	var eventTitle		= document.createElement( "h3" );
	var eventLink		= document.createElement( "a" );
	eventLink.href		= $.trim(eLink);
	eventLink.title		= $.trim(title);
	eventLink.target	= "_blank";
	var linkText		= document.createTextNode($.trim(title).substring(0, 38) + '...');
	
	var eventDesc		= document.createElement( "p" );
	if (desc[2]) {
		var descText		= document.createTextNode(desc[2].substring(0, 58) + '...');
	}
	else {
		var descText		= document.createTextNode('');
	}
	
	// now start putting everything together in order
	spanMonth.appendChild( monthText );
	spanDay.appendChild( dayText );
	spanTime.appendChild( timeText );
	dateDiv.appendChild( spanMonth );
	dateDiv.appendChild( spanDay );
	dateDiv.appendChild( spanTime );
	
	eventDesc.appendChild( descText );
	eventLink.appendChild( linkText );
	eventTitle.appendChild( eventLink );
	
	eventDiv.appendChild( dateDiv );
	eventDiv.appendChild( eventTitle );
	eventDiv.appendChild( eventDesc );
	// finally go ahead and insert the new event HTML into the actual HTML document 
	$(eventDiv).insertBefore($("#calendarLink"));
	
}




// this will remove the loading box, and then print an error message
function printEventError( errorMessage, errorImage )
{
	$loadingBox.hide();
	var errorDiv	= document.createElement( "div" );
	errorDiv.id		= "error";
	
	var img			= document.createElement( "img" );
	img.src			= "/img/icons/" + errorImage;
	img.alt			= "Calendar error image";
	
	var message		= document.createElement( "p" );
	var messageText	= document.createTextNode( errorMessage );
	message.appendChild( messageText );
	
	errorDiv.appendChild( img );
	errorDiv.appendChild( message );
	
	$(errorDiv).insertAfter($("#events h2"));
}

