<!--
/*Código original desarrollado en los tutoriales de google api, 
se agrego las funciones TransformarHora y establecerAmPm, además
se modificó totalmente la función listEvents para adaptarla al 
contexto del portal.
*/


/* 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);
}

/**
 * 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 TransformarHora(num) {
  if (num >12 ) {
    return "" + num-12;
  }
  return num;
}

function establecerAmPm(num){
	if (num > 11 ) {
		return "p.m.";
	}
	else{
		return "a.m.";
	}  
}

/**
 * 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, maxResults) {
  var calendarUrl = 'http://www.google.com/calendar/feeds/' +
                    calendarAddress + 
                    '/public/full';
  loadCalendar(calendarUrl, maxResults);
}

/**
 * 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, maxResults) {
  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(maxResults);
  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) { 
  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 children = $j('#eventos').children();
  if (children.length > 0) {
    $j('#eventos').empty();
  }	  
  /* se realiza un ciclo por cada evento*/
  var len = entries.length;  
  for (var i = 0; i < len; i++) {	
    var entry = entries[i];
	var titulo = '<font class=tituloEvento>' + entry.getTitle().getText() + '</font>';
    var contenido = '<font class=contenidoEvento>' + entry.getContent().getText() + '</font>';
    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.getDate() + "/" + padNumber(startJSDate.getMonth() + 1);
    if (!startDateTime.isDateOnly()) {
		timeString = " " + padNumber(TransformarHora(startJSDate.getHours())) + ":" + padNumber(startJSDate.getMinutes()) + " " + establecerAmPm(startJSDate.getHours());
    } 
	else{
		timeString = "Todo el dia";
	}	
	
	fecha = '<font class="fechaEvento">' + dateString + ' - '+ timeString + '</font>';
		
	if (entryLinkHref != null) {
		enlace = '<a href="' + entryLinkHref + '">' + titulo + '</a>'
        $j('#eventos').append('<div>' + fecha + ' ' + enlace + '</div>');
    } else {
		$j('#eventos').append('<div>' + fecha + ' '+ titulo + '</div>');	
      
    }	
	$j('#eventos').append('<div>' + contenido + '</div>');
	$j('#eventos').append('<br/>');
  }
  
}

google.setOnLoadCallback(init);
//-->