/*

Better(?) Image cross fader (C)2004 Patrick H. Lauke aka redux

Inspired by Steve at Slayeroffice http://slayeroffice.com/code/imageCrossFade/ 

preInit "Scheduler" idea by Cameron Adams aka The Man in Blue
http://www.themaninblue.com/writing/perspective/2004/09/29/ 

Tweaked to deal with empty nodes 19 Feb 2006

Modified for rssb website  Nov 3, 2007.

*/
/* declare fade_pause globally for pausing the fading */
/* galleryImagesArray to be declared outside as an array */
/* blank image should be a 2x2 transparent gif image. by default use the src of second image */
/* general variables */
var blank_image;
var galleryImages; /* array with image 0 and 1 */
var currentImage; /* keeps track of which image should currently be showing */
var preInitTimer;
var active_top = 1; /* is active image on top */
preInit();
/* pause in seconds */
var pause = 2;

// utility function to test if image specified by name image_src is loaded

function IsImageLoaded(image_temp) {
  if ((typeof image_temp.naturalWidth) != "undefined") {
      // for firefox
    if (image_temp.naturalWidth > 20) return true;
    else return false;
  } else {
      // for IE
      return image_temp.complete;
  }
}

/* functions */

function preInit() {
	/* an inspired kludge that - in most cases - manages to initially hide the image gallery list
	   before even onload is triggered (at which point it's normally too late, and the whole list already
	   appeared to the user before being remolded) */
	if (document.getElementById) {
		if (typeof preInitTimer != 'undefined') clearTimeout(preInitTimer); /* thanks to Steve Clay http://mrclay.org/ for this small Opera fix */
	} else {
		preInitTimer = setTimeout("preInit()",2);
	}
}

function fader(imageObj,opacity) {
	/* helper function to deal specifically with images and the cross-browser differences in opacity handling */
	var obj=imageObj;
	if (obj.style) {
		if (obj.style.MozOpacity!=null) {  
			/* Mozilla's pre-CSS3 proprietary rule */
			obj.style.MozOpacity = (opacity/100) - .001;
		} else if (obj.style.opacity!=null) {
			/* CSS3 compatible */
			obj.style.opacity = (opacity/100) - .001;
		} else if (obj.style.filter!=null) {
			/* IE's proprietary filter */
			obj.style.filter = "alpha(opacity="+opacity+")";
		}
	}
}

function fadeInit() {
	if (document.getElementById && galleryImagesArray && (galleryImagesArray.length > 1)) {
		preInit(); /* shouldn't be necessary, but IE can sometimes get ahead of itself and trigger fadeInit first */
                pause = typeof(fade_pause) == "undefined" ? pause : fade_pause;
		galleryImages = new Array;
		var node0 = document.getElementById("image_fader0");
                var node1 = document.getElementById("image_fader1");
                galleryImages.push(node0);
                galleryImages.push(node1);
                galleryImages[0].style.zIndex=100;
                galleryImages[1].style.zIndex=0; 
		if ((typeof  blank_image) == "undefined")
		    blank_image = galleryImages[1].src;
		currentImage=0;
                fader(galleryImages[0], 100);
                fader(galleryImages[1], 100);
                newimage_active(); 
		/* start the whole crossfade process after a pause */
		window.setTimeout("crossfade(0)", pause*1000); 
	}
}

/* called when the new image becomes active */
function newimage_active() {
/*      fader(galleryImages[0], 100);
      fader(galleryImages[1], 0); */

      if (currentImage + 1 >= galleryImagesArray.length) {
         currentImage=0;
      } else {
         currentImage = currentImage + 1;
      }
      var new_index = 0;
      if (active_top) {
         new_index = 1;
      }
      galleryImages[new_index].style.visibility = "hidden";
      galleryImages[new_index].src = blank_image;
      galleryImages[new_index].src = galleryImagesArray[currentImage]
/*      galleryImages[new_index].style.zIndex = 0;
      galleryImages[1 - new_index].style.zIndex = 100; */
/*      var obj = galleryImages[1];
      galleryImages[1] = galleryImages[0];
      galleryImages[0] = obj; */
}

function crossfade(opacity) {
    if (!IsImageLoaded(galleryImages[active_top])) {
	window.setTimeout("crossfade("+opacity+")", 200);
	return;
    } 
    if (opacity < 100) {
	/* current image not faded up fully yet...so increase its opacity */
	galleryImages[0].style.visibility = "visible";
	galleryImages[1].style.visibility = "visible";
	opacity += 10;
	if (active_top) {
	    /*  fade away the top */
	    fader(galleryImages[0], 100 - opacity);
	    fader(galleryImages[1],  opacity);
	} else {
	    /* fade in the top */
	    fader(galleryImages[0], opacity);
	    fader(galleryImages[1], 100 - opacity);
	}
	window.setTimeout("crossfade("+opacity+")", 200);
    } else {
	active_top = 1 - active_top;
	newimage_active();
	window.setTimeout("crossfade(0)", pause*1000);
    }
    
}

 /*
   pre load all the images 
 */
_images_temp = new Array()
if (document.images && (typeof(galleryImages) != "undefined")) {
 for (var i = 0; i < galleryImagesArray.length; i++) {
        var imagex = new Image();
        imagex.src = galleryImagesArray[i]
	_images_temp.push(imagex);
 }
}
/* done load */


/* initialise fader by hiding image object first */
addEvent(window,'load',fadeInit)



/* 3rd party helper functions */

/* addEvent handler for IE and other browsers */
function addEvent(elm, evType, fn, useCapture) 
// addEvent and removeEvent
// cross-browser event handling for IE5+,  NS6 and Mozilla
// By Scott Andrew
{
 if (elm.addEventListener){
   elm.addEventListener(evType, fn, useCapture);
   return true;
 } else if (elm.attachEvent){
   var r = elm.attachEvent("on"+evType, fn);
   return r;
 }
} 
