//
//	Based on original code from: http://brainerror.net/scripts/javascript/blendtrans/demo.html
//



/*
<div id="blendme" class="blend" style="width:400px; height:200px;">
<img src="image1.jpg" alt="my first image">
<img src="image1.jpg" alt="my second image">
<img src="image1.jpg" alt="my third image">
</div>


blendImages(blendme, 30, 1500, 'caption')

*/


//	Set default values for parameters and starting image
function blendImages(id, speed, pause, caption)
{
    if(speed == null)
        speed = 30;
    
    if(pause == null)
        pause = 1500;

    var blend = document.getElementById(id);

    var image = firstChildImage(blend);

    startBlending(image, speed, pause, caption);
}



//	Find next image amongst siblings inside an element
function nextImage(obj)
{
    do obj = obj.nextSibling;
    while(obj && obj.tagName != 'IMG');

    return obj;
}


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



//	Set the opacity of an element [obj] to a given value [op]
function setOpacity(obj, op) {

    obj.style.opacity = (op / 100);
    obj.style.MozOpacity = (op / 100);
    obj.style.KhtmlOpacity = (op / 100);
    obj.style.filter = 'alpha(opacity=' + op + ')';
}


//	Make current image invisible and make next one the current image
function getNextImage(image)
{
	
    if (next = nextImage(image))
    {
		image.style.display = 'none';
		image.style.zIndex = 0;
	
		next.style.display = 'block';
		next.style.zIndex = 100;

    } else
    {
		//if there is not a next image, return to the first image
		next = firstChildImage(image.parentNode);
    }

    return next;
}


//	Make image a block-element and set the caption
function startBlending(image, speed, pause, caption)
{
    image.style.display = 'block';

//   if(caption != null)
//		document.getElementById(caption).innerHTML = image.alt;

    continueFadeImage(image, 0, speed, pause, caption);
}


// ASC: copied from http://www.sean.co.uk/a/webdesign/javascriptdelay.shtm
function pausecomp(millis)
{
    var date = new Date();
    var curDate = null;

    do { curDate = new Date(); }
    while(curDate-date < millis);
} 



//	Set an increased opacity until blend complete
function continueFadeImage(image, opacity, speed, pause, caption)
{
    opacity = opacity + 3;

    if (opacity < 103)
		setTimeout(function() {fadeImage(image, opacity, speed, pause, caption)}, speed);

    else
    {
		// If the image is done, set it to the background and make it transparent
		image.parentNode.style.backgroundImage = "url("+image.src+")";
	
		// ASC: pause 1 sec here to prevent MSIE image flash ...
		var delay = pause-1000;
		if (delay < 0 )
			delay = 0;
		pausecomp(1000);
	
		setOpacity(image,0);
		//get the next image and start blending it again
		image = getNextImage(image);

		setTimeout(function() {startBlending(image, speed, pause, caption)}, delay);		
    }
}


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