// JavaScript Document
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//iCalendar interface
//iCal display Widget Framework

var category = 0;
if(catID) category = catID;
//this is the API for the iCal feed
cal_ical_api_url = "/wwwadmin/globals/templates/9910/iCalendar.cfm";

cal_dsp_default_img_path = "/wwwadmin/globals/templates/9910/images/";

icalevents = new Array();

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//DATE SUPPORTING FUNCTIONS
//labels for the days of the week
cal_days_labels_sing = ['S', 'M', 'T', 'W', 'R', 'F', 'S'];
cal_days_labels_short = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];

//human-readable month name labels, in order
cal_months_labels = ['January', 'February', 'March', 'April',
                     'May', 'June', 'July', 'August', 'September',
                     'October', 'November', 'December'];

cal_months_labels_short = ['Jan', 'Feb', 'Mar', 'Apr',
                     'May', 'Jun', 'Jul', 'Aug', 'Sep',
                     'Oct', 'Nov', 'Dec'];

//days of the week for each month, in order
cal_days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

//current date
cal_current_date = new Date(); 

//length of month
function getMonthLength(month, year)
{
	var monthLen = cal_days_in_month[month];

	if (month == 1) { // February only
	  if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
		monthLen = 29;
	  }
	}	
	return monthLen;
}

//first day of month
function getFirstDayOfMonth(month, year)
{
	//var thismonth = (isNaN(month) || month == null) ? cal_current_date.getMonth() : month;
	//var thisyear  = (isNaN(year) || year == null) ? cal_current_date.getFullYear() : year;
	thismonth=month;
	thisyear=year;
	//get first day of week
	var firstDay = new Date(thisyear, thismonth, 1);
	var startingDay = firstDay.getDay();
	
	return startingDay;
}



/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//iCal object
function iCalObject(start, end, summary, url,  description, image, categories)
{
	try
	{
		this.start=start;
		this.end=end;
		this.summary=summary;
		this.url=url;
//		this.image=image;
		this.description=description;
		this.categories = categories;
	}
	catch(e)
	{
	
	}

}

function prevMonth()
{
	cal_current_date.setFullYear(cal_current_date.getFullYear(),cal_current_date.getMonth() - 1, cal_current_date.getDate());
	paint_calendar();
	loadevents(-1);	
	
}
function nextMonth()
{
	cal_current_date.setFullYear(cal_current_date.getFullYear(),cal_current_date.getMonth() + 1, cal_current_date.getDate());
	paint_calendar();
	loadevents(-1);
}

//generate API call for iCal object
function generateIcalRequest(callback)
{
	var month = cal_current_date.getMonth();
	var year = cal_current_date.getFullYear();
	var monthLength = getMonthLength(((month  % 12)), (year + Math.floor(month / 12)));
	if(String(month+1).length==1)
	{
		month= '0' + String(month+1)	
	}
	else
	{
		month=month+1;	
	}
	var start = year + '-' + month + '-' + '01';
	
	var end= (year + Math.floor(month / 12)) + '-' + ((month  % 12) ) + '-' + monthLength;
	
	request = cal_ical_api_url + '?start=' + start + '&end=' + end + '&catID=' + category;
	
	ajaxFunction(request, callback);
}

function parseiCal(icalText)
{
	//possible problem with extra newlines
	var icalBuff = icalText.split("\n");
	icalevents = new Array();
	
	var i = 0;
	var eventseq = 0;
	var mode = "H";
	
	for(i = 0; i < icalBuff.length; i++)
	{
		var attrib = icalBuff[i].split(":", 2);
		
		switch (attrib[0])
		{
			case 'BEGIN':
				if(attrib[1].substring(0,6)=='VEVENT')
				{
					dtstart=dtend=summary=url=description=categories='';
				}
				break;
				
			case 'DTSTART':
				dtstart=icalBuff[i].substring(attrib[0].length+1 , icalBuff[i].length);
				break;
			case 'DTEND':
				dtend=icalBuff[i].substring(attrib[0].length+1 , icalBuff[i].length);
				break;
			case 'SUMMARY':
				summary=icalBuff[i].substring(attrib[0].length+1 , icalBuff[i].length);
				break;
			case 'URL':
				url=icalBuff[i].substring(attrib[0].length+1 , icalBuff[i].length);
				break;
			case 'DESCRIPTION':
				description=icalBuff[i].substring(attrib[0].length+1 , icalBuff[i].length);
				break;
			case 'CATEGORIES':
				categories=icalBuff[i].substring(attrib[0].length+1 , icalBuff[i].length);
				break;
			case 'ATTACH':
				image=icalBuff[i].substring(attrib[0].length + 'EVENTIMAGE'.length  , icalBuff[i].length);
				break;
			case 'END':
				if(attrib[1].substring(0,6)=='VEVENT')
				{
					icalevents[eventseq] = new iCalObject(dtstart, dtend, summary, url,  description, "", categories);
					eventseq++;	
				}
				break;
			default:
		}
	}
	
}

//call the server to pull data
function ajaxFunction(queryString, callback)
{
	var xmlHttp;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Warning, your browser does not seem to support ajax...");
				return false;
			}
		}
	}
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			parseiCal(xmlHttp.responseText);
			callback();
		}
	}
	xmlHttp.open("GET",queryString,true);
	xmlHttp.send(null);
}


