// For drawing the location map for Rocky Ridge

// This Javascript is based on code provided by the
// Blackpool Community Church Javascript Team
// http://www.commchurch.freeserve.co.uk/   
// http://econym.googlepages.com/index.htm

var side_bar_html = "";
var gmarkers = [];
var htmls = [];
// arrays to hold variants of the info window html with get direction forms open
var to_htmls = [];
var from_htmls = [];

// This function picks up the click and opens the corresponding info window
function openSpecificWindow(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]);
}

// ===== request the directions =====
function getDirections() {
  // ==== set the start and end locations ====
  var saddr = document.getElementById("saddr").value;
  var daddr = document.getElementById("daddr").value;
  gdir.load("from: "+saddr+" to: "+daddr);
}

function initMap() {
	
	if (GBrowserIsCompatible()) {


		// 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 info window version with the "to here" form open
		  to_htmls[i] = html + '<div style="padding-top: 10px; font-size: 94%;">Directions: <b>To here<\/b> - <a href="javascript:fromhere(' + i + ')">From here<\/a>' +
		     '<br />Start address:<form action="javascript:getDirections()">' +
		     '<input type="text" size="40" maxlength="40" name="saddr" id="saddr" value="" /><br />' +
		     '<input value="Get Directions" type="submit" /><br />' +
		     '<input type="hidden" id="daddr" value="'+name+"@"+ point.lat() + ',' + point.lng() + 
		     '"/></div>';
		  // The info window version with the "from here" form open
		  from_htmls[i] = html + '<div style="padding-top: 10px; font-size: 94%;">Directions: <a href="javascript:tohere(' + i + ')">To here<\/a> - <b>From here<\/b>' +
		     '<br />End address:<form action="javascript:getDirections()">' +
		     '<input type="text" size="40" maxlength="40" name="daddr" id="daddr" value="" /><br />' +
		     '<input value="Get Directions" type="submit" /><br />' +
		     '<input type="hidden" id="saddr" value="'+name+"@"+ point.lat() + ',' + point.lng() +
		     '" /></div>';
		  // The inactive version of the direction info
		  html = html + '<div style="padding-top: 10px; font-size: 94%;">Directions: <a href="javascript:tohere('+i+')">To here<\/a> - <a href="javascript:fromhere('+i+')">From here<\/a></div>';

		  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;

		  return marker;
		}

	  // create the map
	  rrscMap = new GMap2(document.getElementById("map-canvas"));
  
	  // create the two locations
		var yard = new GLatLng(35.948481,-84.980836);
		var yardHtml = "<div style='padding-top: 0; line-height: 1.4em;'><h3 style='margin-top: 0; margin-bottom: 5px; font-size: 16px;'>Rocky Ridge Quarry/Yard</h3>204 Deathridge Road<br />Crossville TN 38555</div>";
		var yardMarker = createMarker(yard, "Rocky Ridge Quarry/Yard", yardHtml);
	  rrscMap.addOverlay(yardMarker);
	
//	var business = new GLatLng(35.746512,-85.045166);
//	var businessHtml = "<h3 style='margin-top: 0; margin-bottom: 5px'>Business Address</h3>29 Pugh Cemetery Road<br />Crossville TN 38571";
//	var businessMarker = createMarker(business, "Business Addresss", businessHtml);	
//  rrscMap.addOverlay(businessMarker);		

	  rrscMap.setUIToDefault();
	  rrscMap.setCenter(yard, 6);
    rrscMap.disableScrollWheelZoom();

		openSpecificWindow(0);

	  // === create a GDirections Object ===
	  gdir = new GDirections(rrscMap, document.getElementById("direction-results"));
	  GEvent.addListener(gdir, "load", function() {
	    $('#directions').slideDown('fast');
	  });

	  // === Array for decoding the failure codes ===
	  var reasons=[];
	  reasons[G_GEO_SUCCESS]            = "Success";
	  reasons[G_GEO_MISSING_ADDRESS]    = "Missing Address: The address was either missing or had no value.";
	  reasons[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
	  reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
	  reasons[G_GEO_BAD_KEY]            = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
	  reasons[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
	  reasons[G_GEO_SERVER_ERROR]       = "Server error: The geocoding request could not be successfully processed.";
	  reasons[G_GEO_BAD_REQUEST]        = "A directions request could not be successfully parsed.";
	  reasons[G_GEO_MISSING_QUERY]      = "No query was specified in the input.";
	  reasons[G_GEO_UNKNOWN_DIRECTIONS] = "The GDirections object could not compute directions between the points.";

	  // === catch Directions errors ===
	  GEvent.addListener(gdir, "error", function() {
	    var code = gdir.getStatus().code;
	    var reason="Code "+code;
	    if (reasons[code]) {
	      reason = reasons[code]
	    } 

	    alert("Failed to obtain directions, "+reason);
	  });

	}

}

