

var locations;
var map;
var geocoder;




function showAddress(location) {
  var html = "<b>"+location.name +"</b><br/>";

  var bubbleHtml = html;

  var directionsLink = '<a href="http://maps.google.com/maps?saddr=&daddr=' + encodeURI(location.address) + '" target ="_blank">Click for Driving Directions</a>';

  bubbleHtml += "<p>" + directionsLink + "</p>";

  html += "<p>" + location.full_address + "<br/>" + directionsLink + "</p>";

  if (location.atm) {
	html += "<p><b>ATM LOCATION:</b><br/>" + location.atm + "</p>";
	bubbleHtml += "<p><b>ATM LOCATION:</b><br/>" + location.atm + "</p>";
  }

  if (location.lobby) {
	html += "<p><b>LOBBY HOURS:</b><br/>" + location.lobby + "</p>";
  }

  if (location.drive_up) {
	html += "<p><b>DRIVE UP HOURS:</b><br/>" + location.drive_up + "</p>";
  }
   
  if (location.lat && location.lng) {
	var point = new GLatLng(location.lat, location.lng, true);
	addMarkerAtPoint(point, html, bubbleHtml);
  } else {
  	geocoder.getLatLng(
    	location.address,
    	function(point) {
      		if (!point) {
        		alert(location.address + " not found");
	       } else {
			addMarkerAtPoint(point, html, bubbleHtml);
      		}
    	});
        pause(225);
  }
}

function addMarkerAtPoint(point, html, bubbleHtml) {
        var marker = new GMarker(point);
        map.addOverlay(marker);
	 GEvent.addListener(marker, "click", function() {
          $('bankingbarint').innerHTML = html	;
	  marker.openInfoWindowHtml(bubbleHtml);
      });
}

function processJSON(jsonData){
         locations = eval('('+jsonData.responseText+')');
         for (i=0; i<=locations.length; i++) {
                showAddress(locations[i]);
        }
}



    function doload() {
      if (GBrowserIsCompatible()) {
	$('bankingbarint').innerHTML="<b>To use the map:</b><p>Click on any of the pushpins in the map to view information about that branch.</p> <p>Click and drag in the map window to navigate.</p>";
        map = new GMap2(document.getElementById("gmap"));
        map.addControl(new GLargeMapControl());
        map.addControl(new GScaleControl());

        map.setCenter(new GLatLng(38.6500, -90.2967), 9);
        geocoder = new GClientGeocoder();
        var req = new Ajax.Request('locations.pl',{
		method:'get',
                onSuccess:processJSON
        });
      }
}

function pause(millisecondi) {
  var now = new Date();
  var exitTime = now.getTime() + millisecondi;
  while(true)
  {
    now = new Date();
    if(now.getTime() > exitTime) return;
  }
}
