// JavaScript Document

function MultimapInlineMap(client) {
	/*	-----------------
		Attributes
	-----------------	*/
	this.client = client;
	this.cgiurl = "http://www.multimap.com/clients/gif.cgi";
	this.baseurl = this.cgiurl+"?client="+this.client;
	this.panx = 0;
	this.pany = 0;
	this.ds = 0;
	this.width = 500; // Default width
	this.height = 300; // Default height
//	this.noautoredraw = 0;
	this.debug = 0; // Disable debug output

	/*	-----------------
		Methods
	-----------------	*/
	// Panning controls.
	this.pan = function pan(xinc, yinc) {
		this.panx = this.panx + xinc;
		this.pany = this.pany + yinc;
		this.redrawMap();
	}
	// Pan on the X axis
	this.panX = function panX(inc) { this.pan(inc, 0); }
	// Pan on the Y axis
	this.panY = function panY(inc) { this.pan(0, inc); }

	// Zooming/scale functions.
	this._getScaleIndex = function getScaleIndex() {
		if (this.scale && !this.scales) {
			this.flog("Scale value specified, but no scale range is defined - using defaults","D");
			this.scales = new Array(10000, 25000, 50000, 100000, 200000, 500000);
		}
		for (n=0; n<this.scales.length; n++) {
			if (this.scales[n] == this.scale) {
				return n;
			}
		}
	}
	this.zoom = function zoom(inc) {
		if (this.scale) {
			// We have a fixed range of scales, move up and down the scales array
			this._scaleindex = this._getScaleIndex() + inc;
			if (this._scaleindex < 0 || this._scaleindex >= this.scales.length) {
				this.flog("Cannot zoom beyond current range of scales","D");
				return false;
			}
			this.scale = this.scales[this._scaleindex];
		} else {
			// Use delta scaling
			this.ds = this.ds + inc;
		}
		this.redrawMap();
	}
	this.zoomIn = function zoomIn() { this.zoom(-1); }
	this.zoomOut = function zoomOut() { this.zoom(1); }
	this.gotoScale= function gotoScale(scale) {
		this.scale = scale;
		this._scaleindex = this._getScaleIndex();
		this.redrawMap();
	}
	
	// Generate a full url for this map request.
	this.buildUrl = function buildUrl() {
		
		// Location of map
		if (this.query_string && this.debug) {
			
			var valid_params = new Array();
			valid_params["client"]=1;
			valid_params["lat"]=1;
			valid_params["lon"]=1;
			valid_params["f_client_id"]=1;
			valid_params["filter_params"]=1;
			valid_params["x"]=1;
			valid_params["y"]=1;
			valid_params["pc"]=1;
			valid_params["coordsys"]=1;
			valid_params["scale"]=1;
			valid_params["scales"]=1;
			valid_params["width"]=1;
			valid_params["height"]=1;
			valid_params["xhtml"]=1;
			valid_params["debug"]=1;
			valid_params["icon"]=1;
			valid_params["query_string"]=1;
			var param = new Array();
			var query = this.query_string;
			var param_arr = query.split('&');
			var filter_params_arr = new Array();
			for (var i=0; i<param_arr.length; i++) {
				var pos = param_arr[i].indexOf('=');
				if (pos > 0) {
					var key = param_arr[i].substring(0,pos);
					var val = param_arr[i].substring(pos+1);
					if (key.match(/^f_/)) {
						filter_params_arr.push(key+'='+val);
					} else {
						if (valid_params[key] != 1) {
							this.flog(key + " is not a valid paramaeter","D");
						}
						param[key] = val;
					}
				}
			}
			if (param["client"]) { // If client is passed in through the query_string
				this.client = param["client"];
			}
			this.lat = param["lat"];
			this.lon = param["lon"];
			this.x = param["x"];
			this.y = param["y"];
			this.coordsys = param["coordsys"];
			this.pc = param["pc"];
			this.xhtml = param["xhtml"];
			this.width = param["width"];
			this.height = param["height"];
			this.scale = param["scale"];
			this.scales = param["scales"];
			this.icon = param["icon"];
			this.f_client_id = param["f_client_id"];
			this.filter_params = filter_params_arr;
			
		} else if (this.query_string) {
			
			var newUrl;
			if (this.client) {
				// I client is specified in the construstor
				newUrl = this.cgiurl+"?client="+this.client+"&"+this.query_string;
			} else {
				// I client is within the query_string
				newUrl = this.cgiurl+"?"+this.query_string;
			}
			// Panning values
			panning = "&panx="+this.panx+"&pany="+this.pany;
			// Scale values
			scale = (this.scale) ? "&scale="+this.scale : "";
			zooming = (this.ds) ? "&ds="+this.ds : "";
			
			icon = (this.icon) ? "&icon="+this.icon : "";

			newUrl = newUrl+scale+panning+zooming+icon;
			return newUrl;
		}
		
				
		if (this.lat && this.lon) {
			
			maplocation = "&lat="+this.lat+"&lon="+this.lon;
			
		} else if (this.x && this.y) {
			
			// Check if we have a coordsys, if not assume OSNG coords.
			if (this.coordsys == "") {
				this.coordsys = "gb";
			}
			maplocation = "&X="+this.x+"&Y="+this.y+"&coordsys="+this.coordsys;
			
		} else if (this.pc) {
			
			// GB Postcode
			maplocation = "&pc="+this.pc;
			
//		} else if (this.filter_params.length > 0) {
//			alert('4');
//			
//			// Multiple Filters
//			maplocation = "&" + this.filter_params.join("&");
//			
		} else if (this.f_client_id) {
			
			// f_client_id
			maplocation = "&f_client_id="+this.f_client_id;
			
		} else {
			
			// No coordinates
			this.flog("Request has no coordiantes","D");

		}
				
		// Map size (pixel dimensions)
		size = "&width="+this.width+"&height="+this.height;
		// Panning values
		panning = "&panx="+this.panx+"&pany="+this.pany;
		// Scale values
		scale = (this.scale) ? "&scale="+this.scale : "";
		zooming = (this.ds) ? "&ds="+this.ds : "";
		
		icon = (this.icon) ? "&icon="+this.icon : "";
		//icon = "&icon=sun_logo";
				
		var newUrl = this.cgiurl+"?client="+this.client+maplocation+scale+size+panning+zooming+icon;
		return newUrl;
	}
	// Draw Map
	this.drawMap = function drawMap() {
		this.fullUrl = this.buildUrl();
		var endtag = (this.xhtml == 'true') ? " />" : ">";
		document.write("<img src='"+this.fullUrl+"' id='multimap' alt='map'"+endtag);
	}
	// Refresh map view
	this.redrawMap = function RedrawMap() {
		this.fullUrl = this.buildUrl();
		// Update image with new map url.
		document.getElementById('multimap').src = this.fullUrl;
	}
	
	// Debug alerts, only display if this.debug returns true.
	this.flog = function flog(msg, type) {
		if (type == "D" && !(this.debug)) {
			return;
		} else {
			alert(msg);
		}
	}
}
