var map;
var centerLatitude = 26.279701;
var centerLongitude = -80.25921;
var startZoom = 13;


/* [listing 6-15] */
var deselectCurrent = function() {};

function initializePoint(pointData) {
	var point = new GLatLng(pointData.latitude, pointData.longitude);
	var icon = new GIcon();
		icon.image = "http://interactive.sun-sentinel.com/crime_news/blue-dot.png";
		//icon.iconSize = new GSize(13, 30);
		icon.shadowSize = new GSize(22, 20);
		icon.iconAnchor = new GPoint(6, 20);
		icon.infoWindowAnchor = new GPoint(5, 1);
	var marker = new GMarker(point,{icon:icon});
	var listItem = document.createElement('li');
	var listItemLink = listItem.appendChild(document.createElement('a'));
	listItemLink.href = "#";
	listItemLink.innerHTML = '<table><tr><td><img src="blue-dot.png" /></td>' +
	'<td><strong>' + pointData.crime_type + '</div></strong><span>' + pointData.address + '<br/>' +
	pointData.city + '</td></tr></table>';
	
	var focusPoint = function() {
		deselectCurrent();
		listItem.className = 'current';
		deselectCurrent = function() { listItem.className = ''; }
		if (pointData.city == '') {
	marker.openInfoWindowHtml('<table width="315"><tr><td>' + '<b>' + pointData.crime_type.toUpperCase() + '</b>' + '<br/>' + pointData.address + '<br/>' + pointData.height + ', ' + pointData.elevation + '<br/>' + pointData.description + '</td></tr></table>');
	
}
else {
marker.openInfoWindowHtml('<table width="315"><tr><td>' + '<b>' + pointData.crime_type.toUpperCase() + '</b>' + '<br/>' + pointData.address + '<br/>' + pointData.height + ', ' + pointData.elevation + '<br/>' + pointData.description + '</td></tr></table>');
}
		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 - 30;
	document.getElementById('map').style.height = height + 'px';
	document.getElementById('sidebar').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 GSmallMapControl());
	map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
	
	for(id in markers) {
		initializePoint(markers[id]);
	}
}

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





