// Slideshow
// Please credit me and the relevant authors of extensions used in this code if you decide to use the menu for your own site.
// by Greg Poole | greg@webengine.com.au | www.webengine.com.au

var SLIDESHOW_STATE_FADEOUT = 1;
var SLIDESHOW_STATE_STATIC = 2;
var SLIDESHOW_UPDATE_FREQ = 10;
var SLIDESHOW_FADE_SPEED = 0.05;
var browserName = getBrowser();
var SLIDESHOW_SPEED = 5000;

function Slideshow(objContainer) {
	
	this.images = new Array();
	this.p_Images = 0;
	this.currentImage = 0;
	this.container = document.getElementById(objContainer);
	this.display = this.container.childNodes[0];
	this.displayState = SLIDESHOW_STATE_STATIC;
	this.timer = new Timer(this);
	this.fadeTimeout = -1;
	this.fadeOpacity = 1.0;
	this.slideshowTimeout = -1;
	this.playing = false;
	
	this.add = function(src) {
		var pointer = this.p_Images++;
		this.images[pointer] = new Image();
		this.images[pointer].src = src;
	}
	
	this.next = function() {
		var newImage = this.currentImage + 1;
		
		if(newImage >= this.images.length)
			newImage = 0;
		
		this.show(newImage);
	}
	
	this.previous = function() {
		var newImage = this.currentImage - 1;
		
		if(newImage < 0)
			newImage = this.images.length - 1;
		
		this.show(newImage);
	}
	
	this.start = function() {
		this.slideshowUpdate();
		this.playing = true;
	}
	
	this.stop = function() {
		this.timer.clearTimeout(this.slideshowTimeout);
		this.playing = false;
	}
	
	this.slideshowUpdate = function() {
		this.slideshowTimeout = this.timer.setTimeout("slideshowUpdate", SLIDESHOW_SPEED);
		this.next();
	}
	
	this.show = function(index) {
		if(this.displayState != SLIDESHOW_STATE_STATIC || index < 0 || index >= this.images.length)
			return;
		
		var oldImage = this.currentImage;
		this.currentImage = index;
		
		this.fadeOpacity = 1;
		this.displayState = SLIDESHOW_STATE_FADEOUT;
		this.container.style.backgroundImage = "url(" + this.images[this.currentImage].src + ")";
		this.fadeTimeout = this.timer.setTimeout("update", SLIDESHOW_UPDATE_FREQ);
	}
	
	this.update = function() {
		if(this.displayState == SLIDESHOW_STATE_STATIC) {
			this.timer.clearTimeout(this.fadeTimeout);
			return;
		}
		
		if(this.displayState == SLIDESHOW_STATE_FADEOUT) {
			if(this.fadeOpacity - SLIDESHOW_FADE_SPEED <= 0) {
				this.fadeOpacity = 1;
				this.displayState = SLIDESHOW_STATE_STATIC;
				this.display.src = this.images[this.currentImage].src;
			} else {
				this.fadeOpacity -= SLIDESHOW_FADE_SPEED;
			}
			
			if(browserName == "Firefox" || browserName == "Mozilla" || browserName == "Netscape")
				this.display.style.MozOpacity = this.fadeOpacity;
			else if(browserName == "Internet Explorer")
				this.display.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + (this.fadeOpacity * 100) + ")";
			else if(browserName == "Opera" || browserName == "Safari")
				this.display.style.opacity = this.fadeOpacity;
		}
		
		this.fadeTimeout = this.timer.setTimeout("update", SLIDESHOW_UPDATE_FREQ);
	}
}