

// OLIVIER


/* manips sur les dates */

var month_names = new Array ( );
month_names[month_names.length] = "January";
month_names[month_names.length] = "February";
month_names[month_names.length] = "March";
month_names[month_names.length] = "April";
month_names[month_names.length] = "May";
month_names[month_names.length] = "June";
month_names[month_names.length] = "July";
month_names[month_names.length] = "August";
month_names[month_names.length] = "September";
month_names[month_names.length] = "October";
month_names[month_names.length] = "November";
month_names[month_names.length] = "December";

var day_names = new Array ( );
day_names[day_names.length] = "Sunday";
day_names[day_names.length] = "Monday";
day_names[day_names.length] = "Tuesday";
day_names[day_names.length] = "Wednesday";
day_names[day_names.length] = "Thursday";
day_names[day_names.length] = "Friday";
day_names[day_names.length] = "Saturday";

/* Exemple: document.write ( day_names[current_date.getDay()] );
           document.write ( ", " );
           document.write ( month_names[current_date.getMonth()] );
           document.write ( " " + current_date.getDate() );
           document.write ( " " );
         document.write ( " " + current_date.getFullYear() ); */

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

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

/**
 * Loads the Google Developers Event Calendar
 */
function loadMyCalendar() {
  loadCalendarByAddress(emailcalendrier,ONLYFUTURE);
}

/**
 * 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,onlyfuture) {
  var calendarUrl = 'http://www.google.com/calendar/feeds/' +
                    calendarAddress + 
                    '/public/full';
  loadCalendar(calendarUrl,onlyfuture);
}

/**
 * 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,onlyfuture) {
  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(onlyfuture);
  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) {

  // Olivier
  var event_display="<li><a href='###LINK###'> @ </a> ###DATE### ###FROM### ###END### <b> <a href='###DESCRIPTION###'> ###TITLE### </a></b> </li>";


  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;
  /* 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 endDateTime = null;
    var endJSDate = null;
    var comment = null;
    var quellien = null;
    var times = entry.getTimes();
    if (times.length > 0) {
      startDateTime = times[0].getStartTime();
      startJSDate = startDateTime.getDate();
      endDateTime = times[0].getEndTime();
      endJSDate = endDateTime.getDate();
    }
    var entryLinkHref = null;
    if (entry.getHtmlLink() != null) {
      entryLinkHref = entry.getHtmlLink().getHref();
    }

    // Cusisine sur les dates
    var dateString = day_names[startJSDate.getDay()]+" "+ startJSDate.getDate() + " " + month_names[startJSDate.getMonth()] + " " + startJSDate.getFullYear();
    if (!startDateTime.isDateOnly()) {
      dateString += " " + startJSDate.getHours() + ":" + 
          padNumber(startJSDate.getMinutes());
    }
  // Hack sur date de fin
    if (endDateTime.isDateOnly())
      endJSDate.setDate(endJSDate.getDate()-1);
    dateString += " - " + day_names[endJSDate.getDay()]+" "+ endJSDate.getDate() + " " + month_names[endJSDate.getMonth()] + " " + endJSDate.getFullYear();
    if (!endDateTime.isDateOnly()) {
      dateString += " " + endJSDate.getHours() + ":" + 
          padNumber(endJSDate.getMinutes());
    }

   // Reste

   comment = entry.getContent().getText();
   if (comment != "") {
      quellien = comment;
  } else {quellien = entryLinkHref}

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

    // FORMAT
    // $event_display="<li><a href='###LINK###'> @ </a> ###DATE### ###FROM### ###END### <b> <a href='###DESCRIPTION###'> ###TITLE### </a></b> </li>";
    // TRADUIT ICI EN JAVASCRIPT


    /* 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('@'));
      li.appendChild(entryLink);
      li.appendChild(document.createTextNode(" "+dateString+" "));
     entryLink = document.createElement('b');
     if (quellien != null) {
        childentryLink=document.createElement('a');
        childentryLink.setAttribute('href',quellien);
        childentryLink.appendChild(document.createTextNode(title));
     entryLink.appendChild(childentryLink);
    } else entryLink.appendChild(document.createTextNode(title));
      
     // entryLink.appendChild(document.createTextNode(title));
      li.appendChild(entryLink);
    } else {
      li.appendChild(document.createTextNode(title + ' - ' + dateString));
    }	    

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

 /*  document.write(event_display);
  

    // if we have a link to the event, create an 'a' element 
    if (entryLinkHref != null) {
      document.write("<a href='"+entryLinkHref+"'>"+title+"</a>");
      document.write(' - ' + dateString);
    } else {
      document.write(title + ' - ' + dateString);
    }
*/
   
  }
 eventDiv.appendChild(ul);
}

//google.setOnLoadCallback(init);

