/**
 * Smooth opacity changing using CSS
 * Disabled for IE 5.5 and IE 6.
 * from http://brainerror.net/scripts/javascript/blendtrans/
 */


/**
 * Smoothly change the opacity of the element with the given id from
 * opacStart (0-100) to opacEnd (0-100) over millisec milliseconds.
 */
function com_threerings_fading_fadeOpacity(id, opacStart, opacEnd, millisec) 
{
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("com_threerings_fading_changeOpacity(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("com_threerings_fading_changeOpacity(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

/**
 * Change the opacity of the given object id for different browsers,
 * using a variety of css elements.
 */
function com_threerings_fading_changeOpacity(opacity, id) 
{
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}


/** If faded out, this will be less than 100% opaque */
var currentOpacity = 100;

/** 
 * Automatic fade back in during window focus 
 */
var com_threerings_fading_onfocus = window.onfocus;
window.onfocus = function () 
{
    // Execute the chained onfocus handler
    if (typeof(com_threerings_fading_onfocus) == 'function') {
        com_threerings_fading_onfocus();
    }

    // disable for ie 5.5 + 6, it interferes with pngfix.
    if (/MSIE (5\.5|6\.)/.test(navigator.userAgent)) {
        return;
    }

    // if the screen is faded out, fade it back in
    if (currentOpacity < 100) {
        fromOpacity = currentOpacity;
        currentOpacity = 100;
        com_threerings_fading_fadeOpacity('YPP_templateContainer', fromOpacity, 100, 500);
    }
}

/** 
 * Call this to fade out to opacity % (optional; defaults to 50) 
 */
function fadeout(opacity) 
{
    // disable for ie 5.5 + 6, it interferes with pngfix.
    if (/MSIE (5\.5|6\.)/.test(navigator.userAgent)) {
        return;
    }

    // if the screen is faded in, fade it out
    if (currentOpacity == 100) {
	    if (opacity == null) {
	        opacity = 50;
	    }
        currentOpacity = opacity;
        com_threerings_fading_fadeOpacity('YPP_templateContainer', 100, opacity, 500);
    }
}
