var map = null;
var geocoder = null;
var bounds = new GLatLngBounds();
var j = ["45.460922,-123.969747"];
var names = ["<strong>Oceanfront Cabins</strong>"];
var htmls = [];
// arrays to hold variants of the info window html with get direction forms open
var to_htmls = [];
var from_htmls = [];
var gmarkers = [];
function loadmap() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("googlemap"));
            map.setCenter(new GLatLng(45.460922, -123.969747), 14);
    map.addControl(new GSmallMapControl());
            map.addControl(new GMapTypeControl());
    geocoder = new GClientGeocoder();
            //add address markers
            for (var i=0; i < j.length; i++){
                addAddress(j[i],i);
            }
        }
        
}
    function addAddress(address,counter){
        if (geocoder) {
   geocoder.getLatLng(
      address,
      function(point) {
        if (!point) {
          alert(address + " not found");
        } else {
          //var marker = new GMarker(point);
          var marker = createMarker(point,counter);
                        map.addOverlay(marker);
                        bounds.extend(point);
                        centerMap(counter);
            
        }
      }
    );
  }
    }
    function centerAndZoomOnBounds(bounds){
        var center = bounds.getCenter();
        var newZoom = map.getBoundsZoomLevel(bounds);
        if (map.getZoom() != newZoom){
            map.setCenter(center, newZoom);
        }else{
            map.panTo(center);
        }
    }
    function centerMap(counter){
        if (counter == j.length-1) {
            // centerAndZoomOnBounds(bounds);
            activatedefault();
            }
    }
    // A function to create the marker and set up the event window
    function createMarker(point,counter) {
        var marker = new GMarker(point);
        var html = names[counter];
        // The info window version with the "to here" form open
        to_htmls[counter] = html + '<br>Directions: <b>To here</b> - <a href="javascript:fromhere(' + counter + ')">From here</a>' +
             '<br>Start address:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
             '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br>' +
             '<INPUT value="Get Directions" TYPE="SUBMIT">' +
             '<input type="hidden" name="daddr" value="' + j[counter] + "(" + names[counter] + ")" + 
             '"/>';
        // The info window version with the "to here" form open
        from_htmls[counter] = html + '<br>Directions: <a href="javascript:tohere(' + counter + ')">To here</a> - <b>From here</b>' +
             '<br>End address:<form action="http://maps.google.com/maps" method="get"" target="_blank">' +
             '<input type="text" SIZE=40 MAXLENGTH=40 name="daddr" id="daddr" value="" /><br>' +
             '<INPUT value="Get Directions" TYPE="SUBMIT">' +
             '<input type="hidden" name="saddr" value="' + j[counter] + "(" + names[counter] + ")" + 
             '"/>';
        // The inactive version of the direction info
        html = html + '<br><br>Get Driving Directions - Enter Your Address:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
             '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" style="width:220px;"/><br>' +
             '<INPUT value="Get Directions" TYPE="SUBMIT">' +
             '<input type="hidden" name="daddr" value="' + j[counter] + "(" + names[counter] + ")" + 
             '"/>';
        // html = '<div style="width:200px;">' + html + '<br><br>Directions: <a href="javascript:tohere('+counter+')">To here</a> - <a href="javascript:fromhere('+counter+')">From here</a></div>';
        GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(html);
        });
        gmarkers[counter] = marker;
        htmls[counter] = html;
        return marker;
    }
    // This function picks up the click and opens the corresponding info window
    function myclick(i) {
        gmarkers[i].openInfoWindowHtml(htmls[i]);
    }
    // functions that open the directions forms
    function tohere(i) {
        gmarkers[i].openInfoWindowHtml(to_htmls[i]);
    }
    function fromhere(i) {
        gmarkers[i].openInfoWindowHtml(from_htmls[i]);
    }

    function activatedefault() {
        myclick(0);
    }
