/*
 * Original from: http://brainerror.net/scripts/javascript/blendtrans/demo.html
 *
 * Edits by ASC:
 *   - Removed unnecessary OO code that caused MSIE to choke
 *   - Added pause between setting bg image and setting opacity to zero to
 *     prevent MSIE image flash
 *   - Increased opacity check argument to 103 for completely smooth fade
 *
 */

//find next image
function nextImage(o) {
    do o = o.nextSibling;
    while(o && o.tagName != 'IMG');
    return o;
}

//find first image inside an element
function firstChildImage(o) {
    o = o.firstChild;
    while(o && o.tagName != 'IMG') {
        o = o.nextSibling;
    }
    return o;
}

//set the opacity of an element to a specified value
function setOpacity(obj, o) {
    obj.style.opacity = (o / 100);
    obj.style.MozOpacity = (o / 100);
    obj.style.KhtmlOpacity = (o / 100);
    obj.style.filter = 'alpha(opacity=' + o + ')';
}


//set default values for parameters and starting image
function blendImages(id) {
    speed = 200;
    var blend = document.getElementById(id);
    var image = firstChildImage(blend);
    startBlending(image, speed);
}

//make image a block-element and set the caption
function startBlending(image, speed) {
    image.style.display = 'block';
    continueFadeImage(image, 0, speed, 'ein');
} 

function randNo(min, max){
	return Math.round(Math.random()*(max-min))+min;
}



//set an increased opacity and check if the image is done blending
durchgang = 1;
function continueFadeImage(image, opacity, speed, richtung) {
    if(richtung=='ein'){
	opacity = opacity + 1;
	if(opacity > 70){
		opacity = opacity + 1;
	}else if(opacity < 40){
		opacity = 40;
	}
    }else{
	opacity = opacity - 1;
	if(opacity > 60){
                opacity = opacity - 1;
        }
    }
    if (opacity > 99) {
	// restart blend
	//opacity = 0;
	richtung = 'aus';
        durchgang = 2;
    } else if(opacity < 1) {
	richtung = 'ein';		
    }
    if(durchgang !=2 || richtung == 'aus'){
	setTimeout(function() {fadeImage(image, opacity, speed, richtung)}, speed);
    }
}

//set the opacity to a new value and continue the fading
function fadeImage(image, opacity, speed, richtung) {
    setOpacity(image,opacity);
    continueFadeImage(image, opacity, speed, richtung);
}
