// JavaScript Document

// ===== This function picks up the click and opens the corresponding info window ===== 
      function myclick(i) {
		  //document.getElementById("details").innerHTML = htmlsdetails[i];
		  gmarkers[i].openInfoWindowHtml(htmls[i]);
		  map.setZoom(10);
      }
      
// ===== 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() {
        var saddr = document.getElementById("saddr").value
        var daddr = document.getElementById("daddr").value
        gdir.load("from: "+saddr+" to: "+daddr);
      }

// function centers the map on the adddress in the text field
	  function centerAddress(address) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
			  startPoint=point;
              map.setCenter(point, 11);
            }
          }
        );
      }
    }

// ===== function shows the address or location from the search box ===== 
	  function showAddress(address) {
		  //alert("start showaddress");
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
			  startPoint=point;
              map.setCenter(point, 11);
			  //alert("Number of records: "+branchData.length);
			  // begin sorting
			  for (var i = 0; i < branchData.length; i++) {
				//alert("i: "+i+"/"+branchData.length+ " next is: "+ branchData[1][0]+ " then is: "+ branchData[2][0]);
				var point = branchData[i][0];
				//alert("i: "+i+"/"+branchData.length+" point"+point);
				var dist = point.distanceFrom(startPoint)*0.000621371192;//get distance from search address // convert dist to miles
				
				branchData[i][4]=dist;
				//alert("Dist: "+branchData[i][4]);
				//alert(branchData[i].Label+" with a dist of "+branchData[i].Dist);
		 	  }
			  branchData.sort(sortByDist);
			  for (var i = 0; i < 5; i++) {
			  	branches_html += '<tr><td colspan=3><hr></td></tr>';
				//branches_html += '<tr valign="top"><td><b><a href="javascript:myclick(' + branchData[i].MarkerIndex + ')">';
				branches_html += '<tr valign="top"><td><b><a href="javascript:myclick(' + branchData[i][6] + ')">';
				//branches_html += branchData[i].Label;
				branches_html += branchData[i][1];
				branches_html += '</a></b><br>'
				branches_html += branchData[i][2];
				//branches_html += branchData[i].HTML;
				branches_html += '</td><td>'
				//branches_html += branchData[i].ServHours;
				branches_html += branchData[i][5];
				branches_html += '</td><td>'
				//var dist = branchData[i].Dist;
				var dist = branchData[i][4];
				dist = dist.toFixed(2);
				branches_html += dist + ' miles';
				branches_html += '</td></tr>';
				
			  }
			  branches_html += '</table>';
			  //myclick(branchData[0].MarkerIndex);
			  myclick(branchData[0][6]);
			  // put the assembled side_bar_html contents into the side_bar div
			  document.getElementById("side_bar").innerHTML = branches_html;
			  // end of sort
            }
          }
        );
      }
    }
	
// ===== A function to create the marker and set up the event window ===== 
      function createMarker(point,name,html,state) {
		//var marker = new GMarker(point,icon2);
		var marker = new GMarker(point);
		/****** THIS VERSION FOR DIRECTIONS ON OUR OWN PAGE***************
		// The info window version with the "to here" form open
        to_htmls[i] = html + '<br>Get 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=100 name="saddr" id="saddr" value="" /><br>' +
           '<INPUT value="Get Directions" TYPE="SUBMIT">' +
           '<input type="hidden" id="daddr" value="'+name+"@"+ point.lat() + ',' + point.lng() + 
           '"/>';
        // The info window version with the "to here" form open
        from_htmls[i] = html + '<br>Get 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=100 name="daddr" id="daddr" value="" /><br>' +
           '<INPUT value="Get Directions" TYPE="SUBMIT">' +
           '<input type="hidden" id="saddr" value="'+name+"@"+ point.lat() + ',' + point.lng() +
           '"/>';
		*********************************************************************/
		
		// The info window version with the "to here" form open (The Directions Form part.)
        to_htmls[i] = html + '<br><br>Directions: <b>To here</b> - <a href="javascript:fromhere(' + i + ')">From here</a>' +
           '<br>Start address:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
           '<input type="text" size=40 maxlength=60 name="saddr" id="saddr" value="" /><br>' +
           '<input value="Get Directions" TYPE="submit">' +
           '<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() +
                  // "(" + name + ")" +
           '"/>';
        // The info window version with the "to here" form open
        from_htmls[i] = html + '<br><br>Directions: <a href="javascript:tohere(' + i + ')">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=60 name="daddr" id="daddr" value="" /><br>' +
           '<input value="Get Directions" type="SUBMIT">' +
           '<input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() +
                  // "(" + name + ")" +
           '"/>';
		   
		
        // The inactive version of the direction info
        html = html + '<br><br>Directions: <a href="javascript:tohere('+i+')">To here</a> - <a href="javascript:fromhere('+i+')">From here</a>';
		
        GEvent.addListener(marker, "click", function() {
		  //document.getElementById("details").innerHTML = htmldetails;
          marker.openInfoWindowHtml(html);
        });
        // save the info we need to use later for the side_bar
        gmarkers[i] = marker;
        htmls[i] = html;

        i++;
        return marker;
      }
	
	
// ===== sort the array by distance ===== 
	  function sortByDist(a,b){
		var x= a[4];
		var y= b[4];
		return((x<y)?-1:((x>y)?1:0));
	  }
	  
// ===== convert meters to miles ===== 
	  function metersToMiles(a){
		var x= a*0.000621371192;
		x = x.toFixed(2);
		return(x);
	  }
  
// ===== add data to data array ===== 
	  function branch(point, label, html, state, dist, servhours, markerindex){
	  	this.Point=point;
		this.Label=label;
		this.HTML=html;
		this.State=state;
		this.Dist=dist;
		this.ServHours=servhours;
		this.MarkerIndex=markerindex;
	  }

