// GOOGLE MAP WRAPPER CLASS
function GMapWrap(){
	
	this.geocoder = null;	// THE GEOCODER
	this.map = null;		// THE GMAP ITSELF
	this.map_div = null;	// GMAP WRAPPER DIV
	this.loc_form = null;	// LOCATION FORM, WHEN NECESSARY
	
	this.isCompatible = function(){ 
		return GBrowserIsCompatible(); 
	};
	
	this.setup = function(map_div_id){
		// DISPLAY THE MAP, WITH SOME CONTROLS AND SET THE INITIAL LOCATION 
		this.map_div = document.getElementById(map_div_id);
		this.map = new GMap2(this.map_div);
		
		this.geocoder = new GClientGeocoder();

		window.addEvent("unload", GUnload);
	};
	
	this.addPoint = function(html, lat, lng, zoom){	
		var point = new GLatLng(lat, lng);
		var marker = this.createMarker(point, html);
	    
		this.map.setCenter(point);
		this.map.addOverlay(marker);
	};
	
	this.createMarker = function(point,html) {
		var marker = new GMarker(point);
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(html);
		});
		
		return marker;
	};

	this.geocode = function(form_id){
		this.loc_form = $(form_id);
		
		var address = this.compileAddress(this.loc_form);
			
		this.geocoder.getLatLng(address, function(point){gMap.setLatLng(point);});
	};
	
	this.setLatLng = function(point){
		if(!point){
			alert('Error Geocoding Address!');
		}
		
		else{
			var lat = point.lat();
			var lng = point.lng();
								
			gMap.loc_form.lat.value = lat;		
			gMap.loc_form.lng.value = lng;		
			
			gMap.loc_form.submit();
		}
	};
	
	this.compileAddress = function(loc_form){
		var street 		= loc_form.address.value;
		var city 		= loc_form.city.value;
		var territory 	= loc_form.territory.value;
		var zip 		= loc_form.zip_code.value;
	
		var address = street + ' ' + city  + ' ' + territory  + ' ' + zip;	
		return address;
	};
}
