function initMaps() {
    $('div[class=map]').each(function() {

        var fm = {};
        fm.maps = new Array();

        $(this).addClass('mapdimensions');
        var mapId = $(this).attr('id');

        var value = $(this).html();
        value = $.trim(value);
        var point = value.split(',');

        var lat = parseFloat(point[0]);
        var lon = parseFloat(point[1]);
        var zoom = parseFloat(point[2]);

        fm.maps[fm.maps.length] = new GMap2(document.getElementById(mapId));
        var m = fm.maps[fm.maps.length - 1];

        var p = new GLatLng(lat, lon);
        m.setCenter(p, zoom);

        var marker = createMarker(p, 'MY LABEL', '<b>Mid-Town Marian</b><br>285 5th St<br>East Dubuque, IL 61025<br>(815) 747-3310');
        m.addOverlay(marker);

        m.setMapType(G_HYBRID_MAP);
        m.addControl(new GLargeMapControl());
        m.addControl(new GMapTypeControl());
    });
}

$(document).ready(function() {
    initMaps();
});

var gmarkers = [];
var htmls = [];
// arrays to hold variants of the info window html with get direction forms open
var to_htmls = [];
var from_htmls = [];

// A function to create the marker and set up the event window
function createMarker(point, name, html) {

    var marker = new GMarker(point);
    var i = gmarkers.length;

    // The inactive version of the direction info
    html = html + '<br><form action="http://maps.google.com/maps" method="get" target="_blank">' +
                  '<p><label for="saddr">Get directions:</label><br>' +
                    '<input type="text" size="40" name="saddr" id="saddr" value="" />' +
                    '<input type="submit" value="Go" />' +
                    '<input type="hidden" name="daddr" value="285 5th St Exd, East Dubuque, IL 61025" />' +
                    '<input type="hidden" name="hl" value="en" />' +
                  '</p></form>';
        
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(html);
    });

    // save the info we need to use later for the side_bar
    gmarkers.push(marker);

    htmls[i] = html;
    
    // add a line to the side_bar html
    return marker;
}

// ===== request the directions =====
function getDirections() {
    // ==== Set up the walk and avoid highways options ====
    var opts = {};
    if (document.getElementById("walk").checked) {
        opts.travelMode = G_TRAVEL_MODE_WALKING;
    }
    if (document.getElementById("highways").checked) {
        opts.avoidHighways = true;
    }
    // ==== set the start and end locations ====
    var saddr = document.getElementById("saddr").value
    var daddr = document.getElementById("daddr").value
    gdir.load("from: " + saddr + " to: " + daddr, opts);
}

