<?xml version="1.0" encoding="UTF-8" ?> 
<Module>
  <ModulePrefs title="Rodalies BCN Watchdog" 
     title_url="http://www.domocat.com/bicingadget/renferod.xml" 
     height="230" 
     author="Pedro Hernández" 
     author_email="pedro@domocat.com" />
  <Content type="html">
  <![CDATA[ 
  <div id="content_div"></div>
  <script type="text/javascript"> 
	// This gadget fetches data from the renfe.com conmuter trains information page 
	// and displays it a bit reshaped in order to better fit the igoogle environment

	// Global vars for general functionality
	var nummins = 5; // Refresh of the information for the Google Cache
	var dif = 1;     // Interval of hours to be showed
	var estor = "78805" // Departure station (Plaça Catalunya in BCN)
	var estde = "72302" // Destionation station (Sant Joan Despi in BCN)

        var today=new Date();
	var h=today.getHours();

	var d=today.getDate();
	d=checkTime(d);
	var mo=today.getMonth()+1;
	mo=checkTime(mo);
	var y=1900+today.getYear();

	var travelday="";
	travelday+=y+mo+d;

	var inetaddress = ""
	// If it is before 12 am, then show the direct journey, Else show the round trip.
	if (h < 12) {
		inetaddress = "http://horarios.renfe.es/cer/hjcer310.jsp?&nucleo=50&o="+estor+"&d="+estde+"&tc=DIA&td=D&df="+travelday+"&th=1&ho="+h+"&hd="+(h+dif)+"&i=0&cp=SI&TXTInfo= ";
		} else {
		inetaddress = "http://horarios.renfe.es/cer/hjcer310.jsp?&nucleo=50&o="+estde+"&d="+estor+"&tc=DIA&td=D&df="+travelday+"&th=1&ho="+h+"&hd="+(h+dif)+"&i=0&cp=SI&TXTInfo= ";  
		}

 	_IG_FetchContent(inetaddress, function (responseText) {

     	// Set CSS for div.
	var html= "<div style='padding: 5px;background-color: #FFFFBF;font-family:Arial, Helvetica;"+"text-align:left;font-size:small'>"; 
        
      	// Use the search function to find the position in the text of the station data
	var firstheading = "<table border=0 cellpadding=0 cellspacing=0 width=95% align=center>";
	var  pos_stat = responseText.search(firstheading) + firstheading.length;
	// And the end position of this part is determined by the start of the second.
	var secondheading = "<table border=0 cellpadding=1 cellspacing=1 width=95% align=center>"
	var pos_fin = responseText.search(secondheading);
	
	//Append Heading
	html += "<table border=0 cellpadding=0 cellspacing=0 width=95% align=center style='font-size:x-small'>";
        // Append substrings to html.
	html += responseText.substring(pos_stat,pos_fin);
	html += " ";
	
	//Append second part of the table: Heading...
	html += "<table border=0 cellpadding=1 cellspacing=1 width=95% align=center style='font-size:x-small'>";
	// and the actual information
	pos_stat = pos_fin + secondheading.length;
	var pos_fin = responseText.search("<tr><td align=right colspan=20><br>");
        html += responseText.substring(pos_stat,pos_fin);
	
	// End tag for the information page
	html += "</table>";
    	html += "</div>";

	//html=inetaddress;
    	// Output html in div.
    	_gel('content_div').innerHTML = html; 
	}, nummins*60);
	function checkTime(i)
	{
		if (i<10) 
		  {i="0" + i}
		  return i;
	}
 	</script>
     ]]>
  </Content> 
</Module>

