// this variable will collect the html which will eventually be placed in the side_bar
var side_bar_html = "";

var gmarkers = [];
var htmls = [];
var i = 0;
var map;
var icon0;
var newpoints = new Array();
 
function addLoadEvent(func) { 
	var oldonload = window.onload; 
	if (typeof window.onload != 'function'){ 
		window.onload = func
	} else { 
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

addLoadEvent(loadMap);
addLoadEvent(addPoints);
 
function loadMap() {
	map = new GMap2(document.getElementById("map"));
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	map.setCenter(new GLatLng( -33.81850, 151.021700), 17);
	map.setMapType(G_MAP_TYPE);
 
	icon0 = new GIcon();
	icon0.image = "/persistent/templateimages/gmapMarker.png";
	icon0.shadow = "/persistent/templateimages/gmapMarkerShadow.png";
	icon0.iconSize = new GSize(75, 72);
	icon0.shadowSize = new GSize(80, 48);
	icon0.iconAnchor = new GPoint(28, 58);
	icon0.infoWindowAnchor = new GPoint(28, 2);
	icon0.infoShadowAnchor = new GPoint(18, 58);
}
 
function addPoints() {
 
	newpoints[0] = new Array(-33.81785, 151.022585, icon0, 'Rosehill', '<img src=/persistent/templateimages/logo_icon.jpg width=50 height=50 class=gmapsLogo /><strong>Code Red</strong><br /><br />Unit 3, 175 James Ruse Drive<br />(Cnr of Grand Ave <strong>North</strong>),<br />Rosehill, 2142.<br />');
 
	for(var i = 0; i < newpoints.length; i++) {
		var point = new GPoint(newpoints[i][1],newpoints[i][0]);
		var popuphtml = newpoints[i][4] ;
		var marker = createMarker(point,newpoints[i][2],popuphtml,newpoints[i][3]);
		map.addOverlay(marker);
		
		// put the assembled side_bar_html contents into the side_bar div
		document.getElementById("side_bar").innerHTML = side_bar_html;
	}
}
 
function createMarker(point, icon, popuphtml, name) {
	var popuphtml = "<div id=\"popup\">" + popuphtml + "<\/div>";
	var marker = new GMarker(point, icon);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(popuphtml);
	});
	// save the info we need to use later for the side_bar
	gmarkers[i] = marker;
	htmls[i] = popuphtml;
	// add a line to the side_bar html
	side_bar_html += '<a href="javascript:myclick(' + i + ')">' + name + '</a>&nbsp;&nbsp;&nbsp;&nbsp;';
	i++;
	return marker;
}

// This function picks up the click and opens the corresponding info window
function myclick(i) {
	gmarkers[i].openInfoWindowHtml(htmls[i]);
}
