<!--
/* Loads the Google data JavaScript client library */
google.load("gdata", "1");

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

//JP 20090915  I commented this call out because it was recalling loadCalendar() 
//             which then overrides the first call thats in the HTML body tag.
//  loadDeveloperCalendar();
}

/**
 * Loads the Google Developers Event Calendar
 */
function loadDeveloperCalendar() {
//loadCalendarByAddress('developer-calendar@google.com');
//  loadCalendarByAddress('jhnpttmn@gmail.com');
  loadCalendarByAddress('milwaukeedog@gmail.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;
}

/**
 * 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';
//alert (" CalenarUrl " + calendarUrl);
//  loadCalendar(calendarUrl, "Puppy Class Start");
}

/**
 * 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, q) {
  var d=new Date();
  var today=(d.getMonth()+1) +"-"+ d.getDate() +"-"+ d.getFullYear();
//alert("today is " + today);

  var service = new 
      google.gdata.calendar.CalendarService('gdata-js-client-samples-simple');

  var query = new google.gdata.calendar.CalendarEventQuery(calendarUrl);

//alert("today is " + calendarUrl);

  query.setOrderBy('starttime');
  query.setSortOrder('ascending');
// JP 12-21-2009 changed to true from false
  query.setFutureEvents(true);
  query.setSingleEvents(true);
  query.setMaxResults(1);
//  query.setMinimumStartTime("2009-11-11");
  query.setMinimumStartTime(today);
//  query.setFullTextQuery(q);
  query.setFullTextQuery("Puppy Class Start");

//alert("loadCalendar");
  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) {
//alert("handleGDError");
  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());
  }
}


function formatTime(d){
  
  var hours = d.getHours()
  var minutes = d.getMinutes()

  var suffix = "AM";
  if (hours >= 12) {
  suffix = "PM";
  hours = hours - 12;
  }
  if (hours == 0) {
  hours = 12;
  }

  if (minutes < 10){
    minutes = "0" + minutes;
  }

//alert( hours + ":" + minutes + " " + suffix);

  return hours + ":" + minutes + " " + suffix;

}


function getDayofWeek(d){

var weekday=new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";

var wd = weekday[d.getDay()]

//alert("week day is " + wd);

return wd;

}


function formatMonth(d){

var months=new Array(12);
months[0]="January";
months[1]="February";
months[2]="March";
months[3]="April";
months[4]="May";
months[5]="June";
months[6]="July";
months[7]="August";
months[8]="September";
months[9]="October";
months[10]="November";
months[11]="December";

var m = months[d.getMonth()];

//alert("the month is " + m);

return m;

}


/**
 * 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) {
//alert("listEvents");
  var entries = feedRoot.feed.getEntries();
//  var eventDiv = document.getElementById('events');

//  if (eventDiv.childNodes.length > 0) {
//    eventDiv.removeChild(eventDiv.childNodes[0]);
//  }	  

// JP 11-11-2009  added temporarily
// JP 12-21-2009  commented out
//document.getElementById('puppyclassstart').innerHTML = "as yet to be determined";

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

  /* loop through each event in the feed */

  var len = entries.length;
//alert("entries.length" + entries.length);
  if (len>0){
  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 dateString = getDayofWeek(startJSDate) + ", " + formatMonth(startJSDate) + " " + startJSDate.getDate()+ ", " + startJSDate.getFullYear();

//  This adds the time to the date
//    if (!startDateTime.isDateOnly()) {
//      dateString += " " + formatTime(startJSDate);
//   }
    if (title == "Puppy Class Start"){
       document.getElementById('puppyclassstart').innerHTML = dateString;
    }
	
  }   // for loop
  }  // if len>0
  else{
       document.getElementById('puppyclassstart').innerHTML = "a date to be determined";	
  }
}  // function listEvents() end

google.setOnLoadCallback(init);

//-->
