/**
 * Copyright (C) 2006 by FGCZ.
 * http://www.snyke.net
 *
 * @author	Christian Decker <decker.christian@gmail.com>
 *
 * GCalendar is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Foobar; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
// ==========================================================================
window.onload = initCal;

var cal = null;
function initCal()
{
	cal = new Calendar();
	cal.onsuccess = displayEvents;
	cal.loadFeed();	

	var eventDiv = $('events');
	eventDiv.innerHTML = "<center><i>Loading Calendar Data...</i></center>";
}

function displayEvents(c)
{
	var k = -1;
	var eventDiv = $('events');
	eventDiv.innerHTML = "";

	for (k in c.entries)
	{
		addEvent(c.entries[k], k);
	}

	if (k == -1)
	{
		var div = document.createElement("div");
		div.style.margin_left = "25px";
		div.innerHTML = "<center><b>no upcoming events for now...</b></center>";
		eventDiv.appendChild(div);
	}
}

function addEvent(entry, count)
{
	try
	{
		var eventDiv = $('events');
		var div = document.createElement("div");
		//div.style.width = "600px";
		//div.style.margin_left = "auto";
		//div.style.margin_right = "auto";
		div.style.border = "1px #004000 dashed";
		if (count > 0)
		{
			div.style.borderTop = "0px";
		}
		if (count%2)
		{
			bgc = "#ffffbb";
		}
		else
		{
			//bgc = "#99ff77";
			bgc = "#ffffff";
		}
		var str = "<table border='0' width='100%' cellspacing='0'>";
		//str += "<tr bgcolor='#537342'>";
		str += "<tr bgcolor=" + bgc + ">";
		str += "<td colspan=\"2\" style=\"text-align: center;\"><b>" + entry.title + "</b></td>";
		str += "</tr>";
		str += "<tr bgcolor=" + bgc + ">";
		str += "<td width='150' align='right' valign='top'><u>Call Time:</u>&nbsp;</td>";
		str += "<td>" + formatDate(entry.startDate, true, true, entry.startDateIncludesTime) + "</td>";
		str += "</tr>";
		var startms = entry.startDate.getTime();
		var endms = entry.endDate.getTime();
		var duration = Math.floor((endms - startms)/1000);
		if (!entry.startDateIncludesTime && !entry.endDateIncludesTime)
		{
			endms = endms - (1000 * 60 * 60 * 24);
			entry.endDate.setTime(endms);
		}

		if (startms != endms)
		{
			str += "<tr bgcolor=" + bgc + ">";
			str += "<td align='right' valign='top'><u>End Time:</u>&nbsp;</td>";
			str += "<td>";
			str += formatDate(entry.endDate, true, true, entry.endDateIncludesTime);
			str += "</td>";
			str += "</tr>";
		}

		if (entry.location)
		{
			str += "<tr bgcolor=" + bgc + "><td align='right' valign='top'><u>Where:</u>&nbsp;</td><td colspan='2'>" + entry.location + "</td></tr>";
		}

		if (entry.content)
		{
			str += "<tr bgcolor=" + bgc + "><td align='right' valign='top'><u>Notes:</u>&nbsp;</td><td colspan='2'>" + entry.content.replace(/\n/g, "<br>") + "</td></tr>";
		}

		if (entry.updated)
		{
			str += "<tr bgcolor=" + bgc + "><td align='right' colspan='2'><font size=-1><i>This entry last updated: " + formatDate(entry.updated, false, true, true) + "</i></font></td></tr>";
		}

		str += "</table>";

		div.innerHTML = str;
		eventDiv.appendChild(div);
	}
	catch(e)
	{
		alert(e.description);
	}
}


function formatDate(dt, includeDay, includeDate, includeTime)
{
	var days = {0:'Sun',1:'Mon',2:'Tue',3:'Wed',4:'Thu',5:'Fri',6:'Sat',7:'Sun'}; 
	var months = {0: 'Jan',1:'Feb',2:'Mar',3:'Apr',4:'May',5:'Jun',6:'Jul',7:'Aug',8:'Sep',9: 'Oct',10:'Nov',11:'Dec'};
	var res = "";

	if (includeDay)
	{
		res += days[dt.getDay()];
	}

	if (includeDate)
	{
		if (includeDay)
		{
			res += ", ";
		}

		res += months[dt.getMonth()] + " " + dt.getDate();
	}

	if (includeTime)
	{
		if (includeDate || includeDay)
		{
			res += " - ";
		}

		res += (dt.getHours()<13?(dt.getHours()<1?"12":dt.getHours()):dt.getHours()-12) + ":" + (dt.getMinutes()<10?"0" + dt.getMinutes():dt.getMinutes()) + (dt.getHours()<12?"am":"pm");
	}

	return res;
}
