
/* code for using the Google API */
var geocoder = new google.maps.Geocoder();
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var marker = new google.maps.Marker();  // = new google.maps.Marker();
var infowindow = new google.maps.InfoWindow();  // = new google.maps.InfoWindow();

/* show a large map over the US */
function initialize() { 
    directionsDisplay = new google.maps.DirectionsRenderer(); 
    var latlng = new google.maps.LatLng(38.929047, -94.682199); 
    var myOptions = { 
        zoom: 3, 
        center: latlng, 
        mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    directionsDisplay.setPanel(document.getElementById("directionsPanel"));
	
	showShop();
} 


/* calculate the route */
function calcRoute(inStart, inEnd) {
	//alert("Starting calcRoute\nInStart is: " + inStart + "\ninEnd is: " + inEnd)
    var start = inStart;
    var end = inEnd;
    var request = {
        origin:start, 
        destination:end,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
    directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
		directionsDisplay.setMap(map);
        directionsDisplay.setDirections(response);
		document.getElementById("directionsPanel").style.height = '150px';
      }
    });
}

/* get the addresses */
function codeAddress() { 
	
	directionsDisplay.setMap(null);
    
	var jsAddressTo = trimString(document.getElementById('toMapAddress').value) + ' ';
    jsAddressTo += trimString(document.getElementById('toMapCity').value) + ' ';
    jsAddressTo += trimString(document.getElementById('toMapState').value) + ' ';
    jsAddressTo += trimString(document.getElementById('toMapZip').value);
	//alert("The addressTo is: " + jsAddressTo);
	
	var jsAddressFrom = trimString(document.getElementById('fromMapAddress').value) + ' ';
    jsAddressFrom += trimString(document.getElementById('fromMapCity').value) + ' ';
    jsAddressFrom += trimString(document.getElementById('fromMapState').value) + ' ';
    jsAddressFrom += trimString(document.getElementById('fromMapZip').value);
	// alert("the addressFrom is: " + jsAddressFrom);
	
	if (trimString(jsAddressFrom).length > 0) {
		//alert("the length is: " + trimString(jsAddressFrom).length);
		infowindow.close(map,marker);
		marker.setMap(null);
        calcRoute(jsAddressFrom, jsAddressTo);
    } else {
        showShop();
    }
}

/* clean up the input data */
function trimString (str) {
	str = str + ' ';
	str = str.replace(/^\s+/, '');
	for (var i = str.length - 1; i >= 0; i--) {
		if (/\S/.test(str.charAt(i))) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return str;
}

/* put a marker on the map based on address */
function showShop() {
	//alert("starting showShop");
	document.getElementById("directionsPanel").style.height = '0px';

	var latlng = new google.maps.LatLng(document.getElementById('latitude').value, document.getElementById('longitude').value);
	map.setCenter(latlng);
	map.setZoom(12);
	marker.setPosition(latlng);
	marker.setMap(map);
	  
	var message = "<div style='font-size:12px'><b>" + trimString(document.getElementById('toMapName').value) + "</b><br><br>";
	message += trimString(document.getElementById('toMapAddress').value) + "<br>" ;
	message += trimString(document.getElementById('toMapCity').value) + ', ';
	message += trimString(document.getElementById('toMapState').value)+ '  ';
	message += trimString(document.getElementById('toMapZip').value) + "</div>";
	infowindow.setContent(message);
	
	/* open the window on each click */
	infowindow.open(map,marker);
	
	/* open the info window on each mouseover */
	google.maps.event.addListener(marker, 'mouseover', function() {
		infowindow.open(map,marker);
	});

}
