//-----------------------------------------------------------------------------------------------------------
function JSMap(control_id, form_id, lat_elem_id, lon_elem_id, zoom){
	this.control_id = control_id;
	this.form_id = form_id;
	this.lat_elem_id = lat_elem_id;
	this.lon_elem_id = lon_elem_id;
	this.control = null;
	this.form = null;
	this.lat_elem = null;
	this.lon_elem = null;
	this.map = null;
	this.zoom = Number(zoom);
	this.large_map_control = null;
	this.marker = null;
	this.map_type_control = null;
	this.scale_control = null;
	this.initForm();
	this.init();
}
//-----------------------------------------------------------------------------------------------------------
/* set prototype */
JSMapPrototype = JSMap.prototype;
//-----------------------------------------------------------------------------------------------------------
JSMapPrototype.init = function() {
	this.control = layer(this.control_id);
	this.map = new GMap2(this.control.object);
	this.large_map_control = new GLargeMapControl();
	this.map_type_control = new GMapTypeControl();
	this.scale_control = new GScaleControl();
	this.map.addControl(this.large_map_control);
	this.map.addControl(this.map_type_control);
	this.map.addControl(this.scale_control);
	this.map.setCenter(new GLatLng(this.getLat(), this.getLon()), this.zoom);
	var point = new GLatLng(46.568302354495, -91.378784179688);
	this.map.jsmap = this;
	this.marker = new GMarker(point);
	this.map.addOverlay(this.marker);
	GEvent.addListener(this.map, "click", this.clickListener);
	//this.map.marker = new GMarker(point);
}
//-----------------------------------------------------------------------------------------------------------
JSMapPrototype.clickListener = function(marker, point) {
	if (!marker) {
		if (this.jsmap.marker) this.removeOverlay(this.jsmap.marker);
		this.jsmap.marker = new GMarker(point);
		this.jsmap.setLat(point.lat());
		this.jsmap.setLon(point.lng());
		this.addOverlay(this.jsmap.marker);
	}
}
//-----------------------------------------------------------------------------------------------------------
JSMapPrototype.initForm = function() {
	this.form = layerByPath(this.getFormPath(this.form_id));
	this.lat_elem = layerByPath(this.getFormElemPath(this.form_id, this.lat_elem_id, false));
	this.lon_elem = layerByPath(this.getFormElemPath(this.form_id, this.lon_elem_id, false));
}
//-----------------------------------------------------------------------------------------------------------
JSMapPrototype.getFormPath = function(form_name) {
	return "document.forms['" + form_name + "']";	
}

JSMapPrototype.getFormElemPath = function(form_name, elem_name, index) {
	var path 		= this.getFormPath(form_name);
	var subpath		= (index !== false) ? '[]' : '';
	path 			+= ".elements['" + elem_name + subpath + "']";
	return path;
}
//-----------------------------------------------------------------------------------------------------------
JSMapPrototype.getLat = function() {
	if (this.lat_elem.object) {
		var lat = this.lat_elem.object.value;
		if (lat) return Number(lat);
		else return 0;
	} else return 0;	
}
//-----------------------------------------------------------------------------------------------------------
JSMapPrototype.getLon = function() {
	if (this.lat_elem.object) {
		var lon = this.lon_elem.object.value;
		if (lon) return Number(lon);
		else return 0;
	} else return 0;	
}
//-----------------------------------------------------------------------------------------------------------
JSMapPrototype.setLat = function(value) {
	if (this.lat_elem.object) {
		this.lat_elem.object.value = String(value);
	}
}
//-----------------------------------------------------------------------------------------------------------
JSMapPrototype.setLon = function(value) {
	if (this.lon_elem.object) {
		this.lon_elem.object.value = String(value);
	}
}
//-----------------------------------------------------------------------------------------------------------
