google.load("feeds", "1",{"nocss" : true});
var map;
var infowindows = new Array();

function findlong(rssentry)
{
	for (var j=0;j<rssentry.length; j++) 
	{
		if ( rssentry[j]=="long:" ) {
			long=rssentry[j+1];
			var n=long.replace(/\,/,"");
			long=n;
			return long;
		}
	}
}

function findlat(rssentry)
{
	for (var j=0;j<rssentry.length; j++) 
	{
		if ( rssentry[j]=="lat:" ) {
			lat=rssentry[j+1];
			var n=lat.replace(/\,/,"");
			lat=n;
			return lat;
		}
	}
}

function process_one_rss(entry)
{	
var rssentry;
var both;
var lat;
var long;
var myLatlng;
var marker;

	//alert("dentro process_one_rss");
	//alert(entry["title"]);
	/*
	entry["title"] is the title of the entry in the feed;
	entry["content"] is the content of the entry in the feed
	I need to parse the content, extract lat and long (to start with)
	and create the marker 
	*/
	rssentry=entry["content"].split(" ");
	both=0;
	lat = findlat(rssentry);
	long = findlong(rssentry);
	/* now create marker */
	myLatlng = new google.maps.LatLng(lat, long);
	marker = new google.maps.Marker({
				clickable: true,
				position: myLatlng, 
				map: map,
				title: entry["title"]
			});   
	// now here we need to sort out the bloody infowindow+listener problem!!
  var infowindow = new google.maps.InfoWindow(
      { content: entry["title"],
        size: new google.maps.Size(50,50)
      });
infowindows.push(infowindow);
	
  google.maps.event.addListener(marker, 'click', function() {
	// go through the markers and close them
	for (var i=0; i<infowindows.length; i++) {
		infowindows[i].close();
	}
    	infowindow.open(map,marker);
  });
}

function process_rss(result)
{
	if (!result.error) 
	{
		for (var i = 0; i < result.feed.entries.length; i++) 
		{
			//alert(i);	
			process_one_rss(result.feed.entries[i]);
		}
	}
}

function initialize() 
{
	var lat="";
	var long="";
	var markercounter=0;
	var myLatlng = new google.maps.LatLng(37.33165, -122.030704);
	var myOptions = {
	      zoom: 2,
	      center: myLatlng,
	      mapTypeId: google.maps.MapTypeId.ROADMAP
	    }
	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	/* for the rss feed */
	var feed = new google.feeds.Feed("http://spreadsheets.google.com/feeds/list/0AqFzt44EOaD4dFpJVTBEM2J6eXVadHotZmtHWnBHbEE/od6/public/basic?alt=rss");
	feed.setNumEntries(1000000);
	feed.load(process_rss);
}

