//
// rewrite of the IXF1.11 :: Image cross-fade script by http://www.brothercake.com/. 
//
// changes: Changed code into on object
//
// todo: json configuration
// 
// Altered and completed by Peter Hagen
// http://www.willow-net.nl
// http://www.dm-ict-service.nl
//

/* example

<script>

	function setup()
	{
		cf = new crossfade();
		cf.imgs[0] = "header.1.jpg";
		cf.imgs[1] = "header.2.jpg";
		cf.imgs[2] = "header.3.jpg";
		cf.imgs[3] = "header.4.jpg";

		cf.preloadImages();
		cf.setElement(document.getElementById('headxfade'));
		cf.startDelay = 3000;
		cf.interval = 5000;
		cf.run();
	}

WillowRegisterOnloadFunction(setup);

</script>

/**/
function crossfade()
{
	crossfade.Instances = new Array();

	this.id = crossfade.Instances.length;
	this.clock = null;
	this.count = 1;
	this.imgs = new Array;
	this.cache = new Array;

	this.obj = null;	/* reference to image element /**/
	this.src = null;
	this.newimg = null;
	this.length = 0;
	this.resolution = 0;

	this.debug = false;

	crossfade.Instances[this.id] = this;

	this.imgRef = -1;
	this.startDelay = 0;
	this.interval = 1000;
	this.runCounter = 0;

	/* events */
	this.onAfterFirstFade = null;

	/* functions */
	this.run = function()
	{
		if (this.imgs.length > 0)
		{
			if (this.runCounter == 1 && this.onAfterFirstFade != null) 
				this.onAfterFirstFade();

			if (this.imgRef == -1 && this.startDelay > 0)
			{
				// first startup
				this.imgRef++;
				setTimeout('crossfade.Instances['+this.id+'].run()' , this.startDelay);
			} else if (this.imgRef == -1) {
				/* first fading */
				this.runCounter++;
				this.imgRef++;
				this.fadeNext();
			} else {
				this.runCounter++;
				setTimeout('crossfade.Instances['+this.id+'].fadeNext()', this.interval);
			}
		}
	}

	this.fadeNext = function()
	{
		// fadeCounter++;
		this.startfade(null,  this.imgs[this.imgRef++], '2', '');	
		if (this.imgRef >= this.imgs.length)
			this.imgRef = 0;
	}

	this.setElement = function(o)
	{
		//copy the image object 
		this.obj = o;

		/* debug */
		if (this.debug && o != null)
			alert(this.obj.id);
	}

	this.preloadImages = function()
	{
		/* debug */
		if (this.debug) alert('nr of images=' + this.imgs.length);

		if (this.imgs.length > 0)
		{
			//cache the images
			imgsLen = this.imgs.length;
		
			for(var i=0; i<imgsLen; i++)
			{
				this.cache[i] = new Image;
				this.cache[i].src = this.imgs[i];
			}
		}
	}

	//crossfade timer function
	this.fade = function()
	{
		// if (this.debug) alert(this.id);

		//decrease the counter on a linear scale
		this.count -= (1 / this.resolution);
	
		//if the counter has reached the bottom
		if(this.count < (1 / this.resolution))
		{
			//clear the timer
			clearInterval(this.clock);
			this.clock = null;
		
			//reset the counter
			this.count = 1;
		
			//set the original image to the src of the new image
			this.obj.src = this.src;
		}
	
		//set new opacity value on both elements
		//using whatever method is supported
		switch(this.type)
		{
			case 'ie' :
				this.obj.filters.alpha.opacity = this.count * 100;
				this.newimg.filters.alpha.opacity = (1 - this.count) * 100;
				break;
			
			case 'khtml' :
				this.obj.style.KhtmlOpacity = this.count;
				this.newimg.style.KhtmlOpacity = (1 - this.count);
				break;
			
			case 'moz' : 
				//restrict max opacity to prevent a visual popping effect in firefox
				this.obj.style.MozOpacity = (this.count == 1 ? 0.9999999 : this.count);
				this.newimg.style.MozOpacity = (1 - this.count);
				break;
			
			default : 
				//restrict max opacity to prevent a visual popping effect in firefox
				this.obj.style.opacity = (this.count == 1 ? 0.9999999 : this.count);
				this.newimg.style.opacity = (1 - this.count);
		}
	
		//now that we've gone through one fade iteration 
		//we can show the image that's fading in
		this.newimg.style.visibility = 'visible';
	
		//keep new image in position with original image
		//in case text size changes mid transition or something
		this.newimg.style.left = this.getRealPosition(this.obj, 'x') + 'px';
		this.newimg.style.top = this.getRealPosition(this.obj, 'y') + 'px';
	
		//if the counter is at the top, which is just after the timer has finished
		if(this.count == 1)
		{
			//remove the duplicate image
			this.newimg.parentNode.removeChild(this.newimg);
			this.run();
		}
	};

	//get real position method
	this.getRealPosition = function()
	{
		this.pos = (arguments[1] == 'x') ? arguments[0].offsetLeft : arguments[0].offsetTop;
		this.tmp = arguments[0].offsetParent;
		while(this.tmp != null)
		{
			this.pos += (arguments[1] == 'x') ? this.tmp.offsetLeft : this.tmp.offsetTop;
			this.tmp = this.tmp.offsetParent;
		}
	
		return this.pos;
	};

	this.startfade = function()
	{
		//if the timer is not already going
		if(this.clock == null && this.obj != null)
		{
			//copy the image src argument 
			this.src = arguments[1];
		
			var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;

			//store the supported form of opacity
			if (false || IE6)
			{
				this.type = 'none';
			}
			else if(typeof this.obj.style.opacity != 'undefined')
			{
				this.type = 'w3c';
			}
			else if(typeof this.obj.style.MozOpacity != 'undefined')
			{
				this.type = 'moz';
			}
			else if(typeof this.obj.style.KhtmlOpacity != 'undefined')
			{
				this.type = 'khtml';
			}
			else if(typeof this.obj.filters == 'object')
			{
				//weed out win/ie5.0 by testing the length of the filters collection (where filters is an object with no data)
				//then weed out mac/ie5 by testing first the existence of the alpha object (to prevent errors in win/ie5.0)
				//then the returned value type, which should be a number, but in mac/ie5 is an empty string
				this.type = (this.obj.filters.length > 0 && typeof this.obj.filters.alpha == 'object' && typeof this.obj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
			}
			else
			{
				this.type = 'none';
			}
		
			//change the image alt text if defined
			if(typeof arguments[3] != 'undefined' && arguments[3] != '')
			{
				this.obj.alt = arguments[3];
			}
		
			//if any kind of opacity is supported
			if(this.type != 'none')
			{
				//create a new image object and append it to body
				//detecting support for namespaced element creation, in case we're in the XML DOM
				this.newimg = document.getElementsByTagName('body')[0].appendChild((typeof document.createElementNS != 'undefined') ? document.createElementNS('http://www.w3.org/1999/xhtml', 'img') : document.createElement('img'));

				//set positioning classname
				this.newimg.className = 'idupe';
			
				//set src to new image src
				this.newimg.src = this.src

				//move it to superimpose original image
				this.newimg.style.left = this.getRealPosition(this.obj, 'x') + 'px';
				this.newimg.style.top = this.getRealPosition(this.obj, 'y') + 'px';
			
				//copy and convert fade duration argument 
				this.length = parseInt(arguments[2], 10) * 1000;
			
				//create fade resolution argument as 20 steps per transition
				this.resolution = parseInt(arguments[2], 10) * 20;
			
				//start the timer
				this.clock = setInterval('crossfade.Instances['+this.id+'].fade()', this.length/this.resolution);
			}
			//otherwise if opacity is not supported
			else
			{
				//just do the image swap
				this.obj.src = this.src;
				this.run();
			}
		}
	}
};
