window.addEvent('domready', function(){
	// if map is present, load google maps
	if($("map")) {
		load();
	}
});

//       function initialize() {
//         if(GBrowserIsCompatible()) {
//           var map = new GMap2(document.getElementById('map'));
//           map.setCenter(new GLatLng(39.91, 116.38), 2);
//           map.addControl(new GLargeMapControl());
//           var icon = new GIcon(G_DEFAULT_ICON);
//           icon.image = "http://chart.apis.google.com/chart?cht=mm&chs=24x32&chco=FFFFFF,008CFF,000000&ext=.png";
//           var markers = [];
//           for (var i = 0; i < 100; ++i) {
//             var latlng = new GLatLng(data.photos[i].latitude, data.photos[i].longitude);
//             var marker = new GMarker(latlng, {icon: icon});
//             markers.push(marker);
//           }
//           var markerCluster = new MarkerClusterer(map, markers);
//         }
//       }


function load() {
	if (GBrowserIsCompatible()) {		
	  // 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;
	
	// Global variables
	var poly;
	var count = 0;
	var icon_url ="http://labs.google.com/ridefinder/images/";
	var lineColor = "#0000af";
	var fillColor = "#335599";
	var lineWeight = 3;
	var lineOpacity = .8;
	var fillOpacity = .2;
	
	function addIcon(icon) { // Add icon attributes
	 icon.shadow= icon_url + "mm_20_shadow.png";
	 icon.iconSize = new GSize(30, 47);
	 icon.shadowSize = new GSize(40, 47);
	 icon.iconAnchor = new GPoint(10, 47);
	 icon.infoWindowAnchor = new GPoint(15, 0);
	}
	
	  // A function to create the marker and set up the event window
	  function createMarker(point,name,html,username) {	  
		var marker = new GMarker(point,{draggable:true, bouncy:false, dragCrossMove:true});
		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] = html;
		i++;
		return marker;
	  }
	
	
	  // create the map
	  var map = new GMap2(document.getElementById("map"));
	  map.addControl(new GLargeMapControl());
	  map.addControl(new GMapTypeControl());
	  map.setCenter(new GLatLng( 42.28,-83.745), 5, G_HYBRID_MAP);
	  
	  // === Define the function thats going to process the text file ===
	  function process_it(doc) {
	  	var points = [];
		// === split the document into lines ===
		lines = doc.split("\n");
		var prevPoint;
		for (var i=0; i<lines.length; i++) {
		  if (lines[i].length > 1) {
			// === split each line into parts separated by "|" and use the contents ===
			parts = lines[i].split("|");
			var username = parts[0];
			var lat = parseFloat(parts[1]);
			var lng = parseFloat(parts[2]);
			var html = parts[3];
			var label = parts[0];
			var point = new GLatLng(lat,lng);
			// create the marker
			var marker = createMarker(point,label,html,username);
			points.push(marker.getLatLng());
			map.addOverlay(marker);
		  }
		}
		points = new GPolyline(points, lineColor, lineWeight, lineOpacity);
		map.addOverlay(points);
	  }          		  
	  GDownloadUrl("../words.txt", process_it);
	}
}
