// JavaScript Document
/*
Holds all our events from differing calendars in one Array and 
can be sorted too.
*/
function EventCollection() 
{
	this.evtcollection = [];
	this.dateRE = "/^(\d{2})[\/\- ](\d{2})[\/\- ](\d{4})/"; //regex used for date string 
	
	this.add = function(Event) 
	{
		//we dont check for duplicates, dunno why we should do anyway
		this.evtcollection.push(Event);					
	}
		
	this.getCount = function() 
	{
		return this.evtcollection.length;
	}
	
	this.getEvent = function( iEvent ) 
	{
		return this.evtcollection[iEvent];
	}
	
	this.sortEvents = function()
	{
		var eventsArrayArray = new Array();
		
		if(this.evtcollection.length > 0)
		{
			//first sort by calendar name
			this.evtcollection = this.evtcollection.sort(this.sortbyCalendarName);
			
			var calendarname = null;
			var calendarArray = null;
			
			//break it up into separate calendars
			
			for(var i =0 ; i < this.getCount(); i++)
			{
				if(calendarname == null)
				{
					calendarname = this.evtcollection[i].calendarname;
					calendarArray = new Array();
				}
				
				if(calendarname == this.evtcollection[i].calendarname)
				{
					calendarArray.push(this.evtcollection[i]);
					
					if(this.getCount()-1 == i)
					{
						eventsArrayArray.push(calendarArray);
					}
					continue;					
				}
				else
				{
					eventsArrayArray.push(calendarArray);
					calendarname = null;
					i--; //go back one 
				}				
			}	
			
			this.evtcollection = new Array();
			
			//now sort each calendar array by time
			for(var i =0 ; i < eventsArrayArray.length; i++)
			{
				eventsArrayArray[i] = eventsArrayArray[i].sort(this.sortbyAscendingDate_ddmmyyyy);
				this.evtcollection = this.evtcollection.concat(eventsArrayArray[i]);								
			}			
		}
	}
	
	/*Comparator function that will compare two events, required for sorting functionality
	Here we sort dates in ascending chronological order with format exactly dd/mm/yyyy. 
	*/
	
	this.sortbyAscendingDate_ddmmyyyy = function( event1, event2)
	{
		var date1 = event1.startDateTime.getDate();
		var date2 = event2.startDateTime.getDate();
		
		if(date1 > date2)
			return 1;
		if(date1 < date2)
			return -1;
			
		return 0;	
	}
	
	this.sortbyCalendarName = function( event1, event2)
	{
		var calendarname1 = event1.calendarname;
		var calendarname2 = event2.calendarname;
		
		if(calendarname1 > calendarname2)
			return 1;
		if(calendarname1 < calendarname2)
			return -1;
			
		return 0;	
	}
}

/*Object holds a single event data*/
function EventObject( calendarname, startdateTime, enddateTime, Event) 
{
	this.startDateTime = startdateTime;
	this.EndDateTime = enddateTime;
	this.EventData = Event;
	this.calendarname = calendarname;
}

/***************************
Global function to trigger when we process all events 
*/
function GlobalCalendarJobCompletionObject( CalendarsRequested, numofMonths ) 
{
	this.numofMonths = numofMonths;
	this.calendarsRequested  = CalendarsRequested;
	this.numofJobs   = CalendarsRequested.length;
	
	this.start = function()
	{
		if(this.calendarsRequested.length > 0)
		{
			var x = 0;
			
			//for each requested calendar lets load it 
			for (x in this.calendarsRequested)
		  		loadCalendar(this.calendarsRequested[x], this.numofMonths);
		}
	}
	
	/* when this fucntion is called then one more calendar has been completely
	retrieved so we decrement the job count until we reach zero, when we reach zero then we
	call the normal listevents() functionality that will generate the HTML and DIV's for the
	DOM that will display all the events.
	*/
	
	this.jobCompletedCallBack = function()
	{
		this.numofJobs--;
		
		if(this.numofJobs <= 0)
		{
			//Update screen with data
			if(this.calendarsRequested != null)
			{	
				//now kick off updating the screen with the events
				//listEvents();
				listDailyEvents(); 
			}
		}			
	}	
}

