var map;
var centerLatitude = 33.827383;
var centerLongitude = -116.795417; 
var startZoom = 11;
/* [listing 6-15] */
var deselectCurrent = function() {};

function initializePoint(pointData) {
	var point = new GLatLng(pointData.latitude, pointData.longitude);
	var marker = new GMarker(point);
	var listItem = document.createElement('li');
	var listItemLink = listItem.appendChild(document.createElement('a'));
	listItemLink.href = "#";
	listItemLink.innerHTML = '<strong>' + pointData.trailName + ' </strong><span>Trail Difficulty: ' + pointData.difficulty + ', Trail Length (roundtrip): ' + pointData.length + ' mile(s), Elevation: ' + pointData.elevation + 'ft, Elevation Gain: ' + pointData.elevationGain + 'ft</span>';
	
	var focusPoint = function() {
		deselectCurrent();
		listItem.className = 'current';
		deselectCurrent = function() { listItem.className = ''; }
		marker.openInfoWindowHtml('<strong>' + pointData.trailName + ' </strong><br /><span>Trail Difficulty: ' + pointData.difficulty + '<br />Trail Length (roundtrip): ' + pointData.length + ' mile(s)<br />Elevation: ' + pointData.elevation + 'ft<br />Elevation Gain: ' + pointData.elevationGain + 'ft</span>');
		map.panTo(point);
		return false;
	}

	GEvent.addListener(marker, 'click', focusPoint);	
	listItemLink.onclick = focusPoint;

	document.getElementById('sidebar-list').appendChild(listItem);

	map.addOverlay(marker);
}
/* [listing 6-15 end] */

function windowHeight() {
	// Standard browsers (Mozilla, Safari, etc.)
	if (self.innerHeight)
		return self.innerHeight;
	// IE 6
	if (document.documentElement && document.documentElement.clientHeight)
		return document.documentElement.clientHeight;
	// IE 5
	if (document.body)
		return document.body.clientHeight;
	// Just in case.
	return 0;
}

function handleResize() {
	var height = windowHeight() - document.getElementById('toolbar').offsetHeight - 5;
	document.getElementById('map').style.height = height + 'px';
	document.getElementById('sidebar-map').style.height = height + 'px';
}

function changeBodyClass(from, to) {
     document.body.className = document.body.className.replace(from, to);
     return false;
}

function init() {
	document.getElementById('button-sidebar-hide').onclick = function() { return changeBodyClass('sidebar-right', 'nosidebar'); };
	document.getElementById('button-sidebar-show').onclick = function() { return changeBodyClass('nosidebar', 'sidebar-right'); };
	
	handleResize();
	
	map = new GMap(document.getElementById("map"));
	map.addControl(new GLargeMapControl());
	map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom, G_HYBRID_MAP);
	map.addControl(new GMapTypeControl());
	
	for(id in markers) {
		initializePoint(markers[id]);
		changeBodyClass('loading', 'standby');
	}
	
}

window.onresize = handleResize;
window.onload = init;