// hFlipButton.js;
 
// usage:     <INPUT TYPE="BUTTON" ...>

<!-- FlipButton handler hooks -->
hookEvent( "click", "flipState" )
hookEvent( "mouseover", "highlightState" )
hookEvent( "mouseout", "unhighlightState" )

// Store the element we are highlighting
_hiElement = null;
_timer = null;

function flipState( e )
{
    if ( e.element.name )
    {
        if ( e.element.name.toLowerCase().indexOf( "flip" ) != -1 )
        {
            // Preserve highlighting state, while changing caption
            var str = e.element.value;
            
            if ( str.indexOf( "SLOW" ) != -1 )
                e.element.value = str.charAt( 0 ) 
                                == "*" ? "*FAST*" : " FAST ";
            else
                e.element.value = str.charAt( 0 ) 
                                == "*" ? "*SLOW*" : " SLOW ";
        }
    }        
    
    return true;
}

function highlightState( e )
{
    if ( e.element.name )
    {
        if ( e.element.name.toLowerCase().indexOf( "highlight" ) != -1 )
        {
            // Set the element to highlight
            _hiElement = e.element;
        
            // Clear previous timers, and apply highlight
            if ( _timer )
                clearTimeout( _timer );
            
            doHighlight();
        }
    }
    return true;
}
function unhighlightState( e )
{
    if ( e.element.name )
    {
        if ( e.element.name.toLowerCase().indexOf( "highlight" ) != -1 )
        {
            // Restore the button to its unhighlit state
            if ( _hiElement != null )
            {
                str = _hiElement.value;
                
                if ( str.charAt( 0 ) == "*" )
                {
                    _hiElement.value = " " + str.substring
                                       ( 1, str.length - 1 ) + " ";
                }
            }        
            // And cancel the highlit element
            _hiElement = null;
        }
    }

    return true;
}
function doHighlight()
{
    if ( _hiElement != null )
    {
        var str = _hiElement.value;
        
        // Toggle the ordinal and terminal characters between " " and "*"
        if ( str.charAt( 0 ) == "*" )
            _hiElement.value = " " + str.substring
                               ( 1, str.length - 1 ) + " ";
        else
            _hiElement.value = "*" + str.substring
                               ( 1, str.length - 1 ) + "*"
                
        // Set the repeat rate
        _timer = setTimeout( "doHighlight()", 300 );
    }
}