function createGallery(containerId)
{
	this.container;
	this.len;
	this.imgs;
	this.stopDelay = 5000;
	this.animationOpacityDecrease = 0.05;
	this.animationSpeed = 10;
	this.activeImgIndex = 0;
	this.currentImg;
	this.nextImg;
	this.i;
	
	container = window.document.getElementById(containerId);
	if(container)
	{
		imgs = container.getElementsByTagName('img');
		len = imgs.length;
		i = len;
		
		while(i--){ // INIT IMG STYLES
			imgs[i].style.opacity = 0;
			imgs[i].style.filter = 'alpha(opacity=0)';
		}
		
		imgs[0].style.display = 'block';
		imgs[0].style.zIndex = 2;
		imgs[0].style.opacity = 1;
		imgs[0].style.filter = 'alpha(opacity=100)';
		startGalleryTransition();
	}
}

function startGalleryTransition(readyState, delayMode)
{
	if(!readyState)
	{
		if(_pageNotLoaded)
		{
			setTimeout("startGalleryTransition()", 500);
		} else {
			startGalleryTransition(true);
		}
	} else {
		if(!delayMode)
		{
			setTimeout("startGalleryTransition(true, true)", this.stopDelay);
		} else { // FINALLY START ANIMATING
			var nextImg = (this.activeImgIndex+1);
			if(nextImg >= this.len) nextImg = 0;
			this.currentImg = this.imgs[this.activeImgIndex];
			this.nextImg = this.imgs[nextImg];
			fadeOut();
		}
	}
}

function fadeOut(opacity)
{
	if(!opacity)
	{
		opacity = 1;
		this.nextImg.style.display = 'block';
		this.nextImg.style.opacity = 1;
		this.nextImg.style.filter = 'alpha(opacity=100)';
	}
	opacity -= this.animationOpacityDecrease;
	if(opacity < 0)
	{
		this.currentImg.style.opacity = 0;
		this.currentImg.style.filter = 'alpha(opacity=0)';
		this.currentImg.style.zIndex = 1;
		this.nextImg.style.zIndex = 2;
		this.activeImgIndex = (this.activeImgIndex+1);
		if(this.activeImgIndex >= this.len) this.activeImgIndex = 0;
		startGalleryTransition(true);
		return false;
	}
	this.currentImg.style.opacity = opacity;
	var ieOpacity = (opacity * 100);
	this.currentImg.style.filter = 'alpha(opacity=' + ieOpacity + ')';
	setTimeout("fadeOut("+opacity+")", this.animationSpeed);
}

function isPageReady()
{
	if(document.readyState == "loaded" || document.readyState == "complete")
	{
		clearInterval(_pageLoadTimer);
		_pageNotLoaded = false;
	}
}

var sUserAgent = navigator.userAgent;
var isIE = (sUserAgent.indexOf("MSIE") > -1);

var _pageNotLoaded = true;
var _pageLoadTimer = setInterval(isPageReady, 100);

createGallery('slideshow');
