/* Contains Overrides for smokejumper population of date items. */
/**
 * Override callback function for the Google data JS client library to call with a feed 
 * of events retrieved.
 *
 * Creates a list of events in a human-readable form.  This list of
 * events is added into a div called 'wpng-cal-events'.  
 *
 * @param {json} feedRoot is the root of the feed, containing all entries 
 */ 
function sj_listEvents(feedRoot) {
  var entries = feedRoot.feed.getEntries();
  var eventDiv = document.getElementById('wpng-cal-events');
  /* clear out anything in the current events DIV */
  while (eventDiv.firstChild) {
	  eventDiv.removeChild(eventDiv.firstChild);
  }
  /* loop through the events in the feed and output to the DIV */
  var prevDateString = null;
  var len = entries.length;
  /* the list is displayed in a table, let's create it */
  var table = document.createElement('table');
  var tableBody = document.createElement('tbody');
  table.setAttribute('className','wpng-page-list-table');
  table.setAttribute('class','wpng-page-list-table');
  for (var i = 0; i < len; i++) {
	  var entry = entries[i];
	  var times = entry.getTimes();
	  var startTime = times[0].getStartTime();
	  /* note: using cool functions from DateJS for formatting */
	  if (startTime.isDateOnly()) {
		  continue;
	  }

	  var displayTime = startTime.getDate().clone();
	  var dateString = null;
	  if (displayTime.clearTime().equals(Date.today())) {
		  dateString = 'Today';
	  }
	  else if (displayTime.clearTime().equals(Date.today().add(1).days())) {
		  dateString = 'Tomorrow';
	  }
	  else {
		  dateString = displayTime.toString('dddd, MMMM d, yyyy');
	  }
	  /* if the date has changed then output a new header row in the table */
	  if (dateString != prevDateString) {
		  var trHead = document.createElement('tr');
		  var tdHead = document.createElement('td');
		  tdHead.setAttribute('class','wpng-page-list-head');
		  tdHead.setAttribute('colSpan','3');
		  tdHead.setAttribute('colspan','3');
		  tdHead.appendChild(document.createTextNode(dateString));
		  trHead.appendChild(tdHead);
		  tableBody.appendChild(trHead);
		  prevDateString = dateString;
	  }
	  /* now display the event itself */
	  var timeString = 'All Day Event';
	  /* if the event has a time, override the default text */
	  if (!startTime.isDateOnly()) {
		  timeString = startTime.getDate().toString("h:mm tt")
	  }
	  /* create an anchor to the ThickBox remote call for the title */
	  var title = entry.getTitle().getText();
	  var uri = entry.getSelfLink().getHref();
	  var anchorTitle = document.createElement('a');
	  anchorTitle.setAttribute('href','javascript:loadCalendarEntry("' + uri + '")');
	  anchorTitle.appendChild(document.createTextNode(title));
	  

	  /* add the event time and title to the table */
	  var trEntry = document.createElement('tr');
	  var tdEntryTime = document.createElement('td');	  
	  var tdEntryTitle = document.createElement('td');	
	  var tdEntryCity = document.createElement('td');  
	  tdEntryTime.setAttribute('class','wpng-page-list-time');
	  tdEntryTitle.setAttribute('class','wpng-page-list-title');
	  tdEntryCity.setAttribute('class','wpng-page-list-city');
	  tdEntryTime.appendChild(document.createTextNode(timeString));
	  tdEntryTitle.appendChild(anchorTitle);

	  var locations = entry.getLocations();
	  var city = getCityWithRetry(locations[0].getValueString());
	  
	  tdEntryCity.appendChild(document.createTextNode(city));
	 
	  trEntry.appendChild(tdEntryTime);
	  trEntry.appendChild(tdEntryTitle);
	  trEntry.appendChild(tdEntryCity);
	  tableBody.appendChild(trEntry);
	  /* get the date from the first / last entry */
	  if (i == 0) {
		  firstDate = displayTime;
	  }
	  else if (i == (len - 1)) {
	          lastDate = displayTime;
	  }
  }
  /* Append the table body to the table */
  table.appendChild(tableBody);
  
  /* if there were some events, add the table */
  if (len != 0) {
	  /* add the table to the event DIV */
	  eventDiv.appendChild(table);
  }
  else {
	  /* show a default message */
	  eventDiv.appendChild(document.createTextNode('No events to show.'));
  }
  	  
  /* at the end of the list, show the navigation links */
  if (showNav) {
	  eventDiv.appendChild(document.createElement('br'));
	  var navTable = document.createElement('table');
	  var navTableBody = document.createElement('tbody');
	  navTable.setAttribute('className','wpng-page-list-table');
	  navTable.setAttribute('class','wpng-page-list-table');
	  var row = document.createElement('tr');
	  
	  var tdOlder = document.createElement('td');
	  var anchorOlder = document.createElement('a');
	  anchorOlder.setAttribute('href','javascript:getOlderEntries()');
	  anchorOlder.appendChild(document.createTextNode('< Show older events'));
	  tdOlder.appendChild(anchorOlder);
	  row.appendChild(tdOlder);
	  
	  var tdLater = document.createElement('td');
	  tdLater.setAttribute('align','right');
	  var anchorLater = document.createElement('a');
	  anchorLater.setAttribute('href','javascript:getLaterEntries()');
	  anchorLater.appendChild(document.createTextNode('Show later events >'));
	  tdLater.appendChild(anchorLater);
	  row.appendChild(tdLater);
	  
	  navTableBody.appendChild(row);
	  navTable.appendChild(navTableBody);
	  eventDiv.appendChild(navTable);
  }
  
  /* Hide the loading image */
  $j("#wpng-cal-load-page").fadeOut("fast");
  
  /* Animate the display of the list */
  $j("#wpng-cal-events").slideDown("slow");
}
function getCityWithRetry(address) {
	var city = getCity(address);
	if(city == "unknown") {
		city = getCity(address);
	}
	return city;
}

function getCity(address) {
	var geocoder = new GClientGeocoder();
	City.prototype.city = "unknown";
	
	geocoder.getLocations(address, function (response)
{
	var place;
	var state;
	var city;
    if (response && response.Placemark) {
      	place = response.Placemark[0];
      	city = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
      	state = place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
	 	City.prototype.city = (city + ", " + state);
	}
});
    
    return City.prototype.city;
}

function City(){
	var city;
}

