/**

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.
Modified by Arlin Sandbulte to include time zone corrections & start-max option.
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.

**/

//var GooCal = 'http://www.google.com/calendar/feeds/duu2oe6gdu3bvt9ampoq0qrhpo@group.calendar.google.com/public/full'; // This is the location of your calendar, just enter the URL here and it should work
var GooCal = 'http://www.google.com/calendar/feeds/g5nl5vbtco73a284fu40b86bn8%40group.calendar.google.com/public/full'; // This is the location of your calendar, just enter the URL here and it should work

var maxDays = 90; // Determines how many days will appear in the calendar window
//var maxResults = 0; // Determines how many events will appear in the calendar window. Set to zero to show all events within range set by maxDays 
var maxResults = 3; // Determines how many events will appear in the calendar window. Set to zero to show all events within range set by maxDays 
var is24Hour = false; // 12 or 24 Hour Times - false = 12 hour, true = 24 hour

// get today's date and format it for google calendar query &start-min
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();
startDate += 'T00:00:00';
startDate += (today.getTimezoneOffset() < 0 ? '+': '-') + ((Math.abs(today.getTimezoneOffset())/60 < 10) ? '0' + Math.abs(today.getTimezoneOffset()) / 60 : Math.abs(today.getTimezoneOffset())/60) + ':00'

// get desired last day and format it for google calendar query &start-max
var dateOffset = new Date(86400000*maxDays);
var nextDate = new Date(today*1 + dateOffset*1);
var endDate = nextDate.getFullYear() + '-';
endDate += (nextDate.getMonth()+1 < 10) ? '0'+(nextDate.getMonth()+1) : nextDate.getMonth()+1; 
endDate += '-';
endDate += (nextDate.getDate() < 10) ? '0'+(nextDate.getDate()) : nextDate.getDate(); 
endDate += 'T00:00:00';
endDate += (today.getTimezoneOffset() < 0 ? '+': '-') + ((Math.abs(today.getTimezoneOffset())/60 < 10) ? '0' + Math.abs(today.getTimezoneOffset()) / 60 : Math.abs(today.getTimezoneOffset())/60) + ':00'

GooCal += '?alt=json-in-script&callback=jsonhandler&singleevents=true&orderby=starttime&sortorder=a';
GooCal += '&start-min=' + startDate;
GooCal += '&start-max=' + endDate;
GooCal += (maxResults == 0) ? '' : '&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 = '<h3><a title="Goto ARC Calendar" href="events.php">Upcomming Events</a></h3>'// Change the formatting here - this the first markup. If you want to use <li> you can, but it will indent EVERYTHING
	var outputHTML = '<table cellpadding="0" cellspacing="0" border="0">'
	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(!itemDescr) itemDescr = " ";
		  

	      outputHTML += '<tr>'; // Formatting markups for each event name
			  outputHTML += '<td valign="top" width="58"><IMG SRC="/images/nw2.gif" WIDTH=58 HEIGHT=15 ALT=""/></td>';
			  outputHTML += '<td><a href="' + itemLink + '" class="c" target="_blank">' + itemTitle + '</a><br>' + itemDescr + '</td>'; // Closing markups for event names - it's possible to color the event links differently using the a:link CSS definitions
              outputHTML += '<td><IMG SRC="/images/spacer.gif" WIDTH=1 HEIGHT=2 ALT=""></td>';
//	      if ((itemTime.getUTCDate()!=itemTimePrev.getUTCDate())||(itemTime.getUTCMonth()!=itemTimePrev.getUTCMonth()))
//	      outputHTML += '<tr><td>' + getDayName(itemTime) +', ' + getMonthName(itemTime)+ ' ' + itemTime.getUTCDate() + '</td><tr>'; // Closing formatting tags here for the month names only
//		  if (!isAllDay) outputHTML += '<tr><td>' + getTimeFormatted(itemTime) + '</td></tr>'; 
//		  if (isAllDay) outputHTML += '<tr><td>' + 'All Day Event</td></tr>'; 
	      itemTimePrev.setTime(itemTime);	  

		  outputHTML += "</tr>";	//close item row
		  outputHTML += "<tr><td><IMG SRC='/images/spacer.gif' WIDTH=1 HEIGHT=8 ALT=''></td></tr>"; //Anter an empty row between items
	    }
	outputHTML += "</table>";
	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("Jan.", "Feb.", "Mar.", 
    "Apr.", "May", "Jun.", "Jul.", "Aug.", "Sep.", 
    "Oct.", "Nov.", "Dec.");
    return(m_names[dateObject.getUTCMonth()]);
}

// This section formats the Day names, so you can abbreviate them if you want
function getDayName(dateObject) {
    var d_names = new Array("Sunday", "Monday", "Tuesday", "Wednesday", 
    "Thursday", "Friday", "Saturday");
    return(d_names[dateObject.getUTCDay()]);
}

// 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="events.php">here.</a></noscript>');
}