﻿//<![CDATA[
function GoogleMap(mapClientId, tabModuleId, lattitude, longitude, dbAddress, dbCity, dbState, dbZip){
	this.addressCtrl = "address" + tabModuleId;
	this.gmarkers = [];
	this.htmls = [];
	this.to_htmls = [];
	this.from_htmls = [];
	this.i = 0;
	this.load = function(){
		if(GBrowserIsCompatible()){
			var mapObj = document.getElementById(mapClientId);
			if(mapObj != null){
				var map = new GMap2(mapObj);
				var html = '<p style="font-family:Arial;font-size:12px;">' + dbAddress + '<br />' + dbCity + ', ' + dbState + ' ' + dbZip;
				map.addControl(new GSmallMapControl());
				map.addControl(new GMapTypeControl());

				var point = new GLatLng(lattitude, longitude);
				var marker;
				if(point != null){
					marker = this.createMarker(point, html);
				}
				map.setCenter(point, 13);
				map.addOverlay(marker);
				this.gmarkers[0].openInfoWindowHtml(this.to_htmls[0]);
			}else{
				alert("The map container object named '" + mapClientId + "' cannot be found.");
			}
		}
	}
	// A function to create the marker and set up the event window
	this.createMarker = function(point, html){
		var marker = new GMarker(point);
		var spacer = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		// The info window version with the "to here" form open
		this.to_htmls[this.i] = html + '<br />Directions: <b>To here</b> - ' +
			'<a href = "javascript:fromhere(' + this.i + ')">From here</a><br>Start address:' + 
			'<input style = "font-family:Arial;" type="text" SIZE=20 MAXLENGTH=40 name="' + this.addressCtrl + '" id="' + this.addressCtrl + '" value="" /><br />' +
			spacer + '<INPUT style = "font-family:Arial;font-size:10px;" value="Get Directions" TYPE="SUBMIT" onclick="getDirections(0)">'
		// The info window version with the "to here" form open
		this.from_htmls[this.i] = html + '<br />Directions: <a href = "javascript:tohere(' + this.i + ')">To here</a>' +
			' - <b>From here</b><br>End address:<input style="font-family:Arial;" type="text" SIZE=20 MAXLENGTH=40 name="' + this.addressCtrl + '" id="' + this.addressCtrl + '" value="" />' + 
			'<br />' + spacer + '<INPUT style="font-family:Arial;font-size:10px;" value="Get Directions" TYPE="SUBMIT" onclick="getDirections(1)">'
		// The inactive version of the direction info
		this.html = html + '<br />Directions: <a href="javascript:tohere(' + this.i + ')">To here</a> - <a href="javascript:fromhere(' + this.i + ')">From here</a>';
		GEvent.addListener(marker, "click", function(){
			marker.openInfoWindowHtml(html);
		});
		this.gmarkers[this.i] = marker;
		this.htmls[this.i] = html;
		this.i++;
		return marker;
	}
	this.getDirections = function(toOrFrom){
		var address = document.getElementById(this.addressCtrl).value;
		var url;
		if(toOrFrom == 1){
			url = 'http://maps.google.com/maps?daddr=' + address + '&saddr=' + dbAddress + ',' + dbCity + ', ' + dbState + ' ' + dbZip;
		}
		if(toOrFrom == 0){
			url = 'http://maps.google.com/maps?saddr=' + address + '&daddr=' + dbAddress + ',' + dbCity + ', ' + dbState + ' ' + dbZip;
		}
		openMe(url);
	}
	this.openMe = function(url){
		window.open(url, 'directions', 'top=100,left=100,width=770,height=600,status=1,resizable=1');
	}
	// functions that open the directions forms
	this.tohere = function(i){
		gmarkers[this.i].openInfoWindowHtml(to_htmls[this.i]);
	}
	this.fromhere = function(i){
		gmarkers[this.i].openInfoWindowHtml(from_htmls[this.i]);
	}
}
//]]>
