/* ==========================================
 *	Javascript Image Rotator
 *
 *	2009 Mark Treble Interactive Media
 *
 * ==========================================
 */

function CImageRotator(name, imgArr){
	var d = document;
	this.currentImage=0;
	this.name = name;
	this.id = name;
	this.images = imgArr;
	eval(this.name+"=this");
	this.img = this.name;
	this.autoMode = true;
	this.setUp();


	this.pauseTime = 3000; 		// Pause time in (ms)
	this.transitionTime = 500;	// Duration for fade transition (ms)
}

CImageRotator.prototype.setUp=function(){

	var d=document;
	
	this.HCurrent=d.getElementById(this.img);
	if (!this.HCurrent){
		window.setTimeout(this.name+".setUp()", 100);
	} else {
		this.HCurrent.style.position='absolute';
		this.HCurrent.style.top='0';
		this.images[0]=this.HCurrent;
		for (i=1;i<this.images.length;i++){
			im = d.createElement('img');
			im.setAttribute('src',this.images[i]);
			im.setAttribute('style','position:absolute;top:0;display:none;z-Index:-'+i.toString());
			im.setAttribute('width',this.HCurrent.width+'px');
			im.setAttribute('height',this.HCurrent.height+'px');
			this.images[i-1].parentNode.insertBefore(im,this.images[i-1]);
			this.images[i]=im;
			delete im;
			this.timer=window.setTimeout(this.name + ".animateNext()", this.pauseTime);
		}
	}
}
CImageRotator.prototype.registerButtons =function(btns){
	this.buttons = btns;
}
CImageRotator.prototype.animateNext = function(){
	clearTimeout(this.timer);
	clearInterval(this.timer);
	this.HOld = this.HCurrent;
	this.buttons.buttons[this.currentImage].id='';
	this.currentImage=(this.currentImage > this.images.length-2)? 0 : this.currentImage+1
	this.buttons.buttons[this.currentImage].id="active";
	this.HOld.style.zIndex='-1';
	this.HCurrent=this.images[this.currentImage];
	this.HCurrent.style.zIndex='0';
	this.HCurrent.style.filter='alpha(opacity=0)';
	this.HCurrent.style.opacity='0';
	this.HCurrent.style.display='block';
	this.alpha = 0;
	this.animating = true;
	this.timer=window.setInterval(this.name + ".fadeIn()", 1000/30);
}
CImageRotator.prototype.animateRandom = function(n){
	this.autoMode = false;
	if (this.animating==true){ this.queue = n; }
	clearTimeout(this.timer);
	clearInterval(this.timer);
	this.HOld = this.HCurrent;
	this.buttons.buttons[this.currentImage].id="";
	this.currentImage=parseInt(n)-1;
	this.buttons.buttons[this.currentImage].id="active";
	this.HOld.style.zIndex='-1';
	this.HCurrent=this.images[this.currentImage];
	this.HCurrent.style.filter='alpha(opacity=0)';
	this.HCurrent.style.opacity='0';
	this.HCurrent.style.zIndex='0';
	this.HCurrent.style.display='block';
	this.alpha = 0;
	this.animating=true;
	this.timer=window.setInterval(this.name + ".fadeIn()", 1000/30);

}
CImageRotator.prototype.fadeIn = function(){
	this.alpha += (1/30) *(1000/this.transitionTime);
	this.HCurrent.style.filter='alpha(opacity='+(this.alpha*100)+')';
	this.HCurrent.style.opacity=this.alpha;
	if (this.alpha>=1){
		clearInterval(this.timer);
		this.HOld.style.display='none';
		if (this.autoMode == true)
			this.timer=window.setTimeout(this.name + ".animateNext()", this.pauseTime);
		this.animating = false;
		if (this.queue != null){
			this.animateRandom(this.queue);
			this.queue = null;
		}
	}
}

function CImageRotatorNav(name, associate){
	this.name = name;
	this.id = name;
	this.associate = associate;
	this.buttons = [];
	this.associate.registerButtons(this);
	eval(this.name+"=this");
	this.setUp();
}

CImageRotatorNav.prototype.setUp = function(){
	var d=document;
	this.Hul = d.getElementById(this.name);
	if (!this.Hul){
		window.setTimeout(this.name + ".setUp()", 100);
	} else {
		for (i=0; i<this.associate.images.length;i++){
			el_li = d.createElement("li");
			el_a = d.createElement("a");
			el_a.setAttribute('href', "#");
			el_li.appendChild(el_a);
			this.Hul.appendChild(el_li);
			el_a.name= i+1;
			this.buttons[i]=el_a;
			this.buttons[i].associate = this.associate;
			this.buttons[i].onclick = function(){
				this.associate.animateRandom(this.name);
				return false;
			}
		}
		this.buttons[0].id="active";
	}
}
	
