/* 
### evArray is a Global array of Event objects referenced in bulletin.js and calendar.js
### this script defines the event object and methods related to the declard evArray.
*/

var evArray = new Array();		  
var i_ev=0;


/* ---------------------------------------------------------------------------------------------------
See the evUpdate.js and evHoliday.js files for examples of how to instantiate Event.    
 
Special Note: if two events fall on the same day, the color will be selected from the first event. This is why
the recurring feast days are up front on this particular calendar. Further down, the style is null, defaulting
to past, present or future date color backgrounds.
------------------------------------------------------------------------------------------------------ */




/* ######---   E v e n t   O b j e c t */

function Event(month,day,year,repeat,sdesc,ldesc,cstyle,bentry) 
{ 
//***  Event properties     ***
	this.month  = month - 1 || "";	// event month. We subtract 1 to be in synch with how javascript months are stored 
	this.day    = day    || "";	// event day	
	this.year   = year   || "";	// event year
	this.repeat = repeat || "";	//"(every)/1st/2nd/3rd/4th Weekday" e.g. "1st Monday" or "Monday" 
	this.sdesc  =  sdesc || "";	//short description of event for title, calendar	  
	this.ldesc  =  ldesc || "";	//long description of title for bulletin, pop-up
	this.cstyle = cstyle || "";	//optional style class to be used in style sheet <td class=xxxxxx>
	this.bentry = (bentry!=""&&bentry!=null)?bentry:0;	//non-zero value places it in bulletin. higher values display 1st.

}


function addEvent(month,day,year,repeat,sdesc,ldesc,cstyle,bentry)
{
	evArray[i_ev]= new Event(month,day,year,repeat,sdesc,ldesc,cstyle,bentry);
	if (evArray[i_ev].day != "" && 
	(evArray[i_ev].repeat.substr(0,1) == "d" || evArray[i_ev].repeat.substr(0,1) == "w" || evArray[i_ev].repeat.substr(0,1) == "m"))
	expandevEntry();
	i_ev++;
}


//######################################################################################################################
//#                      Common Functions used in Calendar / Events / Bulletin 
//######################################################################################################################



/* ******************* this is not an Event method ************************************** 
    This function which looks for an Event object in the Evarray... it returns an array of events (eventarray) which match:
	1. the exact date in evdate
	2. the xth weekday e.g. 1st Tuesday
	3. every weekday   e.g. all Tuesdays
	4. the date generated from evdate with a repetition of d9 = next 9 days, w9 = next 9 weeks, or m9 = next 9 months.  
*********************************************************************** */

function getDayEvents(month,day,year,repeat)
{

	var t_returnArray = new Array();
	var j=0;

	for (i=0; i<evArray.length; i++) 
	{
		if (evArray[i].month == month && evArray[i].day == day && (evArray[i].year == "" || evArray[i].year == year))
		{
			t_returnArray[j] = new Event;
			t_returnArray[j++] =  evArray[i];
		} 
		else 	
		{
			if (evArray[i].repeat == repeat)		// dates not matched, if repeat matched exactly return it 
			{
				t_returnArray[j] = new Event;
				t_returnArray[j++] =  evArray[i];
			}
			else
			{
				if (evArray[i].repeat.substr(0,5) == repeat.substr(4,5))	// not exact repeat, is it every?
				{
					t_returnArray[j] = new Event;
					t_returnArray[j++] =  evArray[i];
				}
			}
		};
	};
	return t_returnArray;	

}  


/* ************** This is NOT an Event Method ****************************** 
    This function expands evArray when the repeat attribute is set to: (e.g.)
	d9  = next 9 days, w3  = next 3 weeks, 	m12 = next 12 months.  
**************************************************************************** */
function expandevEntry()
{
	var rptNum = 0;
	var rptStrng = evArray[i_ev].repeat;
	var expDate = new Date();
	expDate.setDate(evArray[i_ev].day);
	expDate.setMonth(evArray[i_ev].month);
	if (evArray[i_ev].year != "")
		expDate.setFullYear(evArray[i_ev].year);

	rno = eval(evArray[i_ev].repeat.substr(1,evArray[i_ev].repeat.length-1))
	rno--; 
	for (j=0; j<rno; j++)
	{
		if (rptStrng.substr(0,1) == "d")
			expDate.setDate(expDate.getDate()+1);
		if (rptStrng.substr(0,1) == "w")
			expDate.setDate(expDate.getDate()+7);

		if (rptStrng.substr(0,1) == "m")
			expDate.setMonth(expDate.getMonth()+1);

		i_ev++;
		evArray[i_ev]= new Event(expDate.getMonth()+1,  //javascript Date uses 0-11 monthno.
			   expDate.getDate(),
			   expDate.getFullYear(),
			   null, 
			   evArray[i_ev-1].sdesc,
			   evArray[i_ev-1].ldesc,
			   evArray[i_ev-1].cstyle);
	} 
} 
