 // this variable will collect the html which will eventualkly be placed in the side_bar
     var side_bar_html = "<ul>";

// arrays to hold copies of the markers and html used by the side_bar
// because the function closure trick doesnt work there
     var gmarkers = [];
     var htmls = [];
     var i = 0;
// A function to create the marker and set up the event window
// Each instance of the function preserves the contends of a different instance
// of the "marker" and "html" variables which will be needed later when the event triggers.

function createMarker(point,name,html) {
        var marker = new GMarker(point);
        GEvent.addListener(marker, "click", function() {
	        marker.openInfoWindowHtml(html);
	        });
	// save the info we need to use later for the side_bar
        gmarkers[i] = marker;
        htmls[i] = '<div style="width:220px"><span style="font-weight:bold">' + name + ".</span><br>" ;
        htmls[i] += html;
	// add a line to the side_bar html
	side_bar_html += '<li><a href="javascript:myclick(' + i + ')">' + name + "</a></li><br>";
        i++;
	return marker;
      } 
      
function KMLparser(path,map){ //path: url to the kml file. map: div where the map is
        var request = GXmlHttp.create();
        request.open('GET', path, true);
        request.onreadystatechange = function(){		
	    if (request.readyState == 4){
		var data = GXml.parse(request.responseText);
		var placemarks = data.documentElement.getElementsByTagName("Placemark");
		for(var i=0; i<placemarks.length; i++){
			var coordinates;
			coordinates = placemarks[i].getElementsByTagName("coordinates")[0].childNodes[0].nodeValue;
			for(var chunk=1;  chunk<placemarks[i].getElementsByTagName("coordinates")[0].childNodes.length;chunk++){
				 coordinates+=placemarks[i].getElementsByTagName("coordinates")[0].childNodes[chunk].nodeValue;
			}
			coordinates = coordinates.split(" ");
			for(var j=0; j<coordinates.length;j++){
				coordinates[j] = coordinates[j].split(",");
			}
			if(coordinates.length == 1){
				var point = new GLatLng(parseFloat(coordinates[0][1]),parseFloat(coordinates[0][0]));
				var name = placemarks[i].getElementsByTagName("name")[0].childNodes[0].nodeValue;
				var desc = placemarks[i].getElementsByTagName("description")[0].childNodes[0].nodeValue;
										
				var marker = createMarker(point,name,desc);
				map.addOverlay(marker);
			}
			else{
				var points = new Array();
		                for(var j=0; j<coordinates.length; j++){
					points.push(new  GLatLng(parseFloat(coordinates[j][1]),parseFloat(coordinates[j][0])));	
				}
				var temp = new GPolygon(points,"#00FF00",2,1,"#00FF00",0);
				map.addOverlay(temp);
			}								
		}			
	    }		
        }
        request.send(null);	
}
