

function CRandImage(id, srcs, minTime, maxTime, firstOne, speed){
	var context = this;
	
	this.backDiv = $(id + '_backDiv');
	this.object = $(id);
	this.srcs = srcs;
	this.minTime = minTime;
	this.maxTime = maxTime;
	this.actPicture  = firstOne;
	this.nextPicture = 0;
	this.actOpacFactor = 100;
	
	this.changeOpac 	 = changeOpac;
	this.initTimer 	 = initTimer;
	this.timeOutAction = timeOutAction;
	this.replaceImage  = replaceImage;
	this.getNextPicture= getNextPicture;
	this.nextOpac		 = nextOpac;
	this.speed 			 = speed;
	
	
	assert(this.object, 'Can\'t find ' + id);
	assert(this.backDiv, 'Can\'t find backDiv');
		
	this.initTimer();
	
	function replaceImage(){
		this.object.src = this.srcs[this.actPicture];
		this.changeOpac(100);
	}
	
	function timeOutAction(){
		this.actPicture = this.nextPicture;
		var nextImage = this.srcs[this.actPicture];
		 
		this.backDiv.style.backgroundImage = "url(" + nextImage + ")";
		
		this.actOpacFactor = 100;
		var i;
		for (i = 0; i < 100; ++i){
			setTimeout(function() { context.nextOpac(); }, i * this.speed);
		}
		
		setTimeout(function() { context.replaceImage();}, 100 * this.speed);
	
		this.initTimer();
	}
	
	function nextOpac(){
		this.changeOpac(--this.actOpacFactor);
	}


	function initTimer(){
		var time = parseInt((this.maxTime - this.minTime) * Math.random() + this.minTime);

		this.nextPicture = this.getNextPicture();
		
		MM_preloadImages(this.srcs[this.nextPicture]);
		
		if (time > 0){
			setTimeout(function(){ context.timeOutAction(); }, time);
		}
	}
	

	function changeOpac(opacity) {
		var object = this.object;
		
		object = object.style;	 
		object.opacity = (opacity / 100);
		object.MozOpacity = (opacity / 100);
		object.KhtmlOpacity = (opacity / 100);
		object.filter = "alpha(opacity=" + opacity + ")";
	}
	
	function getNextPicture(){
		var newPict;
		
		do{		
			newPict = Math.round(Math.random() * (this.srcs.length - 1));
		}while(newPict == this.actPicture && this.srcs.length > 1);
		
		return newPict;
	}

}