// ============================================================//
// Copyright (c) 2000-2003 GE Smallworld. All Rights Reserved.
//
// ===========================================================// MAX_EVENTS is a constant located in the 'globals.js' file

var hcfRoot = this.top;
var evtUpList_names       = new Array(hcfRoot.MAX_EVENTS);
var evtDownList_names     = new Array(hcfRoot.MAX_EVENTS);
var evtMoveList_names     = new Array(hcfRoot.MAX_EVENTS);
var evtKeyDownList_names      = new Array(hcfRoot.MAX_EVENTS);
var evtMouseOverList_names = new Array(hcfRoot.MAX_EVENTS);
var evtMouseOutList_names = new Array(hcfRoot.MAX_EVENTS);

var evtDownList_funcs     = new Array(hcfRoot.MAX_EVENTS);
var evtUpList_funcs       = new Array(hcfRoot.MAX_EVENTS);
var evtMoveList_funcs     = new Array(hcfRoot.MAX_EVENTS);
var evtKeyDownList_funcs      = new Array(hcfRoot.MAX_EVENTS);
var evtMouseOverList_funcs = new Array(hcfRoot.MAX_EVENTS);
var evtMouseOutList_funcs = new Array(hcfRoot.MAX_EVENTS);

var evtDownListCount = 0;
var evtUpListCount   = 0;
var evtMoveListCount = 0;
var evtKeyDownListCount  = 0;
var evtMouseOutListCount = 0;
var evtMouseOverListCount = 0;


// -------------  Hook up Events  ----------------------
function hookUpEvents(window) {
   // This function hooks up the correct events 
   // to the map layer.
   //
   // Parameters:
   //    window: window - The window that contains the
   //          map layer.
   //
   // Return: none
   
   // Get the map layer - note that the window
   // is the current window object for this frame, 
   // not top.
   var aMapLayer = hcfRoot.xbGetElementById('mapLayer', window);
   
   // get style information for map layer
   var aMapStyle = new hcfRoot.xbStyle(aMapLayer);
   
   // now set up mouse events
   aMapLayer.onmousedown=onMapImageDown;
   aMapLayer.onmouseup=onMapImageUp;
   aMapLayer.onmousemove=onMapImageMove;
   aMapLayer.onmouseover=onMapImageMouseOver;
   aMapLayer.onmouseout=onMapImageMouseOut;

   // next set up key events
   aMapLayer.onkeydown=onMapImageKeyDown;
   
	//switch to enable mouse events was here
}   

//************* Mouse Event Support************************
function setMouseMoveEvents(aMouseLayer, enabled) {
	if (enabled) {
	   // now disable drag and drop
	   switch (hcfRoot.navigator.family) {
	      case 'ie4':
	         if (hcfRoot.navigator.version >= 5) {
	            // this is ie5
	      		aMouseLayer.setCapture();
	         }
	         else {        
	            // this is ie4    
	            //need to return false from drag start method or else IE 4 thinks
	            //we are draggin an object around
	            aMouseLayer.ondragstart=onMapDragStart;
	         }  
	         
	         break;
	                
	      case 'gecko':
	         // setup drag events - we need to turn dragging off
	         // so that Mozilla does not try to drag-and-drop the
	         // map image
	         aMouseLayer.addEventListener("draggesture", onMapDragStart, true);
	
	         break;
		}
	}
   else {
	   switch (hcfRoot.navigator.family) {
	      case 'ie4':
	         if (hcfRoot.navigator.version >= 5) {
	            // this is ie5
   				aMouseLayer.releaseCapture();
	         }
	         else {        
	            // this is ie4    
         		//need to return false from drag start method or else IE 4 thinks
         		//we are draggin an object around
         		aMouseLayer.ondragstart=null;  
	         }  
	         break;
	                
	      case 'gecko':
	         //aMapLayer.addEventListener("draggesture", onMapDragStart, true);
	         break;
		}
	}
}

 
// -------------  Register Events  ----------------------
function registerMapEventDown(evtName, evtFunc) {
	// Register events with a name and corresponding function.
	// The function will be called with two parameters: x and y.

	if (evtDownListCount + 1 == hcfRoot.MAX_EVENTS) {
		alert(hcfRoot.MAX_EVENTS_ERROR_MESSAGE + evtName + REGISTER_MESSAGE);
		return;
	}
	evtDownList_names[evtDownListCount] = evtName;
	evtDownList_funcs[evtDownListCount] = evtFunc;
	evtDownListCount ++;
}

function registerMapEventUp(evtName, evtFunc) {
	// Register events with a name and corresponding function.
	// The function will be called with two parameters: x and y.
	if (evtUpListCount + 1 == hcfRoot.MAX_EVENTS) {
		alert(hcfRoot.MAX_EVENTS_ERROR_MESSAGE + evtName + REGISTER_MESSAGE);
		return;
	}
	evtUpList_names[evtUpListCount] = evtName;
	evtUpList_funcs[evtUpListCount] = evtFunc;
	evtUpListCount ++;

}

function registerMapEventMove(evtName, evtFunc) {
	// Register events with a name and corresponding function.
	// The function will be called with two parameters: x and y.

	if (evtMoveListCount + 1 == hcfRoot.MAX_EVENTS) {
		alert(hcfRoot.MAX_EVENTS_ERROR_MESSAGE + evtName + REGISTER_MESSAGE);
		return;
	}
	evtMoveList_names[evtMoveListCount] = evtName;
	evtMoveList_funcs[evtMoveListCount] = evtFunc;
	evtMoveListCount ++;
}

function registerMapEventMouseOver(evtName, evtFunc) {
	// Register events with a name and corresponding function.
	// The function will be called with two parameters: x and y.

	
	if (evtMouseOverListCount + 1 == hcfRoot.MAX_EVENTS) {
		alert(hcfRoot.MAX_EVENTS_ERROR_MESSAGE + evtName + REGISTER_MESSAGE);
		return;
	}
	evtMouseOverList_names[evtMouseOverListCount] = evtName;
	evtMouseOverList_funcs[evtMouseOverListCount] = evtFunc;
	evtMouseOverListCount ++;
}

function registerMapEventMouseOut(evtName, evtFunc) {
	// Register events with a name and corresponding function.
	// The function will be called with two parameters: x and y.

	
	if (evtMouseOutListCount + 1 == hcfRoot.MAX_EVENTS) {
		alert(hcfRoot.MAX_EVENTS_ERROR_MESSAGE + evtName + REGISTER_MESSAGE);
		return;
	}
	evtMouseOutList_names[evtMouseOutListCount] = evtName;
	evtMouseOutList_funcs[evtMouseOutListCount] = evtFunc;
	evtMouseOutListCount ++;
}

function registerMapEventKeyDown(evtName, evtFunc) {
	// Register events with a name and corresponding function.
	// The function will be called with two parameters: x and y.

	
	if (evtKeyDownListCount + 1 == hcfRoot.MAX_EVENTS) {
		alert(hcfRoot.MAX_EVENTS_ERROR_MESSAGE + evtName + REGISTER_MESSAGE);
		return;
	}
	evtKeyDownList_names[evtKeyDownListCount] = evtName;
	evtKeyDownList_funcs[evtKeyDownListCount] = evtFunc;
	evtKeyDownListCount ++;
}


// ----- event dispatcher -----------------------
function onMapImageDown(evt)
{
   // this method is called when the map is clicked on by the
    // user.  The value of currentTool will determine what action
    // will be done in response to the click.
    // The event must be registered!    
   
   var leftbutton = false;
           
   if(document.all)
   {
      if(event.button == 1)
         leftbutton = true;
   }
   else
   {
      if (evt.which == 1)
         leftbutton = true;
   }
   
   // if the button clicked is the left button
   if(leftbutton)
   {
       // get the coordinates
        var coords = getCoords(evt);
       
       // find and execute tool
        for (i = 0; i < evtDownListCount;i ++) {
          if (evtDownList_names[i] == hcfRoot.TOOL_ALL ||
              (hcfRoot.tools.currentTool != null &&
              evtDownList_names[i] == hcfRoot.tools.currentTool.name)) {
    
                // call event
                return evtDownList_funcs[i](coords);
            }
        }
    }

    return true;
}

function onMapImageUp(evt)
{
    // this method is called when the map is clicked on by the
    // user.  The value of currentTool will determine what action
    // will be done in response to the click.
    // The event must be registered!
    
   var leftbutton = false;
           
   if(document.all)
   {
      if(event.button == 1)
         leftbutton = true;
   }
   else
   {
      if (evt.which == 1)
         leftbutton = true;
   }
 
   if(leftbutton)
   {
       // get the coordinates
        var coords = getCoords(evt);
    
        // find and execute tool
        for (i = 0;i < evtUpListCount;i ++) {
          if (evtUpList_names[i] == hcfRoot.TOOL_ALL ||
              (hcfRoot.tools.currentTool != null &&
              evtUpList_names[i] == hcfRoot.tools.currentTool.name)) {
    
                // call event
                return evtUpList_funcs[i](coords);
            }
        }
   }

   return true;
}

function onMapImageMove(evt){
	// this method is called when the map is clicked on by the
	// user.  The value of currentTool will determine what action
	// will be done in response to the click.
	// The event must be registered!
	
   // get the coordinates
	var coords = getCoords(evt);

	// find and execute tool
	for (i = 0; i < evtMoveListCount;i ++) {
      if (evtMoveList_names[i] == hcfRoot.TOOL_ALL ||
         (hcfRoot.tools.currentTool != null && 
          evtMoveList_names[i] == hcfRoot.tools.currentTool.name)) {

			// call event
         // NOTE - we do NOT return when calling a mouse event.  The 
         // reason for this is to allow every listener to keep track
         // of where the mouse is - this ability enables us to do 
         // things like show the current mouse position in the
         // status bar.
		   evtMoveList_funcs[i](coords);
		}
	}
	return false;
}	


function onMapImageMouseOver(evt){
	// this method is called when the mouse moves out of the map component.
	// The value of currentTool will determine what action
	// will be done in response to the event.

   // get the coordinates
	var coords = getCoords(evt);
	
	// find and execute tool
	for (i = 0;i < evtMouseOverListCount;i ++) {
      if (evtMouseOverList_names[i] == hcfRoot.TOOL_ALL ||
          (hcfRoot.tools.currentTool != null && 
          evtMouseOverList_names[i] == hcfRoot.tools.currentTool.name)) {

			// call event
			return evtMouseOverList_funcs[i](coords);
		}
	}
	return false;
}	

function onMapImageMouseOut(evt){
	// this method is called when the mouse moves out of the map component.
	// The value of currentTool will determine what action
	// will be done in response to the event.

   // get the coordinates
	var coords = getCoords(evt);
	
	// find and execute tool
	for (i = 0;i < evtMouseOutListCount;i ++) {
      if (evtMouseOutList_names[i] == hcfRoot.TOOL_ALL ||
          (hcfRoot.tools.currentTool != null && 
          evtMouseOutList_names[i] == hcfRoot.tools.currentTool.name)) {
			
			// call event
			return evtMouseOutList_funcs[i](coords);
		}
	}
	return false;
}	


function onMapImageKeyDown(evt){
	// this method is called when the map receives a map event.
	// The value of currentTool will determine what action
	// will be done in response to the event.

   // get the coordinates
	var coords = getCoords(evt);

	// find and execute tool
	for (i = 0;i < evtKeyDownListCount;i ++) {
      if (evtKeyDownList_names[i] == hcfRoot.TOOL_ALL ||
          (hcfRoot.tools.currentTool != null && 
          evtKeyDownList_names[i] == hcfRoot.tools.currentTool.name)) {
			
			// call event
			return evtKeyDownList_funcs[i](coords);
		}
	}

	return true;
}	

function onMapDragStart(evt) {
   // This event is called when the Navigation Box is first
   // created.  In IE4 and Mozilla, the browser interprets
   // the start of the Navigation Box as a drag-and-drop operation.
   // Thus we capture the event and turn it off.
   //
   // Parameters
   //    evt: event - The event object associated with
   //          the current event.
   //
   // Return: none 

   switch (hcfRoot.navigator.family) {
      case 'ie4':
         // for ie4 simply return false
         return false;
         
         break;
         
      case 'gecko':
         // for Gecko prevent the default handling
         evt.preventDefault();
         
         break;
   }         
}

// --- helper functions ----------------------------------------------
function getCoords(evt) {
	// get the coordinates associated with this event.'
	// coords0, coords1 - Relation to the object that triggered the event
	// coords2, coords3 - Relation to the current layer
	// coords4, coords5 - Relation to the page

	var coords = new Array(6);
   
   switch (hcfRoot.navigator.family) {
      case 'ie4':
   		//get coordinates in relation to object firing event
   		coords[0]=window.event.offsetX;
   		coords[1]=window.event.offsetY;
   
   		//get coordinates in relation to parent object
   		coords[2]=window.event.x;
   		coords[3]=window.event.y;
   
   		// get coordinates in relation to page - note that
   		// we respect scrolled pages.
   		coords[4]=window.event.clientX + document.body.scrollLeft;
   		coords[5]=window.event.clientY + document.body.scrollTop;

         break;

      case 'gecko':
         // get coords in relation to firing object
   		coords[0]=evt.layerX;
   		coords[1]=evt.layerY;
   
         // get coords in relation to the parent element
   		coords[2]=evt.clientX;
   		coords[3]=evt.clientY;    
   
         // get coords in relation to the page
   		coords[4]=evt.pageX;
   		coords[5]=evt.pageY; 
         
         break;
   
      case 'nn4': 
   		coords[0]=evt.x;
   		coords[1]=evt.y;
   
   		//get coord in page
   		coords[2]=evt.pageX;
   		coords[3]=evt.pageY;    
   
         // get coords in relation to the page
   		coords[4]=evt.pageX;
   		coords[5]=evt.pageY;    
      
         break;
   }       
	return coords;
}
