
ExtImgRotator.randomOrder = true;
ExtImgRotator.fadeImages = true;
ExtImgRotator.fadeduration = 3000;
ExtImgRotator.fadeSteps = 50;
ExtImgRotator.minShowtime = 8000;
ExtImgRotator.maxShowtime = 8000;
ExtImgRotator.col=[];

function ExtImgRotator(id, path, imgAr, randomOrder, minShowTime, maxShowTime, fadeDuration){
	this.id = id;
	this.name='rotator'+this.id;
	this.carlink='carlink'+this.id;
	this.cartext='cartext'+this.id;
	this.randomOrder = (randomOrder == null) ? ExtImgRotator.randomOrder : randomOrder;
	this.minShowtime = (minShowTime == null) ? ExtImgRotator.minShowtime : minShowTime;
	this.maxShowtime = (maxShowTime == null) ? ExtImgRotator.maxShowtime : maxShowTime;
	this.fadeduration = 500; //(fadeDuration == null) ? ExtImgRotator.fadeDuration : fadeDuration;
	this.fadeImages = false; //''(fadeDuration == 0) ? false : true;
	this.fadeSteps = Math.floor((this.fadeduration * 1.5) / 100);
	this.speed = Math.floor(Math.random()*(this.maxShowtime - this.minShowtime)) + this.minShowtime-this.fadeduration;
	this.path=path||"";
	this.ctr=0;
	this.timer=0;
	this.imgs=[];
	this.href=[];
	this.text=[];
	this.actions=[];
	this.index=ExtImgRotator.col.length;
	ExtImgRotator.col[this.index]=this;
	this.animString="ExtImgRotator.col["+this.index+"]";
	this.doStop = false;
	
	// Add images
	for(var i=0;imgAr[i];i++)
		this.addImages(imgAr[i]);
			
	// Show first image
	if (this.randomOrder) {
		this.ctr=Math.floor(Math.random()*imgAr.length);
	}
	var img=imgAr[this.ctr][0];
	var imgStr='<img name="'+this.name+'" src="'+this.path+img+'"';
	imgStr+=' alt="">';
	document.write(imgStr);
	document.close();
};

ExtImgRotator.prototype.addImages=function(){
	
	var img;
	for(var i=0;arguments[i];i++){
		img=new Image();
		img.src=this.path+arguments[i][0];
		var pos = this.imgs.length;
		this.imgs[pos]=img;
		this.href[pos]=arguments[i][1];
		this.text[pos]=arguments[i][2];
	}
};

ExtImgRotator.prototype.addActions=function(){
	var len=arguments.length;
	for(var i=0;i<len;i++)
		this.actions[this.actions.length]=arguments[i];
};

ExtImgRotator.prototype.rotate=function(){
	clearTimeout(this.timer);
	this.timer=null;
	if (this.doStop == false) {
		var ctr = null;
		if (this.randomOrder) {
			ctr=Math.floor(Math.random()*this.imgs.length);
			var i=0;
			while(ctr==this.ctr&&i<6){
				ctr=Math.floor(Math.random()*this.imgs.length);
				i++;
			}
		}
		else {
			ctr = this.ctr + 1;
			if (ctr == this.imgs.length) {
				ctr = 0;
			}
		}
		this.ctr=ctr;
		var imgObj=document.images[this.name];
		if(!imgObj)
			return;
	
		var newImgObj = this.imgs[this.ctr].src;
		document.getElementById(this.carlink).href=this.href[this.ctr];
		document.getElementById(this.cartext).innerHTML=this.text[this.ctr];
		
		this.speed = Math.floor(Math.random()*(this.maxShowtime - this.minShowtime)) + this.minShowtime;
		if(this.fadeImages) {
			// if(typeof imgObj.filters!="undefined"){
			if(1==0){
				imgObj.style.filter='blendTrans(duration=' + (this.fadeduration/1000) + ')';
				if(imgObj.filters.blendTrans)
					imgObj.filters.blendTrans.Apply();
					
				imgObj.src = newImgObj;
				
				if(imgObj.filters.blendTrans)
					imgObj.filters.blendTrans.Play();
					
				this.timer=setTimeout(this.animString+".rotate()",this.speed - this.fadeduration);
			}
			else {			
				var parent = imgObj.parentNode;
				var newImg = document.createElement("img");
				newImg.name = this.name+'-new';
				newImg.src = newImgObj;
				newImg.style.opacity = 0.01;
				parent.insertBefore(newImg, imgObj.nextSibling);
				var timeout = this.fadeduration / this.fadeSteps;
				var newOpacity = 100 / this.fadeSteps;
				this.fading = true;
				this.timer = setTimeout(this.animString+'.fade(' + newOpacity + ')',timeout);
			}
		}
		else {
			imgObj.src = newImgObj;
			this.timer=setTimeout(this.animString+".rotate()",this.speed);
		}
	}
};

ExtImgRotator.prototype.fade=function(newOpacity){
	var newImgObj=document.images[this.name+'-new'];
	var curOpacity = newImgObj.style.opacity;
	if (curOpacity < 1) {
		newImgObj.style.opacity = newOpacity / 100;
		
		var timeout = this.fadeduration / this.fadeSteps;
		newOpacity += 100 / this.fadeSteps;
		this.timer = setTimeout(this.animString+'.fade('+(newOpacity)+')',timeout);
	}
	else {
		var oldImgObj = document.images[this.name];
		var parent = oldImgObj.parentNode;
		parent.removeChild(oldImgObj);
		newImgObj.name = this.name;
		this.fading = false;
		
		this.timer=setTimeout(this.animString+".rotate()",this.speed - this.fadeduration);
	}
};

ExtImgRotator.showOverlay=function(id){
	var len=ExtImgRotator.col.length,obj;
	for(var i=0;i<len;i++){
		obj=ExtImgRotator.col[i];
		if(obj&&obj.id == id) {
			obj.doStop = true;
			
			if (document.images[obj.name+'-new'] != document.images[obj.name+'-bestaatniet']) {
				var newImgObj=document.images[obj.name+'-new'];
				var oldImgObj = document.images[obj.name];
				var parent = oldImgObj.parentNode;
				parent.removeChild(oldImgObj);
				newImgObj.style.opacity = 1;
				newImgObj.name = obj.name;
				obj.fading = false;
				clearTimeout(obj.timer);
			}
		
			document.getElementById('caroverlay'+id).style.display='block';
		}
	}
}

ExtImgRotator.hideOverlay=function(id) {
	var len=ExtImgRotator.col.length,obj;
	for(var i=0;i<len;i++){
		obj=ExtImgRotator.col[i];
		if(obj&&obj.id == id) {
			document.getElementById('caroverlay'+id).style.display='none';
			obj.doStop = false;
			obj.timer=setTimeout(obj.animString+".rotate()",100);
		}
	}
}

ExtImgRotator.start=function(){
	var len=ExtImgRotator.col.length,obj;
	for(var i=0;i<len;i++){
		obj=ExtImgRotator.col[i];
		if(obj&&obj.name) {
			document.getElementById(obj.carlink).href=obj.href[obj.ctr];
			document.getElementById(obj.cartext).innerHTML=obj.text[obj.ctr];
			obj.timer=setTimeout(obj.animString+".rotate()",obj.speed);
		}
	}
};


