// =============================================================
//
// Copyright (c) 2000-2003 GE Smallworld. All Rights Reserved.
//
// =============================================================

// -- constants ----
var TOOL_ALL = 'all';
var TOOL_SELECT = 'select';
var TOOL_PAN = 'pan';
var TOOL_ZOOM_IN = 'zoom_in';
var TOOL_ZOOM_OUT = 'zoom_out';
var TOOL_TRAIL_POINT = 'trail_point';
var TOOL_TRACE_CLEAR = 'trace_clear';
var TOOL_TRACE_START = 'trace_start';
var TOOL_TRACE_END = 'trace_end';
var TOOL_TRACE_STOP = 'trace_stop';
var TOOL_MEASURE_POINT = 'measure_point';
var TOOL_VER_PDF = 'ver_pdf';


// setup global value for tools object
var tools = new Tools();


// -------  Tools Object --------------------------
function Tools() {
   // This method creates a new instance of the Tools object.
   //
   // Parameters: none
   //
   // Return: none

   // setup methods
   this.changeTool = Tools_changeTool;
   this.setTool = Tools_setTool;
   
   // setup default tool
   this.currentTool = null;

   // -- constants ----
   this.TOOL_SELECT = 'select';
   this.TOOL_PAN = 'pan';
   this.TOOL_ZOOM_IN = 'zoom_in';
   this.TOOL_ZOOM_OUT = 'zoom_out';
   this.TOOL_TRAIL_POINT = 'trail_point';
   this.TOOL_TRACE_CLEAR = 'trace_clear';
   this.TOOL_TRACE_START = 'trace_start';
   this.TOOL_TRACE_END = 'trace_end';
   this.TOOL_TRACE_STOP = 'trace_stop';
   this.TOOL_MEASURE_POINT = 'measure_point';
	this.TOOL_VER_PDF = 'ver_pdf';
}


function Tools_setTool(aName, aFrame) {
   // This method sets the currently active tool.
   //
   // Parameters
   //    aName: string - The name of the tool.
   //    aFrame: frame - The frame that contains the 
   //             tool's button.
   //
   // Return: none
   
   // create a tool
   aTool = new Tool(aName, aFrame);

   // now setup tool
   this.changeTool(aTool);
}

function Tools_changeTool(aTool)
{
   // This method sets the currently active tool.
   //
   // Parameters
   //    aTool: tool - A instance of a Tool object.
   //
   // Return: none

   // make sure this is a different tool

//	Paul S - 15.10.02 - Allow cursor to be set correctly
//	Paul S - New version - actually change the cursor

	if (this.currentTool != null)
	{
		this.currentTool.toggleImageUp();
}
	this.currentTool = aTool;
   
	if (this.currentTool != null)
	{ 
		this.currentTool.toggleImageDown();
		}

//	Paul S - 15.10.02 - end ----------------------

	// update cursor if this is IE
	if (navigator.family == 'ie4')
	{
		//var mapImg = document.all.mapImage;
		var mapImg = xbGetElementById('mapImage', hcf_content)

		if (mapImg && aTool)
		{
			changeCursor:
			switch (aTool.name)
			{
				case TOOL_PAN:
					mapImg.style.cursor = 'move';
					break changeCursor;
				case TOOL_SELECT:
					mapImg.style.cursor = 'auto';
					break changeCursor;
				case TOOL_ZOOM_IN:
					mapImg.style.cursor = 'crosshair';
					break changeCursor;
				case TOOL_ZOOM_OUT:
					mapImg.style.cursor = 'crosshair';
					break changeCursor;
				case TOOL_TRAIL_POINT:
					mapImg.style.cursor = 'crosshair';
					break changeCursor;
				case TOOL_TRACE_START:
					mapImg.style.cursor = 'crosshair';
					break changeCursor;
				case TOOL_TRACE_END:
					mapImg.style.cursor = 'crosshair';
					break changeCursor;
				case TOOL_TRACE_STOP:
					mapImg.style.cursor = 'crosshair';
					break changeCursor;
				case TOOL_MEASURE_POINT:
					mapImg.style.cursor = 'crosshair';
					break changeCursor;

				default:
					mapImg.style.cursor = 'auto';
			}
		}
	}
}  

// -------  Tool Object --------------------------
function Tool(aName, aFrame) {
   //	----------------------------------------------------------------------
   //	This function defines a Tool object that has a name and targetFrame. 
   //	The intended use of this object is for map tool buttons that are not 
   //	in the hcf_content frame. 
   //	Parameters:
   //		aName: The name of the tool 
   //		aFrame: Frame object where the tool is located
   //	---------------------------------------------------------------------- 

   // setup functions
   this.equals = Tool_equals;
   this.getImage = Tool_getImage;
   this.toggleImageUp = Tool_toggleImageUp;
   this.toggleImageDown = Tool_toggleImageDown;

	// save references to supplied parameters
	this.name = aName;
	this.frame = aFrame;
	
	return this;
}

function Tool_equals(anotherTool) {
   // This function compares two Tool object instances
   // to see if they are equivalent. 
   //
   // Parameters
   //    anotherTool: Tool - A tool object
   //
   // Returns: boolean - True if the two tool objects are 
   //       equivalent, otherwise false.
   if (anotherTool == undefined) {
      return false;
   }
   else if (this.name == anotherTool.name && this.frame == anotherTool.frame) {
      return true;
   }
   else {
      return false;
   }
}
   

function Tool_getImage() {
   // Returns a reference to the image object that
   // represents this tool.
   //
   // Parameters: none
   //
   // Result: element - Element reference to the image.

   // get image name
   aImageName = this.name + '_button';

	// return image
	// return this.frame.document.images[aImageName];
	
	// Need this first check to see if we are in IE 5.01 or less because
	// accessing this.frame.document causes a security error (IE bug). 
	// we hardcode the content and panel frames to avoid this problem but this
	// reduces the extensibility of the HCF for IE5. Thus, in versions higher than
	// 5.01 we use the this.frame attribute.
	if ((top.navigator.family == 'ie4') && (top.navigator.version < 5.5)) {
		if(top.xbGetElementById(this.name, top.hcf_content) != null)
			return top.xbGetElementById(this.name, top.hcf_content);
		if(top.xbGetElementById(this.name, top.hcf_panel) != null)
			return top.xbGetElementById(this.name, top.hcf_panel);
	}
	else { 
		return top.xbGetElementById(this.name, this.frame);	
	}

}

function Tool_toggleImageUp() {
   // Changes the appearence of the (de)selected tool-buttons:
   // the tool button appears pressed in, the prevTool in unselected mode
   // Note: the search-path of the displayed images is constructed by assembling
   // '/sias/images/toolName_button_up.png' or
   // '/sias/images/toolName_button_down.png'
   //
   // Parameters: none
   //
   // Result: none

   // first get the image
   aImage = this.getImage();
   
   if (aImage == undefined) {
      return;
   }
   
   // get the image source
   aImageSource = aImage.src;
   
   // now change the source from down to up
   aImageSource = aImageSource.replace(/_down\./, '_up.');
   
   // now update the image
   aImage.src = aImageSource;
}

function Tool_toggleImageDown() {
   // Changes the appearence of the (de)selected tool-buttons:
   // the tool button appears pressed in, the prevTool in unselected mode
   // Note: the search-path of the displayed images is constructed by assembling
   // '/sias/images/toolName_button_up.png' or
   // '/sias/images/toolName_button_down.png'
   //
   // Parameters: none
   //
   // Result: none

   // first get the image
   aImage = this.getImage();

   if (aImage == undefined) {
      return;
   }

   // get the image source
   aImageSource = aImage.src;

   // now change the source from down to up
   aImageSource = aImageSource.replace(/_up\./, '_down.');

   // now update the image
   aImage.src = aImageSource;
}


