var map;
var bounds;
var geocoder;
var atmIcon;
var branchIcon;
var thepoints = new Array();
function load() {
	 if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(43.54599,-96.731291), 13);
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setZoom(11);

		bounds = new GLatLngBounds();
		geocoder = new GClientGeocoder();

		atmIcon = new GIcon();
		atmIcon.image = "/images/marker-atm.png";
		atmIcon.shadow = "/images/marker-shadow.png";
		atmIcon.iconSize = new GSize(64, 36);
		atmIcon.shadowSize = new GSize(64, 36);
		atmIcon.iconAnchor = new GPoint(23, 33);
		atmIcon.infoWindowAnchor = new GPoint(23, 1);
		
		branchIcon = new GIcon();
		branchIcon.image = "/images/marker-branch.png";
		branchIcon.shadow = "/images/marker-branch-shadow.png";
		branchIcon.iconSize = new GSize(35, 34);
		branchIcon.shadowSize = new GSize(35, 34);
		branchIcon.iconAnchor = new GPoint(17, 15);
		branchIcon.infoWindowAnchor = new GPoint(17, 1);
	 }
}

function setZoom() {
	map.setZoom(map.getBoundsZoomLevel(bounds));
	var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;
	var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;
	map.setCenter(new GLatLng(clat,clng));
	map.savePosition();
}

function getPlaceAndSubmit(theform) {
	var address = theform['location'].value;
	if (address.toLowerCase()=='sioux falls') address = 'Sioux Falls, SD';
	geocoder.getLocations(address, function(response){
	  if (!response || response.Status.code != 200) {
		// no address found
	  } else {
	    place = response.Placemark[0];
		theform['start_latitude'].value = place.Point.coordinates[1];
		theform['start_longitude'].value = place.Point.coordinates[0];
	  }
	  theform.submit();
	});
}

function makeThePoint(data) {
	var point = new GLatLng(data.lat, data.lon);
	var marker = new GMarker(point,{icon: eval(data.icon)});
	map.addOverlay(marker);
	bounds.extend(point);

	GEvent.addListener(marker, 'mouseover', function() {
	    if (data.is_atm) {
			var htmlString = '<div style="font-size:11px; line-height:16px; width:300px;"><b>'+data.name+'</b><br />'+data.address+'</div>';
		}
		else {
			var htmlString = '<div style="font-size:11px; line-height:16px; width:300px;">';
			if (data.image) {
				htmlString += '<img src="/images/locations/'+data.image+'" border=0 style="float:right" />';
			}
			htmlString += '<b>'+data.name+'</b><br />'+data.address+'<br />F: '+data.fax+'</div>';
		}
		marker.openInfoWindowHtml(htmlString);
	});
}

function makeThePoints(data, setView) {
	var i;
	for (i=0; i<data.length; i++) {
		makeThePoint(data[i]);
	}
	if(setView) {
		 setZoom();
	}
}
