/* imageFx class */
var ImageFx = Class.create();

ImageFx.prototype = {
	
	defaults: {
		fxImages:[],
		selectMode:'random',
		imageIdFirst:'teaser-image',
		imageIdSecond:'',
		duration:2,
		blendFx:true
	},
	
	currentIndex:0,
	
	initialize: function (params) {

		this.fxImages = new Array();
		/* preload images */
		if (!params.images) {
			this.fxImages = this.defaults.fxImages;
		}
		else {
			this.preload(params.images); // preload the images
		}
		/* define image select mode */
		if (!params.selectmode) {
			this.selectMode = this.defaults.selectMode;
		}
		else {
			this.selectMode = params.selectmode; // set selectmode (random,linear)
		}
		/* define first HTML-ID (needed) */
		if (!params.firstId) {
			this.imageIdFirst = this.defaults.imageIdFirst;
		}
		else {
			this.imageIdFirst = params.firstId; // id of image
		}
		/* define second HTML-ID (optional) */
		if (!params.secondId) {
			this.imageIdSecond = this.defaults.imageIdSecond;
		}
		else {
			this.imageIdSecond = params.secondId; // id of second bg-image for blend over
		}
		/* define FX duration */
		if (!params.duration) {
			this.duration = this.defaults.duration;
		}
		else {
			this.duration = params.duration;
		}
		
		this.useragent = new BrowserDetect();
		
		this.blendFx = this.defaults.blendFx;
		if (this.useragent.browser == 'Safari') {
			this.blendFx = false;
		} // use blend fx (safari bug)
	}, 

	preload: function (imageSources) {

		for (i=0;i<imageSources.length;i++) {
			var tempImage = new Image();
			tempImage.src = imageSources[i];
			this.fxImages[i] = tempImage; 
		} // for
	}, 

	rotate: function () {

		var valNumber = this.fxImages.length;
		var rndIndex = Math.round(Math.random()*valNumber);
		var imageIndex = 0;
		if (this.selectMode=='random') {
			imageIndex = this.getRandomIndex();
		}
		else {
			imageIndex = this.getLinearIndex();
		}
		this.currentIndex = imageIndex;
		
		var firstId = this.imageIdFirst;
		var images = this.fxImages;
		var duration = this.duration;

		if (typeof(this.fxImages[imageIndex]) == 'object') {
			
			if (typeof($(this.imageIdFirst))=='object') {
			
				if (this.blendFx == false) {
					$(firstId).hide();
					$(firstId).src = images[imageIndex].src;
					$(firstId).show();
				} // if
				else {

					new Effect.Fade(
						$(firstId), 
						{
							duration:2,
							afterFinish:function() {
								
								$(firstId).src = images[imageIndex].src;

								new Effect.Appear(
									$(firstId), 
									{
										duration:2
									}
								);
							}
						}
					);
				} // else
			} // if
		} // if
	}, // function rotate

	getRandomIndex: function() {
		
		var valNumber = this.fxImages.length;
		var rndIndex = Math.round(Math.random()*valNumber);

		if (valNumber<=1) {
			return rndIndex;
		} // if

		while (this.currentIndex == rndIndex) {
			rndIndex = Math.round(Math.random()*valNumber);
		} // while
		
		return rndIndex;
	},
	
	getLinearIndex: function() {
		
		var valNumber = this.fxImages.length;

		var nextIndex = this.currentIndex+1;
		if (nextIndex >= valNumber) {
			nextIndex = 0;
		} // if
		
		return nextIndex;
	}
}