
function EventCoordinator() {
    setupMouseOver();

    if      (m_sClientBrowserType == 'IE')      setupIE();
    else if (m_sClientBrowserType == 'Safari')  setupSafari();
    else                                        setupNetscape();


    // Basic event handlers

    this.getEvent = getEvent;
    function getEvent( evt ) {
        return (evt) ? evt : event;  // Treat IE's static event as an object
    }

    this.getEventTarget = getEventTarget;
    function getEventTarget( evt ) {
        return (m_sClientBrowserType == 'IE') ?  event.srcElement : evt.target;  //!!! Make clear about FF
    }

    this.getEventCoords = getEventCoords;
    function getEventCoords( evt ) {
        x = (m_sClientBrowserType != 'IE') ? evt.clientX
                                           : evt.clientX - mapSplitter.GetPaneById( "MapControl_LeftPane" ).GetWidth() - 8;
        y = (m_sClientBrowserType != 'IE') ? evt.clientY
                                           : evt.clientY - m_imgLogo.height - 8
        return new point( x, y );
    } 


    // Browser specific setup

    function setupIE() {
        this.L_BUTTON = 1;
        this.M_BUTTON = 4;
        this.R_BUTTON = 2;

        m_imgMapCanvas.onmousedown  = handleMouseDownIE;
        m_imgMapCanvas.onmouseup    = handleMouseUpIE;
        m_imgMapCanvas.onmousemove  = handleMouseMove;
        m_imgMapCanvas.onmousewheel = handleMouseWheel;
    }

    function setupNetscape() {
        this.L_BUTTON = 0;
        this.M_BUTTON = 1;
        this.R_BUTTON = 2;

        m_imgMapCanvas.onmousedown   = handleMouseDown;
        m_imgMapCanvas.onmouseup     = handleMouseUp;
        m_imgMapCanvas.onmousemove   = handleMouseMove;
        m_divDragBox.onmouseup       = handleMouseUp;  // On Firefox the target of onmouseup switches here when you resize the zoom box!

        window.addEventListener( "DOMMouseScroll", handleMouseWheel, false );
    }

    function setupSafari() {
        this.L_BUTTON = 1;
        this.M_BUTTON = 'NA';
        this.R_BUTTON = 'NA';

        m_imgMapCanvas.onmousedown   = handleMouseDown;
        m_imgMapCanvas.onmouseup     = handleMouseUp;
        m_imgMapCanvas.onmousemove   = handleMouseMove;
        m_divDragBox.onmouseup       = handleMouseUp;  //!!! Needed on Safari?
    }

    function handleMouseDownIE( evt ) {
        handleMouseDown( evt );
        m_imgMapCanvas.setCapture();  // turn off drag and drop
        return false;
    }

    function handleMouseUpIE( evt ) {
        handleMouseUp( evt );
        document.releaseCapture();  // release drag and drop
        return false;
    }


    // Map Event Handlers

    var nowPanning = false;  // Kludge var to handle Firefox's lame handling of onmousemove
    var lastCoords = 0;      // Track for use during FF's broken mouse wheel event

    function isZoom( evt )     { return evt.button == L_BUTTON && !evt.ctrlKey && !evt.shiftKey && !evt.altKey; }
    function isMeasure( evt )  { return evt.button == L_BUTTON && evt.ctrlKey; }
    function isPan( evt )      { return evt.button == M_BUTTON || (evt.button == L_BUTTON && evt.shiftKey); }
    function isTool( evt )     { return evt.button == R_BUTTON || (evt.button == L_BUTTON && evt.altKey); }

    function handleMouseDown( evt ) {
        evt    = getEvent( evt );
        coords = getEventCoords( evt );

        if (isZoom( evt ) || isTool( evt ))
            agiZoomBox.start( coords.x, coords.y );

        if (isPan( evt ))
            agiPan.start( evt );

        if (isMeasure( evt ))
            agiMeasure.onMapClick( evt );

        nowPanning = true;
        return false;
    }

    function handleMouseUp( evt ) {
        evt    = getEvent( evt );
        coords = getEventCoords( evt );

        if (isZoom( evt )) {
            agiZoomBox.update( coords.x, coords.y );
            zoomBoxIn( evt );
        }

        if (isPan( evt ))
            agiPan.end( evt );

        if (isTool( evt )) {
            if (agiZoomBox.isInProgress()) {
                if (agiZoomBox.tooSmall())
                    agiZoomBox.setDragBoxAroundEndPoint();
                agiZoomBox.update( coords.x, coords.y );
                agiZoomBox.stop();
                agiMapViewer.setQueryExtent( zoomBoxRect() );
            }

            tabHandlers[currentRightClickTab]();
        }

        nowPanning = false;
        return false;
    }

    function handleMouseMove( evt ) {
        evt        = getEvent( evt );
        lastCoords = getEventCoords( evt );

        reportCoords( agiMapViewer.toMapPoint( lastCoords.x, lastCoords.y ) );

        if (nowPanning)
            agiPan.showDrag( evt );

        if (agiZoomBox.isInProgress())
            agiZoomBox.update( lastCoords.x, lastCoords.y );

        return false;
    }

    function handleMouseWheel( evt ) {
        evt    = getEvent( evt );
        coords = (m_sClientBrowserType == 'Netscape') ? lastCoords : getEventCoords( evt );  // Use last mousemove coords for broken FF mousewheel

        // recenter if not already loading an image, e.g. from multiple wheel events.
        if (lastRequest_ == 0)
            recenter( coords );

        if (evt.wheelDelta > 0 || evt.detail < 0)  zoomInPlaceIn();
        else                                       zoomInPlaceOut();

        return false;
    }


    // Mouse Over handlers

    function setupMouseOver() {
        m_imgMapCanvas.onmouseover      = handleMouseOverMap;
        m_imgOverviewCanvas.onmouseover = handleMouseOverMap;
        m_divCoords.onmouseover         = handleMouseOverMap;
        m_divMeasure.onmouseover        = handleMouseOverMap;
        m_divDragBox.onmouseover        = handleMouseOverMap;
        m_divOverviewBox.onmouseover    = handleMouseOverMap;
        m_zoomBar.onmouseover           = handleMouseOverMap;
        m_bufferSelector.onmouseover    = handleMouseOverMap;
        m_imgLogo.onmouseover           = handleMouseOverLogo;
        m_browsers.onmouseover          = handleMouseOverLogo;
        m_activeLayers.onmouseover      = handleMouseOverQuery;

        m_imgMapCanvas.onmouseout       = handleMouseExit;
        m_imgOverviewCanvas.onmouseout  = handleMouseExit;
        m_divCoords.onmouseout          = handleMouseExit;
        m_divMeasure.onmouseout         = handleMouseExit;
        m_divDragBox.onmouseout         = handleMouseExit;
        m_divOverviewBox.onmouseout     = handleMouseExit;
        m_zoomBar.onmouseout            = handleMouseExit;
        m_bufferSelector.onmouseout     = handleMouseExit;
        m_imgLogo.onmouseout            = handleMouseExit;
        m_browsers.onmouseout           = handleMouseExit;
        m_activeLayers.onmouseout       = handleMouseExit;
    }

    function handleMouseOverLogo( evt )  { setTabHelp();   }
    function handleMouseOverMap( evt )   { setMapHelp();   }
    function handleMouseOverQuery( evt ) { setQueryHelp(); }
    function handleMouseExit( evt )      { setTocHelp();   }
}

