/*

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

Made it better by C.Helmer 2009

*/


var galleryIds = new Array("gallery1", "gallery2", "textgallery", "mixedgallery");

var delays = new Array(10, 500, 10);
var istext = new Array(false, false, true, true);

//var galleryClasses = new Array;

var preInitTimer

function crossfade_init() {
    /* thanks to Steve Clay http://mrclay.org/ for this small Opera fix */
    if (document.getElementById) {
        var i;
        for(i=0; i<galleryIds.length; i++) {
            if (document.getElementById(galleryIds[i])) {
                new faderclass(galleryIds[i], delays[i], istext[i]);
            }
/*            else
                alert("bad " + i);*/
        }               
    } else {
		preInitTimer = setTimeout("crossfade_init",2);
	}
}

/* You cannot call a class metheby timeout from within the class, as "this" gets lost.
    Javascript = great language... not. 
*/
function classtimeout(jclass, timeout, param) {
    var self = jclass;
    setTimeout(
        function(){
            self.crossfade(param);
        }, timeout
    ); 
}


/* classes */
function faderclass(galleryId, startdelay, isText) {

	/* helper function to deal specifically with images and the cross-browser differences in opacity handling */
	this.fader=function (imageNumber,opacity) {	
    	var obj=this.galleryImages[imageNumber];
    	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+")";
    			
    		}
    	}
    };
    
    this.crossfade = function (opacity) {
    	if (opacity < 100) {
    		/* current image not faded up fully yet...so increase its opacity */
    		this.fader(this.currentImage,opacity);
    		
            /*  for text or transparent objects. Try it with images! (bg shines though) */
            if (this.isText)
                this.fader(this.previousImage,100-opacity);
            
    		
    		//   SET FADER SETTINGS HERE !!!!111elf
    		
    		opacity += 2;
    		classtimeout(this, 50, opacity);
            //window.setTimeout("this.crossfade("+opacity+")", 30);
    		
    		
    	} else {
    		/* make the previous image - which is now covered by the current one fully - transparent */
    		this.fader(this.previousImage,0);
    		this.galleryImages[this.previousImage].style.visibility = 'hidden';
    		
    		/* current image is now previous image, as we advance in the list of images */
    		this.previousImage=this.currentImage;
    		this.currentImage+=1;
    		if (this.currentImage>=this.galleryImages.length) {
    			/* start over from first image if we cycled through all images in the list */
    			this.currentImage=0;
    		}
    		/* make sure the current image is on top of the previous one */
    		this.galleryImages[this.previousImage].style.zIndex = 0;
    		this.galleryImages[this.currentImage].style.zIndex = 100;
    		
            this.galleryImages[this.previousImage].style.visibility = 'visible';
    		this.galleryImages[this.currentImage].style.visibility = 'visible';
    		
    		/* and start the crossfade after a pause */
    		classtimeout(this, 3000, 0);
            //window.setTimeout("this.crossfade("+opacity+")", 3000);
    	}	
    };
        		
	//preInit(); /* shouldn't be necessary, but IE can sometimes get ahead of itself and trigger fadeInit first */
	// I tell you what: I give a shit about this anti browsing experience tool.
	
	this.isText = isText;
	this.galleryImages = new Array;
    this.gallery=document.getElementById(galleryId);
    this.node = this.gallery.firstChild;
	/* instead of using childNodes (which also gets empty nodes and messes up the script later)
	we do it the old-fashioned way and loop through the first child and its siblings */
	while (this.node) {
		if (this.node.nodeType==1) {
			this.galleryImages.push(this.node);
		}
		this.node = this.node.nextSibling;
	}
	for(i=0; i<this.galleryImages.length; i++) {
		/* loop through all these child nodes and set up their styles */
		
        //TODO CSS
//        this.galleryImages[i].style.position='absolute';
		//this.galleryImages[i].style.top=0;
		//this.galleryImages[i].style.zIndex=0;
		
		/* set their opacity to transparent */
		this.fader(i,0);
	}
	/* make the list visible again */
	//gallery.style.visibility = 'visible';
	/* initialise a few parameters to get the cycle going */
	this.currentImage=0;
	this.previousImage=this.galleryImages.length-1;
	this.fader(this.currentImage,100);
	/* start the whole crossfade process after a pause */
	
	classtimeout(this, startdelay, 100);
	//window.setTimeout("this.crossfade(100)", 1000);
}

addEvent(window,'load',crossfade_init);

/* 3rd party helper functions */

/* addEvent handler for IE and other browsers */
// addEvent and removeEvent
// cross-browser event handling for IE5+,  NS6 and Mozilla
// By Scott Andrew
function addEvent(elm, evType, fn, useCapture) {
 if (elm.addEventListener){
   elm.addEventListener(evType, fn, useCapture);
   return true;
 } else if (elm.attachEvent){
   var r = elm.attachEvent("on"+evType, fn);
   return r;
 }
} 

