var CmsContImageBlend = new Class({
	
	_images: null,
	
	_current: 0,
	
	_container: null,
	
	_size: null,
	
	_durations: null,
	
	_stack: null,
	
	initialize: function(images,size,durations) {
		this._container = $('cmsContImageBlend__imageContainer');
		
		// determine size to be used by the images
		if("object" === $type(size)) {
			this._size = {
				x: size.x || 0,
				y: size.y || 0
			};
		}
		if(null === this._size || 0 === this._size.x || 0 === this._size.y) {
			this._size = this._determineSize();
		}
		
		// determine the durations used for the blending effect
		if("object" === $type(durations)) {
			this._durations = {
				display: ($defined(durations.display) && durations.display > 0) ? durations.display: 5000,
				blend: ($defined(durations.blend) && durations.blend > 0) ? durations.blend: 500
			};
		} else {
			this._durations = {
				display: 5000,
				blend: 500
			};
		}
		
		
		this._images = this._createImages(images);
		this._stack = [];
		
		// only start the effects loop if more than one images are present
		if(this._images.length > 1) {
			setTimeout(function() {
				this._blend();
			}.bind(this), this._durations.display);
		}
	},
	
	_determineSize: function(size) {
		var parent = null,
			child  = this._container;
		
		do {
			if(null !== parent) {
				child = parent;
			}
			parent = child.getParent('div');
			
			if(null === parent) {
				return null;
			}
		} while(parent.getSize().x < 16 || parent.getSize().y < 16 || parent.className.indexOf('cmspid_') > -1);
		
		return parent.getSize();
	},
	
	_createImages: function(imageNames) {
		var images = [],
			image = null,
			styles = {
				position: 'absolute',
				opacity: 0
			};
		
		imageNames.each(function(name,index) {
			image = new CmsContImageBlendImage(name,this._size,this._durations.blend,"cmsContImageBlendImage__"+index);
			
			if(0 === index) {
				image.setStyle("opacity",1);
			}
			
			image.inject(this._container);
			
			images.push(image);
		}.bind(this));
		
		return images;
	},
	
	_getRandomImage: function() {
		
		if(this._images.length < 1) {
			return null;
		}
		
		if(1 === this._images.length) {
			return this._images[0];
		}
		
		if(this._stack.length === this._images.length) {
			// all images have been used.
			this._stack = [];
		}
		
		var i = 0;
		
		if(this._stack.length === this._images.length-1) {
			for(var j=0;i<this._images.length;j++) {
				if(-1 === this._stack.indexOf(j)) {
					i = j;
					break;
				}
			}
		} else {
			do {
				i = parseInt(Math.random()*this._images.length,10);
			} while((this._stack.indexOf(i) > -1 || i === this._current) && this._images.length > 1);
		}
		this._current = i;
		this._stack.push(i);
		
		return this._images[i];
	},
	
	_blend: function() {
		var img1 = this._images[this._current];
		var img2 = this._getRandomImage();
		img1.hide();
		img2.show();
		setTimeout(function(){this._blend();}.bind(this),this._durations.display);
	},
	
	// GETTERS AND SETTERS
	
	getImages: function() {
		return this._images;
	},
	
	getCurrent: function() {
		return this._current;
	},
	
	getContainer: function() {
		return this._container;
	},
	
	getSize: function() {
		return this._size;
	}
});
