// =============================================================
//
// Copyright (c) 2000-2004 General Electric Company. All Rights Reserved.
//
// =============================================================

// -- constants ----
var TOOL_ALL = 'all';
var TOOL_AUTOBUS = 'lineas_autobus';
var TOOL_NORMATIVA='normativa';
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';

// 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_AUTOBUS = 'lineas_autobus';
   this.TOOL_NORMATIVA = 'normativa';	
   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';
}


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
   
	if ((aName=='lineas_autobus' && hcf_panel.document.body.id != 'lineas_autobus') || 
   		(aName=='normativa' && hcf_panel.document.body.id != 'normativa')){
   	aName='select';
   }
  
   if (aName == 'select' && hcf_panel.document.body.id == 'lineas_autobus') {
   				aName='lineas_autobus';
   }
   if (aName == 'select' && hcf_panel.document.body.id == 'normativa') {
   				aName='normativa';
   }

   if (hcf_panel.document.body.id == 'caminos' && aName == 'trace_start') {
   
	elinicio=xbGetElementById('dinicio', hcf_panel);
	if (elinicio != undefined) {
	      elinicioestilo = new xbStyle(elinicio);
      	if (elinicioestilo.getVisibility()=='visible')
			aName='trace_start';
		else
		{
			elfinal=xbGetElementById('dfin', hcf_panel);
			if (elfinal != undefined) {
	     			elfinalestilo = new xbStyle(elfinal);
      			if (elfinalestilo.getVisibility()=='visible')
					aName='trace_end';
			}
			else
				aName='select';

		}
   	}
  }	

   // 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

   	// Set button image
	if (this.currentTool != null)
		this.currentTool.toggleImageUp();

	this.currentTool = aTool;
   
	if (this.currentTool != null)
		this.currentTool.toggleImageDown();

	// Set cursor
	var mapImg = xbGetElementById('mapImage', hcf_content)

	if (mapImg && aTool)
	{
		changeCursor:
		//switch (aTool.name)

		switch (aTool.name)
		{
				case TOOL_PAN:
					mapImg.style.cursor = 'move';
					break changeCursor;
				case TOOL_SELECT:
					mapImg.style.cursor = 'auto';
					break changeCursor;
				case TOOL_AUTOBUS:
					mapImg.style.cursor = 'auto';
					break changeCursor;
				case TOOL_NORMATIVA:
					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';

//CAmbiado por Myriam
	if (this.name == 'lineas_autobus' || this.name == 'normativa') {
	 	imagen = 'select'
		}
	else {
		imagen = this.name
	}

	// 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(imagen, top.botonera) != null)
			return top.xbGetElementById(imagen, top.botonera);
		if(top.xbGetElementById(imagen, top.hcf_panel) != null)
			return top.xbGetElementById(imagen, top.hcf_panel);
	}
	else { 
		return top.xbGetElementById(imagen, top.botonera);	
	}


}

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;
}



