/**
 * 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();	
}

function displayEvents(c){
	for(k in c.entries){
		addEvent(c.entries[k]);
	}
	$('events').style.display = "block";
	$('status').style.display = "none";
}

function addEvent(entry){
	try{
		var eventDiv = $('events');
				
		var div = document.createElement("div");
		div.style.padding = "2px";
		var str = "<div class=\"event\"><h2><strong>" + formatDate(entry.startDate) + "</strong> - " + entry.title + "</h2>";
		if(entry.content)
			str += "<p><strong>Details:</strong> " + entry.content + "</p>";
		if(entry.location)
			str += "<p><strong>Location:</strong> " + entry.location.replace(/,/g) + "</p></div>";
		div.innerHTML = str;
		eventDiv.appendChild(div);
	}catch(e){
		alert(e.description);
	}
}



function formatDate(dt){
	var months = {0: 'January',1:'February',2:'March',3:'April',4:'May',5:'June',6:'July',7:'August',8:'September',9: 'October',10:'November',11:'December'};
	var res = dt.getDate() + " " + months[dt.getMonth()] + " " + dt.getFullYear();
	res += " " + (dt.getHours()%12+1) + ":" + (dt.getMinutes()<10?"0" + dt.getMinutes():dt.getMinutes()) + (dt.getHours() >= 12?" PM":" AM");
	return res;
}