// JavaScript Document

function gSinglePointMap( mapDiv, lat, lng, addy ) {

/* fields */
	this.map = null;
	this.marker = null;
	this.address = addy;
	

/* Method Headers */
	this.getDirections = getDirections;
/* begin __construct */

	if (GBrowserIsCompatible()) {
	
		var c = new GLatLng(lat,lng);

		this.map = new GMap2(document.getElementById(mapDiv));
		this.map.setCenter(c, 13);
		
		
		
		this.map.addControl(new GLargeMapControl ());
		this.map.addControl(new GScaleControl());
		 
		this.map.addControl(new GMenuMapTypeControl());
		
		// Add the draggable marker
		this.marker = new GMarker(c );
		this.marker.mapHandler = this;
		
		this.map.addOverlay(this.marker);
	
	
	} else {
		alert("Sorry, but your browser is not compatible with Google Maps. This admin area will not work.");
		return null;
	}
	/* end __construct */

	
/* Methods */
	function getDirections(sourceBox) {
	
		var d = document.getElementById(sourceBox);
		
		window.open('http://maps.google.com?saddr=' + d.value + '&daddr=' + this.address);
	}

}

function gLocationPicker( mapDiv, latControl, lngControl, addressControl ) {

	/* fields */
	this.map = null;
	this.marker = null;
	this.latCtrl = null;
	this.lngCtrl = null;
	this.addyCtrl = null;

	
	/* method headers */
	this.updateMarkerTracking = updateMarkerTracking;
	this.mark = mark;
	this.lookupAddress = lookupAddress;
	this.showAddress = showAddress;
	this.getDirections = getDirections;

	
	
	/* begin __construct */
	this.latCtrl = document.getElementById(latControl);
	this.lngCtrl = document.getElementById(lngControl);
	this.addyCtrl = document.getElementById(addressControl);


	var c; // centre
	
	if ( this.latCtrl.value != "" && this.lngCtrl.value != "" ) {

		c = new GLatLng(this.latCtrl.value, this.lngCtrl.value);
	} else  {
		c = new GLatLng(43.46772299813115, -80.52949547767639);
	}
	
      if (GBrowserIsCompatible()) {
		
			

       	this.map = new GMap2(document.getElementById(mapDiv));
		// Waterloo Park
        this.map.setCenter(c, 13);
		
      	this.map.addControl(new GLargeMapControl ());
      	this.map.addControl(new GScaleControl());
		 
		this.map.addControl(new GMenuMapTypeControl());
		
		// Add the draggable marker
		this.marker = new GMarker(c, {draggable: true});
		this.marker.mapHandler = this;
		
		GEvent.addListener(this.marker, "dragstart", function() {
		  this.mapHandler.map.closeInfoWindow();
		  });
		
		GEvent.addListener(this.marker, "dragend", function() {
		  this.mapHandler.updateMarkerTracking();
		  });      
		
		this.map.addOverlay(this.marker);
		this.updateMarkerTracking();


	  } else {
	  	alert("Sorry, but your browser is not compatible with Google Maps. This admin area will not work.");
		return null;
	}

	/* end __construct */
	
	
	/* methods */
	
	function updateMarkerTracking() {
		var l = this.marker.getLatLng();
		
		this.latCtrl.value = l.lat();
		this.lngCtrl.value = l.lng();
		
	}

	function mark( point ) {
		this.marker.setLatLng(point);
		this.updateMarkerTracking();

	}
	function lookupAddress() {
		alert("Looking for Address...");
		var address = this.addyCtrl.value;
		this.showAddress(address);
	}
	
	function showAddress(address) {
		var geocoder = new GClientGeocoder();
		marker = this.marker
		geocoder.getLatLng( address, function(point) { if(!point)return;
														  
														  marker.mapHandler.mark(point);
														  marker.openInfoWindow("Location Found: " + address); 
														  marker.mapHandler.map.setCenter(marker.getLatLng());
														  } );
	}
		
	function getDirections(destinationBox) {
	
		var d = this.addyCtrl;
		var s = document.getElementById(destinationBox);
		
		window.open('http://maps.google.com?daddr=' + d.value + '&saddr=' + s.value);
	}

}