<!--
/* Loads the Google data JavaScript client library */

//-->

google.load("gdata", "2.x");

function init() {
  // init the Google data JS client library with an error handler
  google.gdata.client.init(handleGDError);
  // load the code.google.com developer calendar
  loadDeveloperCalendar();
}


/**
 * Loads the Google Developers Event Calendar
 */
function loadDeveloperCalendar() {
   //loadCalendarByAddress('developer-calendar@google.com');
   loadCalendarByAddress('3umapbtvlq6h77bjoognbqgpck@group.calendar.google.com');

}

/**
 * Adds a leading zero to a single-digit number.  Used for displaying dates.
 */
function padNumber(num) {
  if (num <= 9) {
    return "0" + num;
  }
  return num;
}

function getClockTime()
{
   var now    = new Date();
   var hour   = now.getHours();
   var minute = now.getMinutes();
   var second = now.getSeconds();
   var ap = "AM";
   if (hour   > 11) { ap = "PM";             }
   if (hour   > 12) { hour = hour - 12;      }
   if (hour   == 0) { hour = 12;             }
   if (hour   < 10) { hour   = "0" + hour;   }
   if (minute < 10) { minute = "0" + minute; }
   if (second < 10) { second = "0" + second; }
   var timeString = hour +
                    ':' +
                    minute +
                    ':' +
                    second +
                    " " +
                    ap;
   return timeString;
} // function getClockTime()

function formatHours(hour)
{
   var _hour = hour; 
   if (_hour   > 12) { _hour = _hour - 12;      }

   return _hour;
} 

function getAMpm(hour)
{
   var ap = "AM";
   if (hour   > 11) { ap = "PM"; }
   return ap;
} 

/**
 * Determines the full calendarUrl based upon the calendarAddress
 * argument and calls loadCalendar with the calendarUrl value.
 *
 * @param {string} calendarAddress is the email-style address for the calendar
 */ 
function loadCalendarByAddress(calendarAddress) {
  var calendarUrl = 'http://www.google.com/calendar/feeds/' +
                    calendarAddress + 
                    '/public/full';
  loadCalendar(calendarUrl);
}

/**
 * Uses Google data JS client library to retrieve a calendar feed from the specified
 * URL.  The feed is controlled by several query parameters and a callback 
 * function is called to process the feed results.
 *
 * @param {string} calendarUrl is the URL for a public calendar feed
 */  
function loadCalendar(calendarUrl) {
  var service = new 
      google.gdata.calendar.CalendarService('gdata-js-client-samples-simple');
  var query = new google.gdata.calendar.CalendarEventQuery(calendarUrl);
  query.setOrderBy('starttime');
  query.setSortOrder('ascending');
  query.setFutureEvents(true);
  query.setSingleEvents(true);
  query.setMaxResults(1); // changed to 1

  service.getEventsFeed(query, listEvents, handleGDError);
}

/**
 * Callback function for the Google data JS client library to call when an error
 * occurs during the retrieval of the feed.  Details available depend partly
 * on the web browser, but this shows a few basic examples. In the case of
 * a privileged environment using ClientLogin authentication, there may also
 * be an e.type attribute in some cases.
 *
 * @param {Error} e is an instance of an Error 
 */
function handleGDError(e) {
  document.getElementById('jsSourceFinal').setAttribute('style', 
      'display:none');
  if (e instanceof Error) {
    /* alert with the error line number, file and message */
    alert('Error at line ' + e.lineNumber +
          ' in ' + e.fileName + '\n' +
          'Message: ' + e.message);
    /* if available, output HTTP error code and status text */
    if (e.cause) {
      var status = e.cause.status;
      var statusText = e.cause.statusText;
      alert('Root cause: HTTP error ' + status + ' with status text of: ' + 
            statusText);
    }
  } else {
    alert(e.toString());
  }
}
/**
Date Formatter function
*/
function formatDate(mydate) {

var d_names = new Array("Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday");

var m_names = new Array("January", "February", "March", 
"April", "May", "June", "July", "August", "September", 
"October", "November", "December");

var d = mydate;
var sup = "";
if (curr_date == 1 || curr_date == 21 || curr_date ==31)
   {
   sup = "st";
   }
else if (curr_date == 2 || curr_date == 22)
   {
   sup = "nd";
   }
else if (curr_date == 3 || curr_date == 23)
   {
   sup = "rd";
   }
else
   {
   sup = "th";
   }
return (d_names[d.getDay()] + " " + m_names[d.getMonth()] +" "+d.getDate()+"<sup>"+ sup + "</sup>" +", " + d.getFullYear());

}

/**
 * Callback function for the Google data JS client library to call with a feed 
 * of events retrieved.
 *
 * Creates an unordered list of events in a human-readable form.  This list of
 * events is added into a div called 'events'.  The title for the calendar is
 * placed in a div called 'calendarTitle'
 *
 * @param {json} feedRoot is the root of the feed, containing all entries 
 */ 
function listEvents(feedRoot) {
 
  var d_names = new Array("Sunday", "Monday", "Tuesday",
  "Wednesday", "Thursday", "Friday", "Saturday");

  var m_names = new Array("January", "February", "March", 
  "April", "May", "June", "July", "August", "September", 
  "October", "November", "December");

  var entries = feedRoot.feed.getEntries();
  var eventDiv = document.getElementById('events');
  if (eventDiv.childNodes.length > 0) {
    eventDiv.removeChild(eventDiv.childNodes[0]);
  }   
  /* create a new unordered list */
 /* var ul = document.createElement('ul'); */

  /* set the calendarTitle div with the name of the calendar 
  document.getElementById('calendarTitle').innerHTML = 
    "Calendar: " + feedRoot.feed.title.$t;

  commented by shinoi*/

  /* loop through each event in the feed */
  var len = entries.length;
  for (var i = 0; i < len; i++) {
    var entry = entries[i];
    var title = entry.getTitle().getText();
    var startDateTime = null;
    var startJSDate = null;
    var times = entry.getTimes();
    if (times.length > 0) {
      startDateTime = times[0].getStartTime();
      startJSDate = startDateTime.getDate();
    }
    var entryLinkHref = null;
    if (entry.getHtmlLink() != null) {
      entryLinkHref = entry.getHtmlLink().getHref();
    }
    //var dateString = (startJSDate.getMonth() + 1) + "/" + startJSDate.getDate() + "/" + startJSDate.getYear();
    var sup = "";
    var eventdate = startJSDate.getDate(); 
    
   if (eventdate == 1 || eventdate == 21 || eventdate == 31)
    {
     sup = "st";
    }
  else if (eventdate == 2 || eventdate == 22)
    {
    sup = "nd";
    }
  else if (eventdate == 3 || eventdate == 23)
    {
    sup = "rd";
    }
  else
    {
    sup = "th";
    }
    //var dateString = d_names[startJSDate.getDay()] +" "+  m_names[startJSDate.getMonth()]  + " " + startJSDate.getDate() + "<sup>"+sup+"</sup>" + ", " +  startJSDate.getYear();
    var dateString = d_names[startJSDate.getDay()] +", "+  m_names[startJSDate.getMonth()]  + " " + startJSDate.getDate() + "<sup>"+sup+"</sup>";

    if (!startDateTime.isDateOnly()) {
      dateString += " at " + formatHours(startJSDate.getHours()) + ":" + 
          padNumber(startJSDate.getMinutes()) + " " + getAMpm(startJSDate.getHours() );
    } 

    document.getElementById('events').innerHTML = title;

    /* var li = document.createElement('li'); */

    /* if we have a link to the event, create an 'a' element */
    /*  changed by Shinoi */
    
    entryLinkHref = null;

    if (entryLinkHref != null) {
      entryLink = document.createElement('a');
      entryLink.setAttribute('href', entryLinkHref);
      entryLink.appendChild(document.createTextNode(title));
      li.appendChild(entryLink);
      li.appendChild(document.createTextNode(' - ' + dateString));
    } else {
      //li.appendChild(document.createTextNode(title + ' - ' + dateString));
    }     

    /* append the list item onto the unordered list */
    //ul.appendChild(li);
  }
  //eventDiv.appendChild(ul);

  document.getElementById('eventdate').innerHTML = dateString;


}

google.setOnLoadCallback(init);



