// ******************************************************************************
// MODULE NAME	:	imagefade2.js
// PURPOSE		:	rotates pictures (with or without fading)
// AUTHOR		:	KBE28022008 (based on imagefade.js)
// ******************************************************************************



// INSERT THIS IN TEMPLATE (remember to change imagename, custom foldername and ImageRotate techCMS-foldername)
// <SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">var foto1=new imageFade('foto1', '/custom/311_233/', '@CUSTOMER|ImageRotate|159||||||||||@',true,3000,3);foto1.imageFadeStart()</SCRIPT>



// FUNCTION NAME	:	imageFade
// PURPOSE			:
// ARGUMENTS		:	(document image name, path to images, array of image names, randomize true/false, rotate speed in ms, fadespeed in ms)
// RETURNS			:
// AUTHOR			:	KBE28022008
// HISTORY			:
function imageFade(imageContainer, imagePath, imageArray, random, rotateSpeed, fadeSpeed) {

	this.img = '';
	this.imageMax = 0;
	this.imageNo = 0;
	this.imagePath = imagePath;
	this.imageArray = imageArray;

	this.imageContainer = imageContainer;
	this.imageTimeout = rotateSpeed;
	this.imageStartTimeout = 1;
	this.imageFade = fadeSpeed;



	// initialize imageArray
	var str = this.imageArray;
	var arrTmp = str.split( "," );
	this.imageMax = arrTmp.length - 1;
	this.imageNo = this.imageMax;

	if ( document.images ) {
		this.img = new Array( this.imageMax );

		for ( var i = 0; i < this.imageMax; i++ ) {
			this.img[ i ] = new Image();
			this.img[ i ].src = this.imagePath + arrTmp[ i ];
		}
	}



	// Fisher-Yates array randomizer
	if ( random ) {
		var i = this.img.length;
		if ( i == 0 ) return false;
		while ( --i ) {
			var j = Math.floor( Math.random() * ( i + 1 ) );
			var tempi = this.img[i];
			var tempj = this.img[j];
			this.img[i] = tempj;
			this.img[j] = tempi;
		}
	}



	// start image rotation (without fade)
	this.imageStart = function() {
		var that = this;
		var timerID = setTimeout( function() { that.imageRotate() }, this.imageStartTimeout );
	}
	
	
	// keep rotating pictures (without fade)
	this.imageRotate = function() {
		this.imageNo++;

		if ( this.imageNo >= this.imageMax ) {
			this.imageNo = 0;
		};

		document.images[ this.imageContainer ].src = this.img[ this.imageNo ].src;

		var that = this;
		var timerID = setTimeout( function() { that.imageRotate() }, this.imageTimeout );
	} 



	// start image rotation (with fade)
	this.imageFadeStart = function() {
		var that = this;
		var timerID = setTimeout( function() { that.imageFadeRotate() }, this.imageStartTimeout );
	}



	// keep rotating pictures (with fade)
	this.imageFadeRotate = function() {
		this.imageNo++;

		if( this.imageNo >= this.imageMax ) {
			this.imageNo = 0;
		};

		if ( document ) {
			document.images[ this.imageContainer ].style.filter = "blendTrans(duration=" + this.imageFade + ")";
			document.images[ this.imageContainer ].style.filter = "blendTrans(duration=crossFadeDuration)";
			document.images[ this.imageContainer ].filters.blendTrans.Apply();
		}

		document.images[ this.imageContainer ].src = this.img[ this.imageNo ].src;

		if ( document ) {
			document.images[ this.imageContainer ].filters.blendTrans.Play();
		}

		var that = this;
		var timerID = setTimeout( function() { that.imageFadeRotate() }, this.imageTimeout );
	}

}

