// ******************************************************************************
// MODULE NAME	:	imagefade3.js
// PURPOSE		:	rotates pictures (with or without fading) also in FF and Safari
// AUTHOR		:	KBE28022008 (based on imagefade2.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 delay in ms, fade delay in ms)
// RETURNS			:
// AUTHOR			:	KBE28022008
// HISTORY			:
function imageFade(imageContainer, imagePath, imageArray, random, rotateDelay, fadeDelay) {

	this.img = '';
	this.imageMax = 0;
	this.imageNo = 0;
	this.imagePath = imagePath;
	this.imageArray = imageArray;

	this.imageContainer = imageContainer;
	this.imageRotateDelay = rotateDelay;
	this.imageFadeDelay = fadeDelay;



	// initialize imageArray
	var str = this.imageArray;
	var arrTmp = str.split( "," );

	if( arrTmp.length == 0 ) {
		arrTmp = new Array( 1 );
		arrTmp[ 0 ] = str;
		}
	
	this.imageMax = arrTmp.length;
	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;
		document.getElementById( this.imageContainer ).src = this.img[ 0 ].src;
		this.timerID = setInterval( function() { that.imageRotate() }, that.imageRotateDelay );
	}
	
	
	// keep rotating pictures (without fade)
	this.imageRotate = function() {
		this.imageNo++;

		if ( this.imageNo >= this.imageMax ) {
			this.imageNo = 0;
		};

		document.getElementById( this.imageContainer ).src = this.img[ this.imageNo ].src;
	} 



	// start image rotation (with fade)
	this.imageFadeStart = function() {
		var that = this;
		document.getElementById( this.imageContainer ).src = this.img[ 0 ].src;
		this.timerID = setInterval( function() { that.imageFadeRotate() }, that.imageRotateDelay );
	}



	// keep rotating pictures (with fade)
	this.imageFadeRotate = function() {
		this.imageNo++;

		if( this.imageNo >= this.imageMax ) {
			this.imageNo = 0;
		};

		if ( document ) {
			document.getElementById( this.imageContainer ).style.filter = "blendTrans(duration=" + this.imageFade + ")";
			document.getElementById( this.imageContainer ).style.filter = "blendTrans(duration=crossFadeDuration)";
			document.getElementById( this.imageContainer ).filters.blendTrans.Apply();
		}

		document.getElementById( this.imageContainer ).src = this.img[ this.imageNo ].src;

		if ( document ) {
			document.getElementById( this.imageContainer ).filters.blendTrans.Play();
		}

	}

}

// FUNCTION NAME	:	imageFadeWithPriorities
// PURPOSE			:
// ARGUMENTS		:	(document image name, path to images, array of image names, randomize true/false, rotate delay in ms, fade delay in ms)
// RETURNS			:
// AUTHOR			:	KBE28022008
// HISTORY			:
function imageFadeWithPriorities( imageContainer, imagePath, imageArray, random, rotateDelay, fadeDelay ) {
	this.img = '';
	this.imageMax = 0;
	this.imageNo = 0;
	this.imagePath = imagePath;
	eval( "this.imageArray = " + imageArray );
	this.imageContainer = imageContainer;
	this.imageRotateDelay = rotateDelay;
	this.imageFadeDelay = fadeDelay;

	this.imageMax = this.imageArray.length;

	// preload images
	this.currentPriority = 0;
	if ( document.images ) {
		var priority, image;
		for( priority in this.imageArray ) {
			for( image in this.imageArray[ priority ] ) {
				this.imageArray[ priority ][ image ].image = new Image();
				this.imageArray[ priority ][ image ].image.src = this.imagePath + this.imageArray[ priority ][ image ].filename;
				}
			}
	
		}

	// ---------------------------------

	// start image rotation (without fade)
	this.imageStart = function() {
		var that = this;
		var elem;
		elem = document.getElementById( this.imageContainer );
		if( elem ) {
			elem.src = this.imageArray[ 0 ][ 0 ].image.src;
			}
		this.timerID = setInterval( function() { that.imageRotate() }, that.imageRotateDelay );
		}
	
	// keep rotating pictures (without fade)
	this.imageRotate = function() {
		var elem, len, rnd;
		this.currentPriority++;

		if( this.currentPriority >= this.imageArray.length ) {
			this.currentPriority = 0;
			}

		elem = document.getElementById( this.imageContainer );
		if( elem ) {
			len = this.imageArray[ this.currentPriority ].length;
			rnd = Math.floor( Math.random() * ( len  ) );
			elem.src = this.imageArray[ this.currentPriority ][ rnd ].image.src;
			}

		} 

	// ---------------------------------

	// start image rotation (with fade)
	this.imageFadeStart = function() {
		var that = this;
		var elem;
		elem = document.getElementById( this.imageContainer );
		if( elem ) {
			elem.src = this.imageArray[ 0 ][ 0 ].image.src;
			}
		this.timerID = setInterval( function() { that.imageFadeRotate() }, that.imageRotateDelay );
		}

	// keep rotating pictures (with fade)
	this.imageFadeRotate = function() {

		var elem, len, rnd;
		this.currentPriority++;

		if( this.currentPriority >= this.imageArray.length ) {
			this.currentPriority = 0;
			}

		if ( document ) {
			document.getElementById( this.imageContainer ).style.filter = "blendTrans(duration=" + this.imageFade + ")";
			document.getElementById( this.imageContainer ).style.filter = "blendTrans(duration=crossFadeDuration)";
			document.getElementById( this.imageContainer ).filters.blendTrans.Apply();
			}

		elem = document.getElementById( this.imageContainer );
		if( elem ) {
			len = this.imageArray[ this.currentPriority ].length;
			rnd = Math.floor( Math.random() * ( len  ) );
			elem.src = this.imageArray[ this.currentPriority ][ rnd ].image.src;
			}

		if ( document ) {
			document.getElementById( this.imageContainer ).filters.blendTrans.Play();
			}

		}

	}


