/**

Based on the gCalJSON by
Copyright 2006 Mark Percival -  SquarePush, LLC
mark@squarepush.com
Released under GPL
A JSON implementation of gCalAjax.

Modified by Kevin Kresge for better formatting, easier use, and performance.
Much thanks for Mark for his excellent work on this script!

To make this work, just call this script up wherever you want it to appear.
The included CSS file is set up for the Spry accordion divs, which you probably won't have
so just rename them to whatever div you intend to use.

The original script was actually this file, but you'd actually 
call the feed in the code rather than just inluding it in here.
This way, people cannot snag your email/group address out of the code
and start spamming you.

**/

var GooCal = 'http://www.google.com/calendar/feeds/kkresge17603%40hotmail.com/public/full'; // This is the location of your calendar, just enter the URL here and it should work
var maxResults = 3; // Determines how many events will appear in the calendar window 
var is24Hour = false; // 12 or 24 Hour Times - false = 12 hour, true = 24 hour

var today = new Date();
var startDate = today.getFullYear() + '-';
startDate += (today.getMonth()+1 < 10) ? '0'+(today.getMonth()+1) : today.getMonth()+1; 
startDate += '-';
startDate += (today.getDate() < 10) ? '0'+today.getDate() : today.getDate(); 

GooCal += '?alt=json-in-script&callback=jsonhandler&singleevents=true&orderby=starttime&sortorder=a';
GooCal += '&start-min='+startDate;
GooCal += '&max-results=' + maxResults;

RSSRequest(GooCal);

function RSSRequest(url) {
document.write("<script type='text/javascript' language='javascript' src='");
document.write(GooCal);
document.write("'></script>");
}

function jsonhandler(response) {
	var feed = response.feed;
	var outputHTML = "<ul>"; // Change the formatting here - this the first markup. If you want to use <li> you can, but it will indent EVERYTHING
	var itemTimePrev = new Date();
	itemTimePrev.setTime(0);
	
	if(feed.entry) { 
		for (var i = 0 ; i < feed.entry.length; i++) {
	      var entry = feed.entry[i];
	      var itemTitle = entry['title'].$t;
		  var itemLink = entry['link'][0].href;
		  var itemTimeRaw = entry['gd$when'][0].startTime;
		  var itemLocation = entry['gd$where'][0].valueString;
		  var itemDescr = entry['content'].$t;
		  var isAllDay = false;
	      if (itemTimeRaw.length <= 10) isAllDay = true;
	      var itemTime = new Date();
	      itemTime.setTime (Date.UTC(itemTimeRaw.substr(0,4),(itemTimeRaw.substr(5,2)-1),itemTimeRaw.substr(8,2),itemTimeRaw.substr(11,2),itemTimeRaw.substr(14,2)));
	      if ((itemTime.getUTCDate()!=itemTimePrev.getUTCDate())||(itemTime.getUTCMonth()!=itemTimePrev.getUTCMonth()))
	      outputHTML += '<h3>' + getMonthName(itemTime)+ ' ' + itemTime.getUTCDate() + '</h3>'; // Closing formatting tags here for the month names only
	                    
	      outputHTML += '<h1>'; // Formatting markups for each event name
	      if (!isAllDay) outputHTML += getTimeFormatted(itemTime) + ' - ';     
		  outputHTML += ' <a href="' + itemLink + '">' + itemTitle + '</a></h1>'; // Closing markups for event names - it's possible to color the event links differently using the a:link CSS definitions
	      itemTimePrev.setTime(itemTime);	  
	    }
	outputHTML += "</ul>";
	setHTML("gcalajax", outputHTML);
	setHTML("status", "");
	}
	else {setHTML("status", "No events scheduled.");}
}

// This section formats the month names, so you can abbreviate them if you want
function getMonthName(dateObject) {
    var m_names = new Array("January", "February", "March", 
    "April", "May", "June", "July", "August", "September", 
    "October", "November", "December");
    return(m_names[dateObject.getUTCMonth()]);
}

// This section formats the time, otherwise you get a 10 digit time code.
function getTimeFormatted(dateObject) {
    var hours = dateObject.getUTCHours();
    var minutes = dateObject.getUTCMinutes();
    var formattedTime = null;
    if (is24Hour) {
        if (minutes < 10){minutes = "0" + minutes;}
        formattedTime = hours + ':' + minutes;
        return (formattedTime);
    }
    else {
        var ampm = "AM";
        if (hours > 12){
            hours = hours - 12;
            ampm = "PM";}
        if (hours == 12){ampm = 'PM';}
        if (hours == 0) {hours = 12;}
        if (minutes < 10){minutes = "0" + minutes;}
        formattedTime = hours + ':' + minutes + ' ' + ampm;
		return (formattedTime);
    }
}

function setHTML(div, data)
{
	document.write(data);
}   
{// Make sure you change this link to forward to your site's calendar, if you don't you will potentially have people getting 404's
	document.write('<noscript>You must have javascript enabled to view the event calendar. If you want to view the whole calendar, click <a href="../calendar.html">here.</a></noscript>');
}