//<!--
//# Written by Fernando Drabik, NOAA
//# Modified Oct 2006
//# License: GPL
//-->



// toolBox = new ToolBox(mapManager,'tools');

function Tool(name, imgOn, imgOff, toolAction, toolTip, toolBox) {
	this.toolAction = toolAction;
	this.factor = 2; //default
	this.name = name;
	this.toolTip = toolTip
	this.icon = document.createElement("img");
	this.icon.setAttribute("name", name);
	this.icon.setAttribute("title", toolTip);
	this.icon.setAttribute("on", imgOn);
	this.icon.setAttribute("off", imgOff);
	this.icon.setAttribute("src", imgOff);
	this.icon.setAttribute("toolBox", toolBox);
	this.toolBox = toolBox;
	// uses curry function defined as a global in mapmanager.js
	this.focusMe = curry(this, this.focusTool);
	this.unFocusMe = curry(this, this.unFocusTool);
	this.activateMe = curry(this, this.activateTool);
	xAddEventListener(this.icon, 'mouseover', this.focusMe , false);
	xAddEventListener(this.icon, 'mouseout', this.unFocusMe, false);
	xAddEventListener(this.icon, 'mouseup', this.activateMe, false);
}

Tool.prototype.focusTool = function(evt) {
	this.icon.setAttribute("src", this.icon.getAttribute("on")) ;
}

Tool.prototype.unFocusTool = function(evt) {
	this.icon.setAttribute("src", this.icon.getAttribute("off")) ;
}

Tool.prototype.activateTool = function(evt) {
	for(var i = 0; i < this.toolBox.tools.length; i++) {
		this.toolBox.tools[i].icon.setAttribute("src", this.toolBox.tools[i].icon.getAttribute('off'));
		xAddEventListener(toolBox.tools[i].icon, 'mouseout', toolBox.tools[i].unFocusMe, false);
	}
	xRemoveEventListener(this.icon, 'mouseout', this.unFocusMe, false);
	// Next line breaks IE
	this.icon.setAttribute("src", this.icon.getAttribute("on"));
	this.toolBox.mapManager.setActionState(this.icon.getAttribute("name"));
}

Tool.prototype.setToolFactor = function(factor) {
	this.icon.setAttribute("factor", factor);
	this.toolBox.mapManager.factor = factor;
}

Tool.prototype.getToolFactor = function(tool) {
	return this.icon.getAttribute("factor", factor);
}


function ToolBox(mapManager, targetDiv) {
	this.container = xGetElementById(targetDiv);
	this.mapManager = mapManager;
	this.tools = new Array();
	var zoomIn = new Tool('zoomin', 'images/zoomInOn.png', 'images/zoomInOff.png', zoomIn, 'Zoom In (click point or drag box)', this);
	var zoomOut = new Tool('zoomout', 'images/zoomOutOn.png', 'images/zoomOutOff.png', zoomOut, 'Zoom Out (click point)', this);
	var pan = new Tool('pan', 'images/panOn.png', 'images/panOff.png', pan, 'Pan (drag map or click to recenter)', this);
	var transect = new Tool('transect', 'images/transectOn.png', 'images/transectOff.png', transect, 'Transect Query (drag horiz/vert line)', this);
	var print = new Tool('print', 'images/printOn.png', 'images/printOff.png', print, 'Printable (create printable map image)', this);
	var data = new Tool('data', 'images/dataDownloadOn.png', 'images/dataDownloadOff.png', data, 'Download Data', this);
	this.addTool(data);
	this.addTool(print);
	this.addTool(transect);
	this.addTool(pan);
	this.addTool(zoomOut);
	this.addTool(zoomIn);
}

ToolBox.prototype.addTool = function(tool) {
	this.tools[this.tools.length] = tool;
	this.parentLayer = xGetElementById('tools');
	this.parentLayer.insertBefore(tool.icon, this.parentLayer.firstChild);
}

ToolBox.prototype.removeTool = function(tool) {
	//
}

ToolBox.prototype.disableTool = function(tool) {
	//
}

ToolBox.prototype.enableTool = function(tool) {
	// Set icon to OFF
	//tool.icon.setAttribute("src", this.icon.getAttribute("off"));
	// Remove all input listeners
	//xRemoveEventListener(this.icon, 'mouseover', focusTool, false);
	//xRemoveEventListener(this.icon, 'mouseup', activate, false);
	
}


ToolBox.prototype.activateTool = function(tool) {
	if (typeof(tool) == 'string') {
		// object not passed, so get element based on name
		for (var i=0; i<this.tools.length; i++) {
			if (this.tools[i].name == tool) tool = this.tools[i];
		}
		if (typeof(tool) == 'string') return false; // failed to get tool matching name
	}
	for(var i = 0; i < this.tools.length; i++) {
		this.tools[i].icon.setAttribute("src", this.tools[i].icon.getAttribute('off'));
		xAddEventListener(this.tools[i].icon, 'mouseout', this.tools[i].unFocusMe, false);
	}
	xRemoveEventListener(tool.icon, 'mouseout', tool.unFocusMe, false);
	tool.icon.setAttribute("src", tool.icon.getAttribute("on"));
	this.mapManager.setActionState(tool.icon.getAttribute("name"));
}


