/**
 * Have a Default Point (see f(x) initialize for this one.)
 */
var Point_ = null;
/**
 * Create / Return GMap2
 */
var GMap2_ = null;
function getGMap2_(){
	if(!GMap2_){
		GMap2_ = new GMap2(document.getElementById("map_canvas"));
	}
	return GMap2_;
}
/**
 * Create / Return GClientGeocoder
 */
var GClientGeocoder_ = null;
function getGClientGeocoder_(){
	if(!GClientGeocoder_){
		GClientGeocoder_ = new GClientGeocoder();
	}
	return GClientGeocoder_;
}
/**
 * Initialize to default_state e.g. `PA`
 */
function initialize() {
	if (GBrowserIsCompatible()){
    	getGMap2_();
    	getGClientGeocoder_();
    	/**
    	 * Clean Map
    	 */
    	GMap2_.clearOverlays();



    	/**
    	 * Default Map Center
    	 */
    	GClientGeocoder_.getLatLng(
			"PA,US",
	    	function(point){
	    		if(point){
	    			/**
			    	 * Do we have a lat/lon available?
			    	 */
			    	if($('lat').value.length&&$('lon').value.length){
			    	point = new GLatLng($('lat').value,$('lon').value);
			    	GMap2_.setCenter(point, 10);
			    	}else{
			    	GMap2_.setCenter(point, 6);
			    	}
	    			//


	    			GMap2_.addOverlay(new GMarker(point));
	    			/**
	    			 * Default Point
	    			 */
	    			Point_ = point;
	    		}
	    	}
	    );
	}
}
/**
 * SetMap
 */
function setMap(address1,address2,city,state,zip,country){
	if (GBrowserIsCompatible()){
		/**
		 * Rationalize address data
		 */
		address = "";
		if(address1.length){address = address+address1}
		if(address2.length){if(address.length){address = address+","+address2}else{address = address+address2}}
		if(city.length){if(address.length){address = address+","+city}else{address = address+city}}
		if(state.length){if(address.length){address = address+","+state}else{address = address+state}}
		if(zip.length){if(address.length){address = address+","+zip}else{address = address+zip}}
		if(country.length){if(address.length){address = address+","+country}else{address = address+country}}
		//
    	getGMap2_();
    	getGClientGeocoder_();
    	/**
    	 * Clean Map
    	 */
    	GMap2_.clearOverlays();
    	//
    	GClientGeocoder_.getLatLng(
			address,
	    	function(point){
	    		if(point){
	    			GMap2_.setCenter(point, 10);
	
					var marker = new GMarker(point)
	    			GMap2_.addOverlay(marker);
					//marker.openInfoWindowHtml("<h4>" + city + "</h4>");
					/*
					GEvent.addListener(marker, 'click', function() {
					marker.openInfoWindowHtml(html);
					});
					*/
	
		            /*var openmarker = createMarker(openpoint,openhtml)
		            map.addOverlay(openmarker);*/
	
	    			/**
	    			 * The document should have:
	    			 * $("lat")
	    			 * $("lon")
	    			 *
	    			 * Update if we have them
	    			 */
					var lat = $("lat");
					var lon = $("lon");
					
					if ($chk(lat)) lat.value = point.lat();
	    			if ($chk(lon)) lon.value = point.lng();
	    			//
	    			return true;
	    		}else{
	    			/**
	    			 * The document should have:
	    			 * $("lat")
	    			 * $("lon")
	    			 *
	    			 * Blank
	    			 */
	    			$("lat").value = "";
	    			$("lon").value = "";
	    			//
		    		initialize();
		    		//
	    			return false;
	    		}
	    	}
	    );
	}
}
