// Image Rotator
var rotators = new Object();

function getRotator(id)
{
	return rotators[id];
}

var isIE = false;

try
{
	new ActiveXObject("Msxml2.XMLHTTP");
	isIE = true;
}catch(e)
{
	isIE = false;
}

var imageRotators = 0;

function ImageRotator(w, h, seconds, contentView, border)
{
	if(_ie != 6)
		 border = 0;
	
	w += border;
	h+= border;
	
	this.id = "ir"+imageRotators++;
	
	rotators[this.id] = this;
	
	if(!contentView)
	{
		contentView = "contentView";
	}
	
	this.contentView = document.getElementById(contentView);
	this.contentView.style.position = "relative";
	this.contentView.style.overflow = "hidden";
	
	this.caption = document.getElementById("caption");
	
	this.contentView.style.width = w+"px";
	this.contentView.style.height = h+"px";
	this.displays = new Array();
	this.timerSpeed = seconds;
	this.speed = 2;
	var currentDisplay = 0;
	var tweenId;
	var tweenId2;
	var endPos;
	var nextEndPos;
    var opacity = 100;
	var nextOpacity = 0;
	var action;
	var obj;
	var nextObj;
	var rotateId;
	var waitId;
	
	this.addDisplay = function(img, caption)
	{
		this.displays.push(new Display(img, caption));
	}
	
	this.next = function()
	{
		obj = this.displays[currentDisplay];
		if(++currentDisplay > this.displays.length-1)
		{
			currentDisplay = 0;
		}
		nextObj = this.displays[currentDisplay];
		
		action = "next";
		fadeOut.call(this);
		fadeIn.call(this);
	}
	
	this.prev = function()
	{
		obj = this.displays[currentDisplay];
		if(--currentDisplay < 0)
			currentDisplay = this.display.length - 1;
		nextObj = this.displays[currentDisplay];
	
		action = "prev";
		fadeOut.call(this);
	}
	
	function fadeOut()
	{
		clearInterval(tweenId);
		endPos = 0;
		opacity = 100;
		tweenId = setInterval("getRotator('"+this.id+"').fadeOutTween()", 25);
	}
	
	function fadeIn()
	{
		clearInterval(tweenId2);
		display.call(this,nextObj);
		nextEndPos = 100;
		nextOpacity = 0;
		tweenId2 = setInterval("getRotator('"+this.id+"').fadeInTween()", 25);
	}
	
	this.fadeOutTween = function()
	{
		if(opacity == endPos)
		{
			obj.view.style.opacity = opacity/100;
			clearInterval(tweenId);
			this.contentView.removeChild(obj.view);
			opacity = Math.ceil(obj.view.style.opacity)*100;
			return;
		}
		
		opacity -= this.speed;
		setOpacity(obj, opacity);
	}
	
	this.fadeInTween = function()
	{
		if(nextOpacity == nextEndPos)
		{
			nextObj.view.style.opacity = nextOpacity/100;
			clearInterval(tweenId2);
			return;
		}
		nextOpacity += this.speed;
		setOpacity(nextObj, nextOpacity);
	}
	
	function setOpacity(obj, opacity)
	{
		if(isIE)
		{
			obj.view.style.zoom = "1";
			obj.view.style.filter="alpha(opacity="+String(opacity)+")";
		}
		else
			obj.view.style.opacity = opacity/100;
	}
	
	function display(displayItem)
	{
		this.contentView.appendChild(displayItem.view);
		if(displayItem.captionText)
			this.caption.innerHTML = displayItem.captionText;
	}
	
	this.addImages = function(images, tags)
	{
		for(var i = 0; i < images.length; i++)
		{
			this.addDisplay(images[i]);
			
			if(tags)
			{
				this.displays[i].view.alt = tags[i];
				this.displays[i].view.title = tags[i];
			}
		}
	}
	
	this.addDescriptions = function(desc)
	{
		for(var i = 0; i < desc.length; i++)
		{
			this.displays[i].captionText = desc[i];
		}
	}
	
	this.init = function()
	{
		for(var i = 0; i < this.displays.length; i++)
		{
			var displayObj = this.displays[i].view;
			displayObj.style.position = "absolute";
			//displayObj.style.left = (w/2) - (displayObj.width/2) + "px";
			//displayObj.style.top = (h/2) - (displayObj.height/2) + "px";
			setOpacity(this.displays[i], 0);
		}
		
		display.call(this, this.displays[currentDisplay]);
		nextObj = this.displays[currentDisplay];
		fadeIn.call(this);
		this.startTimer();
	}
	
	this.startTimer = function()
	{
		if(this.displays.length < 2)
		{
			if(this.displays.length == 1)
			{
				setOpacity(this.displays[0], 100);
				display(this.displays[0]);
			}
			return;
		}
		clearInterval(waitId);
		rotateId = setInterval("getRotator('"+this.id+"').next()", this.timerSpeed*1000);
	}
	
	this.stopTimer = function()
	{
		clearInterval(rotateId);
	}
	
	this.stopAt = function(i)
	{
		if(this.displays[i])
		{
			this.stopTimer();
			clearInterval(tweenId);
			clearInterval(tweenId2);
			
			clearInterval(waitId);
			
			waitId = setInterval("getRotator('"+this.id+"').startTimer()", 2000);	
			obj = this.displays[currentDisplay];
			nextObj = this.displays[i];
			currentDisplay = i;
			//action = "next";
			fadeOut.call(this);
			fadeIn.call(this);
		}
	}
}

function Display(img, captionText)
{
	this.img = img;
	if(captionText)
		this.captionText = captionText;
	
	this.view = document.createElement("IMG");
	this.view.src = img;
}
