/**

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/691asa5gsco0eda8rp40gajh88%40group.calendar.google.com/public/full'; // This is the location of your calendar, just enter the URL here and it should work
var maxDays = 7; // Determines how many days will appear in the calendar window
var maxResults = 5; // 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 = ''// 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 += '<h2>' + getMonthName(itemTime)+ ' ' + itemTime.getUTCDate() + '</h2>'; // Closing formatting tags here for the month names only
                   
      outputHTML += '<p>'; // Formatting markups for each event name
      if (!isAllDay) outputHTML += getTimeFormatted(itemTime) + ' - ';    
  outputHTML += '<b>' + itemTitle + '</b><br />' + itemLocation + '</p>'; // Closing markups for event names - it's possible to color the event links differently using the a:link CSS definitions
      itemTimePrev.setTime(itemTime);  
    }
outputHTML += "";
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="../news/calendar.html">here.</a></noscript>');
}
