// this javascript code is based on Google Map API 
//  and Blackpool Community Church Javascript Team
//  http://www.econym.demon.co.uk/googlemaps/

//<![CDATA[

// info window content
var address = "135 Main Cross St. Hawesville, KY 42348";
var info = "<div class=\"gmap\"><a href=\"../\">Complete Wellness Chiropractic & Rehabilitation</a><ul><li>135 Main Cross St.</li><li>Hawesville, KY 42348</li><li>270-927-1000</li><li><a href=\"contactUs.html\">Contact Us</a></li></ul></div>Get Directions: ";

var gmarkers = [];
var htmls = [];
var to_htmls = [];
var from_htmls = [];

// depends on function in jquery.js
$(document).ready(mapLoad);
$(document).unload(GUnload);

function mapLoad() {
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map"));
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.enableDoubleClickZoom();
    map.enableContinuousZoom();
    map.setCenter(new GLatLng(37.902035,-86.748807), 12);

    var centerMarker = createMarker(map.getCenter(), info)
    map.addOverlay(centerMarker);

    // call cancel() to openInfoWindowHtml
    cancel(0);
  }
  //else {
  //	$("#map").append(info + googleMapExternalLink);
  // 	alert("Sorry, Google Maps is not compatible with this browser");
  //}
}

  // A function to create the marker and set up the event window
  function createMarker(point, html) {
    var marker = new GMarker(point);
    var i = 0;
    var formStart = "<form action=\"http://maps.google.com/maps\" id=\"directions\" method=\"get\" target=\"_blank\">";
    var formMiddle = "&nbsp;<input value=\"Go\" id=\"getDirections\" name=\"getDirections\" type=\"submit\">";
    var formEnd = "</form><a href=\"javascript:cancel(" + i + ")\">«Back</a>";

    // The info window version with the "to here" form open
    to_htmls[i] = html + "To here - <a href=\"javascript:fromHere(" + i + ")\">From here</a>" +
       "<br />Start address: " + formStart +
       "<input type=\"text\" size=\"35\" maxlength=\"40\" id=\"saddr\" name=\"saddr\" value=\"\" />" +
       formMiddle +
       "<input type=\"hidden\" id=\"daddr\" name=\"daddr\" value=\"" + address + 
              // "(" + name + ")" + 
       "\"/>" +
       formEnd;

    // The info window version with the "from here" form open
    from_htmls[i] = html + "<a href=\"javascript:toHere(" + i + ")\">To here</a> - From here" +
       "<br />End address: " + formStart +
       "<input type=\"text\" size=\"35\" maxlength=\"40\" name=\"daddr\" id=\"daddr\" value=\"\" />" +
       formMiddle +
       "<input type=\"hidden\" name=\"saddr\" value=\"" + address +
              // "(" + name + ")" + 
       "\"/>" +
       formEnd;

    // The inactive version of the direction info
    html = html + "<a href=\"javascript:toHere(" + i + ")\">To here</a> - <a href=\"javascript:fromHere(" + i + ")\">From here</a>";

    GEvent.addListener(marker, "click", function() {
      marker.openInfoWindowHtml(html);
    });
    gmarkers[i] = marker;
    htmls[i] = html;
    i++;
    return marker;
  }

  // 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 cancel(i) {
    gmarkers[i].openInfoWindowHtml(htmls[i]);
  }
  
//]]>