/**
 * JavaScript support for loading data
 * (c) Simon Brooke 2007
 *
 * $Revision: 1.7 $
 *
 */

/*
 * $Log$
 */



/**
 * Get from this element the value of its first child having this name
 */
function getFirstChildValue( element, childName)
{
	var result = "unkown";
	var candidates = element.getElementsByTagName(childName);
	if ( candidates != null && candidates.length > 0)
	{
		result = candidates[0].firstChild.nodeValue;
	}
	
	return result;
}


 /**
  * called after the map and timeline have been rendered to fetch 
  * new datapoints and display them
  */
function refreshData( map, timeline)
{
	var dataurl = "wheelers/primitive";
	var events = [];

    GDownloadUrl( dataurl, 
	      function(data, responseCode) 
	      {
			  var xml = GXml.parse(data);
 			   			  
			  var datapoints = xml.documentElement.getElementsByTagName("story");
			  for (var i = 0; i < datapoints.length && i < 20; i++) 
			  {
			  	var id = datapoints[i].getAttribute("article");
			  	  
			   	var location = datapoints[i].getElementsByTagName("location")[0];
			 	  
			   	var point = new GLatLng(parseFloat(location.getAttribute("latitude")),
						  parseFloat(location.getAttribute("longitude")));
				
			    var title = getFirstChildValue( datapoints[i], "title");
			    var story = getFirstChildValue( datapoints[i], "abstract");

			    var marker = new GMarker(point);

		      	var date = new Date();
		      	date.setTime(datapoints[i].getElementsByTagName( "created")[0].getAttribute( "unixtime"));
				var content = "<div style='font-size:80%;'><h4><a href='http://www.stewartry-wheelers.org/wheelers/story/article_" + 
						id + "'>" + title + "</a><h4>" +
						"<p>" + story + " <a onclick='centerTimeline(\"" + date + "\")'>[focus timeline]</a></p>"+
						"</div>";

		      	marker.content = content;
		      	
		      	marker.date = date;
			      
		      	GEvent.addListener( marker, "mouseover", 
			      		function()
			      		{
			      			/* centerTimeline( marker.date); */
			      		    map.openInfoWindowHtml( marker.content);
			      		});
			      		
			  	/* add the marker to my map */
		      	map.addOverlay( marker );
		      	
		      	/* thanks to joern@techfak.uni-bielefeld.de for this next section 
		      	 * Now add an event to the timeline representing the same item. Arguments are: 
		      	 * start, end, latestStart, earliestEnd, instant, text, description, image, 
		      	 * link, icon, color, textColor */
		      	var color="red";
		      	var evt = new Timeline.DefaultEventSource.Event(date,null,null,null,
                                                    false,title,story,
                                                    null,null,
                                                    null,color,null);
			    evt.mapmarker = marker;
    			events.push(evt);
		      	
		 	  }
		 	  /* add all events to the timeline */
        	  timeline.eventSource.addMany(events);
        	  /* and move the timeline and map to the most recent entry */
        	  var latest = eventSource.getLatestDate();
        	  map.panTo( latest.mapmarker.getPoint());
        	  timeline.getBand(0).setCenterVisibleDate( latest);
		 	  		  	 
	     	});
} 

