var map;
var centerLatitude = 42.093125;
var centerLongitude = -70.692049; 
var startZoom = 13;
var deselectCurrent = function() {};
var icon = new GIcon();

function init() {
   icon.image = "/images/diamond1.png";
   //icon.shadow = "/images/mm_20_shadow.png";
   icon.iconSize = new GSize(32, 25);
   //icon.shadowSize = new GSize(22, 20);
   icon.iconAnchor = new GPoint(16, 25);
   icon.infoWindowAnchor = new GPoint(16, 1);

	document.getElementById('button-view-mybl').onclick = function() { return setCenterMYBL(); };
	document.getElementById('button-view-all').onclick = function() { return setCenterAll(); };
	
	map = new GMap2(document.getElementById("map"));
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
	map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
	
	for(id in markers) {
		initializePoint(markers[id]);
	}

	document.getElementById('button-view-mybl').style.display = 'none';
	showMarkersByType("Marshfield");
}

window.onload = init;

function initializePoint(pointData){
	var point = new GLatLng(pointData.latitude, pointData.longitude);

	var marker = new GMarker(point, icon);
	var listItem  = document.createElement('li');
	var listItemLink = listItem.appendChild(document.createElement('a'));
	var visible = false;
	listItemLink.href = "#";
	listItemLink.innerHTML = pointData.town + ' - <strong>' + pointData.fieldname + '</strong>';
	
	var focusPoint = function() {
		deselectCurrent();
		listItem.className = 'current';
		deselectCurrent = function(){ listItem.className = '';}

		marker.openInfoWindowHtml('<strong>' + pointData.fieldname + '</strong><br>' + pointData.address + '<br><br><a href=javascript:zoomTo(' + pointData.latitude + ',' + pointData.longitude + ',15)>Zoom In</a>&nbsp;&nbsp;<a href=javascript:zoomTo(' + pointData.latitude + ',' + pointData.longitude + ',13)>Zoom Out</a>');
		showDirections(pointData);
		map.panTo(point);
		return false;
	}
	
	GEvent.addListener(marker, 'click', focusPoint);
	listItemLink.onclick = focusPoint;
	
	pointData.show = function() {
		if (!visible) {
			document.getElementById('sidebar-list').appendChild(listItem);
			map.addOverlay(marker);
			visible = true;
		}
	}
	pointData.hide = function() {
		if (visible) {
			document.getElementById('sidebar-list').removeChild(listItem);
			map.removeOverlay(marker);
			visible = false;
		}
	}
}

function showDirections(pointData){
	document.getElementById('directions').innerHTML = "<h3>Directions to " + pointData.fieldname + ", " + pointData.address + " </h3>" + pointData.directions;
}

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 showMarkersByType(type) {

	// loop through markers
	// if town passed in is Marshfield, show only Marshfield Fields
	// if town passed in is All, show all fields
	for(id in markers) {
		if (markers[id].town == "Marshfield" || 'All' == type){
//		alert("show" + markers[id].fieldname);
			markers[id].show();
		}else{
//		alert("hide" + markers[id].fieldname);
			markers[id].hide();
		}	
	}
	return false;
}


function setCenterMYBL(){
	map.setCenter(new GLatLng(42.093125,-70.692049),13);
	document.getElementById('button-view-mybl').style.display = 'none';
	document.getElementById('button-view-all').style.display = '';
	showMarkersByType("Marshfield");
	return false;	  
}
function setCenterAll(){
 	map.setCenter(new GLatLng(42.159574,-70.82172),10);
	document.getElementById('button-view-mybl').style.display = '';
	document.getElementById('button-view-all').style.display = 'none';
	showMarkersByType("All");	  
	return false;	  
}
function zoomTo(lat, lng, zoomlevel){
	map.setCenter(new GLatLng(lat,lng),zoomlevel);  
}

