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

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('fred@flohr.net');
}

/**
 * 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 = 'https://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(100);

  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());
  }
}

/**
 * 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 entries = feedRoot.feed.getEntries();
  var eventDiv = document.getElementById('events');
  if (eventDiv.childNodes.length > 0) {
    eventDiv.removeChild(eventDiv.childNodes[0]);
  }	
  var eventDiv_past = document.getElementById('past_events');
  if (eventDiv_past.childNodes.length > 0) {
    eventDiv_past.removeChild(eventDiv_past.childNodes[0]);
  }  

  /* create a new unordered list */
  var ul = document.createElement('ul');
  var ul_past = document.createElement('ul');

  if (ul.childNodes.length > 0) {
    ul.removeChild(ul.childNodes[0]);
  }  
  if (ul_past.childNodes.length > 0) {
    ul_past.removeChild(ul_past.childNodes[0]);
  }  

  var today =new Date();


  /* set the calendarTitle div with the name of the calendar */
  document.getElementById('calendarTitle').innerHTML = "";
  /* "<a href='https://www.google.com/calendar/embed?src=fred%40flohr.net&ctz=Europe/Paris' target=_empty>Google Calendar</a>"+ feedRoot.feed.title.$t;*/

  /* 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 locations = entry.getLocations();
    
    var place = "ici";
    if (locations.length > 0) {
      place = locations[0].getValueString();
    }
    if (place == "") {place = "Lieu à définir"};

    var description = entry.getContent().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 monthNum = (startJSDate.getMonth() + 1);
    var month = "Jan";
    switch (monthNum) {
    case 2:
	 month = "Feb";
         break;
    case 3:
	 month = "Mar";
         break;
    case 4:
	 month = "Apr";
         break;
    case 5:
	 month = "May";
         break;
    case 6:
	 month = "Jun";
         break;
    case 7:
	 month = "Jul";
         break;
    case 8:
	 month = "Aug";
         break;
    case 9:
	 month = "Sep";
         break;
    case 10:
	 month = "Oct";
         break;
    case 11:
	 month = "Nov";
         break;
    case 12:
	 month = "Dec";
         break;
    }

    var day = startJSDate.getDate();
    if (day <= "9") day = ("0" + day);
    var dateString =  month + "." + day + "." + (startJSDate.getYear() + 1900);
   

   /* if (!startDateTime.isDateOnly()) {
      dateString += " " + startJSDate.getHours() + ":" + 
          padNumber(startJSDate.getMinutes());
    } */

    var li = document.createElement('li');
    var it = document.createElement('i');
    it.appendChild(document.createTextNode(title));

    /* if we have a link to the event, create an 'a' element 
    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 {
    */
    if (description == "annulé" || description == "concert annulé" || description == "festival annulé") {
      li.appendChild(document.createTextNode(dateString + ' - '));
      entryLink = document.createElement('strike');
      entryLink.appendChild(document.createTextNode(place + ', '));
      entryLink.appendChild(it);
      li.appendChild(entryLink);
      var annul = document.createElement('i');
      annul.appendChild(document.createTextNode(' (' + description + ')' ));   
      li.appendChild(annul);
    } else {
      li.appendChild(document.createTextNode(dateString + ' - ' + place + ', '));
      li.appendChild(it);
    }	    

    /* append the list item onto the past or future list */
    if (startJSDate > today) {
      ul.appendChild(li);
    } else {
      ul_past.appendChild(li);
    }
  }
  eventDiv.appendChild(ul);
  eventDiv_past.appendChild(ul_past);
}

google.setOnLoadCallback(init);
/* setTimeout(google.setOnLoadCallback(init);', 500); */ // Timeout évite un crash de IE6 

//-->



