/* js/fade.js -- make an HTML element fade away
Copyright 2008, Martin Rinehart
Based on functions from http://brainerror.net/scripts/javascript/blendtrans/ */

function delayed_fade( id, millis_delay, millis_fade, start, end ) {
    // id -- id of element to fade
    // millis_delay -- millis before starting fade
    // millis_fade -- duration of fade
    // start -- optional starting opacity (1.0 = opaque, default; 0.0 = gone )
    // end -- optional ending opacity (defaults to 0.0)

    to = "fade(";
    to += "'" + id + "'";
    to += "," + millis_fade;
    to += "," + start;
    to += "," + end
    to += ")";
    setTimeout( to, millis_delay );
}

function fade( id, millis, start, end ) {
    // id -- id of element to fade
    // millis -- duration of fade
    // start -- optional starting opacity (1.0 = opaque, default; 0.0 = gone )
    // end -- optional ending opacity (defaults to 0.0)

    // mistake? find out early, complain loudly
    obj = document.getElementById( id );
    if ( !obj ) {
        alert(
            'In fade(): object with id "' + id + '" not found.\n\n'+
            'No one except the programmer should ever see this message.\n\n'+
            'Please contact him immediately ("Contact Us"). Many thanks.' );
        return;
    }

    if ( start === undefined ) start = 1.0;
    if ( end   === undefined ) end   = 0.0;
    //speed for each frame
    var distance = start - end;
    var DELAY = 50 // "constant" - redraw every DELAY millis
    var nsteps = millis / DELAY;
    var stepsize = distance / nsteps;
    var opacity = start;

// alert( 'in fade(): start=' + start +
//     ', end=' + end +
//     ', distance=' + distance +
//     ', nsteps=' + nsteps +
//     ', stepsize=' + stepsize +
//     ', opacity=' + opacity
//     );

    for ( var i = 0; i < nsteps; i++ ) {
        opacity -= stepsize;
        setTimeout( "setOpacity(" + opacity + ", '" + id + "' )", DELAY*i );
    }
    // no ghosts!
    setTimeout( "setOpacity( " + end + ", '" + id + "' )", DELAY*nsteps );
}

// set the opacity for different browsers
// Konqueror had, then lost, opacity capability
function setOpacity( opacity, id ) {
    /* Stuff like this is vital to know and takes forever to ferret out.
       Thanks, brainerror. */

    if ( opacity < 0 ) opacity = 0;
    if ( opacity > 1 ) opacity = 1;

    object = document.getElementById( id )

    if ( object && object.style ) {
        var style_ = object.style;

        style_.opacity = opacity;// most modern browsers
        style_.MozOpacity = opacity; // original Mozilla
        style_.KhtmlOpacity = opacity; // older Konqueror, Safari
        style_.filter = "alpha(opacity=" + (100*opacity) + ")"; // guess who
    }

} // end of changeOpacity()

// end of js/fade.js