//
// Home Image Rotator
//

function HomeImageRotator(instanceName)
{
	////////////////////////
	// Properties
	////////////////////////
	this.instanceName = instanceName;
	this.images = new Array();
	this.interval = 5000;
	this.currentImage = 0;
	this.timer = null;
	this.baseHref = '';
	this.currentAlpha = 100;
	this.fadeInterval = 50;
	this.fadeStep = 5;
	this.oldImageUrl = '';

	this.topImage = null;
	this.btmImage = null;
	this.title = null;
	this.content = null;
	this.link = null;

	////////////////////////
	// Methods
	////////////////////////
	this.add = function(id, title, content, link)
	{
		this.images.push
		(
			{
				'id' 		: id,
				'title'		: title,
				'content' 	: content,
				'link'		: link,
				'cache'		: null
			}
		);
	}

	this.getElement = function(elementId)
	{
		if (document.getElementById(elementId))
		{
			return document.getElementById(elementId);
		}
		alert('Could not locate element with id "'+elementId+'"');
		return false;
	}

	// Init rotator
	this.init = function()
	{
		this.topImage 	= this.getElement('topImage');
		this.btmImage 	= this.getElement('btmImage');
		this.title 		= this.getElement('rotatorTitle');
		this.content 	= this.getElement('rotatorContent');
		this.link 		= this.getElement('rotatorLink');
		this.play();
	}

	// Begin/Resume rotation
	this.play = function()
	{
		var instance = this;
		this.timer = setInterval
		(
			function()
			{
				instance.nextImage();
			},
			this.interval
		);
		delete(instance);
	}

	// Pause rotation
	this.pause = function()
	{
		clearInterval(this.timer);
		this.timer = null;
	}

	// Advance to next image
	this.nextImage = function()
	{
		this.currentImage++;
		if (this.currentImage == this.images.length)
		{
			this.currentImage = 0;
		}

		var image = this.images[this.currentImage];
		var imageUrl = this.baseHref+'/images/display.php?id='+image.id+'&width=800&height=320';

		var instance = this;
		this.btmImage.onload = function()
		{
			instance.fade();
		}
		delete(instance);
		this.btmImage.src = imageUrl;
		return true;
	}

	// Fade top image out
	this.fade = function()
	{
		this.currentAlpha = this.currentAlpha - this.fadeStep;
		if (this.currentAlpha < 0)
		{
			this.fadeComplete();
		}
		else
		{
			this.setAlpha(this.topImage, this.currentAlpha);
			var instance = this;
			setTimeout(function()
			{
				instance.fade();
			},
			this.fadeInterval);
			delete(instance);
		}
	}

	this.fadeComplete = function()
	{
		this.currentAlpha = 100;
		this.topImage.src = this.btmImage.src;

		var image 				= this.images[this.currentImage];
		this.title.innerHTML 	= image.title;
		this.content.innerHTML 	= image.content;
		this.link.href 			= image.link;
		delete(image);

		var instance = this;
		this.topImage.onload = function()
		{
			instance.setAlpha(instance.topImage, 100);
		}
		delete(instance);
	}

	this.setAlpha = function(object, value)
	{
		object.style.opacity = value/100;
		object.style.MozOpacity= value + '%';
		object.style.filter = 'alpha(opacity=' + value + ')';
	}
}
