// These are constants, but IE6 doesn't support 'const'
// Thanks, Microsoft.
var OPACITY_DELTA = 10;
var OPACITY_MAX = 100;

/*
  Global array to keep track of the fading images.
  Necessary because we can't pass anything good to
  setTimeout or setInterval;
*/
var fading_images = [];

// All the images will be at the same opacity for performance reasons.
// I did test this the other way, and it IS slow.
var current_opacity = 0;


/*
  This is here because we don't want to make the images
  invisible by default. If they were, and javascript was
  disabled, we'd never see them. Furthermore, it's important
  to use a pre-1.1 version of mootools here, since v1.1 uses
  XPATH which (I believe) won't work until the DOM is fully
  loaded.
*/
$$(".fade-in img").each(function (element, index) {
    element.style.visibility = "hidden";
    element.fading_image_index = fading_images.length;
    fading_images.push(element);
});



function init_image_fades() {
    fading_images.each(function (image, i) {
	set_opacity(image, current_opacity);
	image.style.visibility = "visible";
    });

    // The first timeout.
    window.setTimeout("fade_all_in()", 150);
}



function fade_all_in() {
    // Increase the opacity either way.
    current_opacity += OPACITY_DELTA;

    if (current_opacity > OPACITY_MAX) {
	// If the value of current_opacity is unacceptable, just return
	// without setting another timeout or changing any opacities.
	return;
    }
    
    // If the new value's good, set it on the images.
    for (var i = 0; i < fading_images.length; i++) {
	set_opacity(fading_images[i], current_opacity);
    }
    
    // And try to increase the opacity again in .15 seconds.
    window.setTimeout("fade_all_in()", 150);

    return;
}



function set_opacity(obj, opacity) {
    // IE/Win
    obj.style.filter = "alpha(opacity:"+opacity+")";
    
    // Safari<1.2, Konqueror
    obj.style.KHTMLOpacity = opacity/100;
    
    // Older Mozilla and Firefox
    obj.style.MozOpacity = opacity/100;
    
    // Safari 1.2, newer Firefox and Mozilla, CSS3
    obj.style.opacity = opacity/100;

    return;
}


window.addEvent("load", init_image_fades);
