$(document).ready(function() {


if(document.getElementById("map")){
      if (GBrowserIsCompatible() && $("#map") ) {

      // Display the map, with some controls and set the initial location
        var map = new GMap2(document.getElementById("map"));
	      	//map.addControl(new GSmallMapControl());
			map.addControl(new GSmallMapControl());
	      	map.addControl(new GMapTypeControl());
	        map.setCenter(new GLatLng(2, -2), 1);
      		//map.setCenter(new GLatLng(45.563938, -122.638141), 9);

			map.enableScrollWheelZoom()

			//prevent page scrolling when on the map
            function wheelevent(e){
				if (!e){
					e = window.event
				}
				if (e.preventDefault){
					e.preventDefault()
				}
				e.returnValue = false;
			}
			GEvent.addDomListener(map.getContainer(), "DOMMouseScroll", wheelevent);
			map.getContainer().onmousewheel = wheelevent;

/*
	  // ==================================================
      // ==================================================  http://econym.googlepages.com/example_map10.htm

      // A function to create a tabbed marker and set up the event window
      function createTabbedMarker(point,html1,html2,label1,label2) {
        var marker = new GMarker(point);
        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowTabsHtml([new GInfoWindowTab(label1,html1), new GInfoWindowTab(label2,html2)]);
        });
        return marker;
      }


      // ==================================================
      // Set up the tabbed markers

      var point = new GLatLng(43.82589,-79.10040);
      var marker = createTabbedMarker(point, "This is the contents of the first tab<br> of marker zero", "Another page<br> of marker zero contents", "Truth", "Beauty");
      map.addOverlay(marker);

      var point = new GLatLng(43.65654,-79.90138);
      var marker = createTabbedMarker(point, "is the time for all good men<br>to come to the aid of the party", "a dangerous temptation comes to us<br>in gay, fine colours", "Now", "Many");
      map.addOverlay(marker);
	  */
	}
  else {
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }// if GBrowser



} //if


		$(".check").click(function(){
			var currentAddress = $("#address1").val();
			checkAddy(currentAddress);
			return false;
			$(".check").html("nice");
		});//if it's not empty

/*
    	$("#address1").blur( function (){
			//if ($("#address1").val().indexOf > 8){
				var currentAddress = $("#address1").val();
				checkAddy(currentAddress);
			//}
		}); // .check click or address blur
*/


function checkAddy(address){

		var addy = address;
		//console.log(addy);
		//http://maps.google.com/maps/geo?q=1600+Amphitheatre+Parkway,+Mountain+View,+CA&output=xml&key=abcdefg

		var geocoder = new GClientGeocoder();
		showAddress(addy)

		function showAddress(address) {
		  geocoder.getLatLng(
		    address,
		    function(point) {
		      if (!point) {
		        //alert("The Address" + address + " not found.  \nTry adding or removing some detail.");
				$(".check").html("Can't find it. Please try again.").addClass("cantfindit").removeClass("foundit");

		      } else {
/* */
			  	map.setCenter(point, 13);
		        var marker = new GMarker(point);
		        map.addOverlay(marker);
		        marker.openInfoWindowHtml(address);

				$("#geotag").val(point);
				$("#address1").addClass("success").removeClass("error");
				$("button.check").html("found it!").addClass("foundit").removeClass("cantfindit");
				$("label.maperror").remove();
		      }
		    }
		  );
		}


		return false
	};

	function mapbrowse(address){

			var addy = address;
			//console.log(addy);
			//http://maps.google.com/maps/geo?q=1600+Amphitheatre+Parkway,+Mountain+View,+CA&output=xml&key=abcdefg

			var geocoder = new GClientGeocoder();
			showAddress(addy)

			function showAddress(address) {
			  geocoder.getLatLng(
			    address,
			    function(point) {
			      if (!point) {
			        //alert("The Address" + address + " not found.  \nTry adding or removing some detail.");
					$(".check").html("Can't find it. Please try again.").addClass("cantfindit").removeClass("foundit");

			      } else {
	/* */
				  	map.setCenter(point, 13);
			        var marker = new GMarker(point);
			        map.addOverlay(marker);
			        marker.openInfoWindowHtml(address);

					$("#geotag").val(point);
					$("#address1").addClass("success").removeClass("error");
					$("button.check").html("found it!").addClass("foundit").removeClass("cantfindit");
					$("label.maperror").remove();
			      }
			    }
			  );
			}


			return false
		};

//make the time input pretty
/*
if($("#time").length > 0){
	$("#time").timeEntry();
}

*/ 
/*
$('#addressSearch').click(function(){
	$('#map').searchAddress({
		address: $('#fromAddress').val(),
		cache: $.jmap.GGeoCache
	});
});
*/

});// document ready / end jquery functions



