var slideshowObjects = new Array();
var browser = {
	ie: (document.all && /msie/.test(navigator.appVersion.toLowerCase())) ? true : false,
	ieVersion: (document.all) ? (navigator.appVersion.toLowerCase().replace(/^.*msie ([0-9\.]+).*$/, "$1")) : 0,
	dom: (document.getElementById) ? true : false
};

function Slideshow(id, duration, pause)
{
	if (browser.dom)
	{
	    this.imageObjects = new Array();
	    this.imageLoader = new Array();
	    this.childNodes = new Array();
	    this.id = id;
	    this.duration = duration;
	    this.pause = pause;
	    this.actualImage = 0;
	    this.opacity = 0;
	    this.mainObject = false;
	    this.registered = false;
	    this.timeout = false;
	}
}

Slideshow.prototype.addImage = function(url, alt, emptyTitle)
{
    var image;
	imageEl = document.createElement("img");
	imageEl.src = url;
	imageEl.alt = alt;
	imageEl.style.position = "absolute";
	
	if (emptyTitle)
	    imageEl.title = "";
	else
	    imageEl.title = alt;

	if (this.registered)
		this.setPauseScript(imageEl);
		
    this.imageObjects.push(imageEl);
    this.childNodes.push(imageEl);
    
	image = new Image();
	image.src = imageEl.src;
	this.imageLoader.push(image);

    return;
}

Slideshow.prototype.showImage = function()
{
	if (this.imageObjects.length > 1)
	{
	    var prevImage = this.actualImage;
	    var nextImage = this.actualImage + 2;
		this.actualImage++;
		this.opacity = 0;
		
		if (this.actualImage >= this.imageObjects.length)
		    this.actualImage = 0;
		    
        if (nextImage >= this.imageObjects.length)
		    nextImage = 0;

		if (this.imageObjects.length > 2)
		    this.imageObjects[nextImage].style.display = "none";

		for (var i = 0; i < this.imageLoader.length; i++)
		    if (this.imageLoader[i].src == this.imageObjects[this.actualImage].src)
		    	this.setSize(i);

		this.imageObjects[prevImage].style.zIndex = 0;
		this.imageObjects[this.actualImage].style.zIndex = 1;
		this.imageObjects[this.actualImage].style.display = "block";
		this.animate();
	}

	return;
}

Slideshow.prototype.setPauseScript = function(el)
{
    if (this.pause)
	{
	    el.onmouseover = new Function("slideshowObjects[" + this.registered + "].pauseIt()");
        el.onmouseout = new Function("slideshowObjects[" + this.registered + "].run()");
	}
			
	return;
}

Slideshow.prototype.animate = function()
{
	var el = this.imageObjects[this.actualImage];

    if (this.opacity < 100)
	{
	    this.opacity += 10;
	    
	    if (browser.ie)
			el.style.filter = "alpha(opacity=" + this.opacity + ")";
		else
	    {
			el.style.MozOpacity = this.opacity / 101;
			el.style.KhtmlOpacity = this.opacity / 100;
			el.style.opacity = this.opacity / 101;
        }

	    this.startTimeout("animate", 75);
	}
	else
	    this.startTimeout("showImage");

	return;
}

Slideshow.prototype.startTimeout = function(functionName, duration)
{
	if (duration)
    	this.timeout = window.setTimeout("slideshowObjects[" + this.registered + "]." + functionName + "()", duration);
	else
	    this.timeout = window.setTimeout("slideshowObjects[" + this.registered + "]." + functionName + "()", this.duration);
	    
	return;
}

Slideshow.prototype.run = function()
{
    if (!(this.timeout || this.registered === false))
		this.startTimeout("showImage");

	return;
}

Slideshow.prototype.pauseIt = function()
{
	if (this.timeout)
	    window.clearTimeout(this.timeout);

	this.timeout = false;

	return;
}

Slideshow.prototype.setSize = function(indent)
{
	if (!indent)
	    indent = 0;

    if (this.imageLoader.length > 0 && this.imageLoader[indent].complete)
	{
        this.mainObject.style.width = this.imageLoader[indent].width + "px";
        this.mainObject.style.height = this.imageLoader[indent].height + "px";
	}
	
	return;
}

Slideshow.prototype.startShow = function()
{
    this.mainObject = document.getElementById(this.id);
    
    if (this.mainObject)
    {
	    this.mainObject.style.display = "block";
		this.mainObject.style.position = "absolute";
		var image;
		var defaultImages = this.mainObject.getElementsByTagName("img");

		for (var i = 0; i < defaultImages.length; i++)
		{
		    image = new Image();
			image.src = defaultImages[0].src;
			this.imageLoader.push(image);
		    this.imageObjects.push(defaultImages[i]);
		}
		
		for (i = 0; i < this.childNodes.length; i++)
            this.mainObject.appendChild(this.childNodes[i]);
	}
	else
	    return window.alert("Try to have slideshow in non existing id [" + this.id + "].");

	if (this.registered !== false)
	{
		for (var i = 0; i < this.imageObjects.length; i++)
		{
		    this.imageObjects[i].style.position = "absolute";

			switch (i)
			{
				case 0:
				    this.imageObjects[i].style.zIndex = 1;
				    break;
				    
				case 1:
				    this.imageObjects[i].style.zIndex = 0;
				    break;
				    
				default:
				    this.imageObjects[i].style.display = "none";
				    break;
			}
	    }
	    
	    this.setSize();
	    this.startTimeout("showImage");
    }
    else
        window.alert("Call Slideshow::register first, it will call this method (Slideshow::startShow()).");
    
	return;
}

Slideshow.prototype.register = function()
{
	var bodyEl = document.getElementsByTagName("body")[0];
    this.registered = slideshowObjects.length;
    slideshowObjects.push(this);

	if (browser.dom)
    	window.domready += this.startShow();
    	
	for (var i = 0; i < this.imageObjects.length; i++)
        this.setPauseScript(this.imageObjects[i]);
        
	return;
}

