var gmarkers = [];
var htmls = [];
var to_htmls = [];
var from_htmls = [];
var i = 0;

window.onload = function showMap() {
    if (GBrowserIsCompatible()) {
        
        function createMarker(point, name, html) {
            var marker = new GMarker(point);
            
            to_htmls[i] = html + '<form action="http://maps.google.com/maps" method="get" target="_blank"><ul style="list-style: none"><li style="margin-top: .5em">Directions: <span style="font-weight: bold">To here</span> - <a href="javascript:fromhere(' + i + ')">From here</a></li>' + '<li>Start address:</li>' + '<li><input type="text" size="40" maxlength="40" name="saddr" id="saddr" value=""></li>' + '<li style="margin-top: .25em"><input type="submit" value="Get Directions"></li></ul>' + '<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() + '(' + name + ')' + '"></form><p style="font-size: 80%; font-style: italic">Driving directions will open a new window and connect with<br>Google\'s mapping service.</p>';
            
            // The info window version with the "to here" form open
            from_htmls[i] = html + '<form action="http://maps.google.com/maps" method="get"" target="_blank"><ul style="list-style: none"><li style="margin-top: .5em">Directions: <a href="javascript:tohere(' + i + ')">To here</a> - <span style="font-weight: bold">From here</span></li>' + '<li>End address:</li>' + '<li><input type="text" size="40" maxlength="40" name="daddr" id="daddr" value=""></li>' + '<li style="margin-top: .25em"><input type="submit" value="Get Directions"></li></ul>' + '<input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() + '(' + name + ')"></form><p style="font-size: 80%; font-style: italic">Driving directions will open a new window and connect with<br>Google\'s mapping service.</p>';
            
            // The inactive version of the direction info
            html = html + '<p>Directions: <a href="javascript:tohere('+i+')">To here</a> - <a href="javascript:fromhere('+i+')">From here</a></p><p style="font-size: 80%; font-style: italic">Driving directions will open a new window and connect with<br>Google\'s mapping service</p>';
            
            GEvent.addListener(marker, "click", function() {
                marker.openInfoWindowHtml(html);
            });

            gmarkers[i] = marker;
            htmls[i] = html;
            i++;

            map.openInfoWindowHtml(map.getCenter(), html);

            return marker;
        }
        
        // Display the map, with some controls and set the initial location 
        var map = new GMap2(document.getElementById("map"));
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(34.655592,-92.393603), 13);
        
        var point = new GLatLng(34.655592,-92.393603);
        var marker = createMarker(point,'First Missionary Baptist Church','<p style="font-weight: bold">First Missionary Baptist Church</p><p>10300 Mabelvale W. Rd. &#183; Mabelvale, AR 72103</p>')
        map.addOverlay(marker);
    }
}
        
function tohere(i) {
    gmarkers[i].openInfoWindowHtml(to_htmls[i]);
}
function fromhere(i) {
    gmarkers[i].openInfoWindowHtml(from_htmls[i]);
}
