$(document).ready(function(){

	$('[name=texsubmit]').attr("disabled", "true");
	$('[name=location]').val("Enter your starting point address here.");
	var panel = document.getElementById("map_canvas");
	var map;
	var marker;
	var dirdisplay;
	var destination = "2797 Kimmel St. Stevensville, MI 49127";
	var origin;
	var dsoptions;
	var options;
	var geocoder = new google.maps.Geocoder();
	var dirservice = new google.maps.DirectionsService();
	var infowindow;
	geocoder.geocode({address: destination}, function(results, status){
	if(status == google.maps.GeocoderStatus.OK){
		destination = results[0].geometry.location;
		options = {
		'backgroundColor': '#f2efe9',
		'zoom': 8,
		'mapTypeId': google.maps.MapTypeId.ROADMAP,
		'center': destination,
		'scrollwheel': false
		};
		map = new google.maps.Map(document.getElementById("map_canvas"), options);
		marker = new google.maps.Marker({
		'map': map, 
		'title': 'Accomplish Quilting Inc.',
		'position': destination
		});
	} else {
		alert("Error retrieving location.");
	}
	});
$("input:button[name=locsubmit]").button();
$("input:button[name=texsubmit]").button();
$("input:button[name=texsubmit]").button("disable");
$("[name=locsubmit]").click(function() {
	if(infowindow != null) infowindow.close();
	origin = $('[name=location]').val();
	geocoder.geocode({address: origin}, function(results, status){
	if(status == google.maps.GeocoderStatus.OK){
		origin = results[0].geometry.location;
		dsoptions = {
			'origin': origin, 
			'destination': destination,
			'travelMode': google.maps.DirectionsTravelMode.DRIVING
		};
		dirservice.route(dsoptions, function(result, status){
		dirdisplay = new google.maps.DirectionsRenderer({ 
			'directions': result,
			'map': map
		});
		$("input:button[name=texsubmit]").button("enable");
		});
	}else{
		infowindow = new google.maps.InfoWindow({
		'content': 'Google did not return any matches for that location.'
		});
		infowindow.open(map, marker);
		$("input:button[name=texsubmit]").button("disable");
	}
	});
});
$("[name=location]").focusin(function() {
	if($('[name=location]').val() == "Enter your starting point address here."){
		$('[name=location]').val("");
	}
});
$("[name=location]").focusout(function() {
	if($('[name=location]').val() == ""){
		$('[name=location]').val("Enter your starting point address here.");
	}
});
$("[name=location]").keyup(function(e){
	if(e.keyCode == 13) {
	$("[name=locsubmit]").click();
	}
});
$("[name=texsubmit]").click(function(){
	if($("[name=texsubmit]").attr("value") == 'Show Text Directions'){
		$("[name=texsubmit]").attr("value", "See Visual Map");
		$("div#map_canvas").text("");
		$("div#map_canvas").css("overflow", "scroll");
		$("div#map_canvas").css("background", "#FFFFFF");
		dirservice.route(dsoptions, function(result, status){
			dirdisplay = new google.maps.DirectionsRenderer({ 
				'directions': result,
				'panel': panel
			});
		});
	} else if($("[name=texsubmit]").attr("value") == 'See Visual Map') {
		$("[name=texsubmit]").attr("value", "Show Text Directions");
		$("div#map_canvas").text("");
		$("div#map_canvas").css("overflow", "hidden");
		dirservice.route(dsoptions, function(result, status){
			map = new google.maps.Map(document.getElementById("map_canvas"), options);
			dirdisplay = new google.maps.DirectionsRenderer({ 
				'directions': result,
				'map': map
			});
		});
	}
});

});