var Slideshow =
{
	init: function()
	{
		Slideshow.beginCycle();
		Slideshow.ActiveImage = $('slidePlaceholder').down('img');
		Slideshow.ActiveButton = $('slideButtons').down('img');
		Slideshow.FirstLoad = true;
		Slideshow.Lock = false;
		Slideshow.ActiveCycle = true;
		
		Slideshow.ActiveImage.onclick = function()
		{
			location.href = Slideshow.ActiveImage.getAttribute('alt');
		}
		
		var iChildIndex = 0;
		var pChild = $('slideButtons').down('img');
		
		while(pChild)
		{
			pChild = pChild.next('img');
			iChildIndex++;
		}
		
		$('buttonRestart').onclick = function()
		{
			Slideshow.beginCycle();
		}
		
		Slideshow.Children = (iChildIndex - 1);
		
		var iChildNodes = $('slideButtons').childNodes.length;
		
		for(iIndex = 0; iIndex < iChildNodes; iIndex++)
		{
			var pChild = $('slideButtons').childNodes[iIndex];
			
			if(pChild.nodeName.toLowerCase() != 'img')
			{
				continue;
			}
			
			pChild.onclick = function()
			{
				Slideshow.endCycle();
				Slideshow.switchImage(this);
			}
		}
		
		var pButtonLeft = $('slideButtonLeft').down('img');
		
		pButtonLeft.onclick = function()
		{
			Slideshow.switchImage(Slideshow.getPreviousImage());
			Slideshow.endCycle();
		}
		
		pButtonLeft.onmouseover = function()
		{
			var szFilename = pButtonLeft.getAttribute('src').replace('default', 'active');
			pButtonLeft.setAttribute('src', szFilename);
		}
		
		pButtonLeft.onmouseout = function()
		{
			var szFilename = pButtonLeft.getAttribute('src').replace('active', 'default');
			pButtonLeft.setAttribute('src', szFilename);
		}
		
		var pButtonRight = $('slideButtonRight').down('img');
		
		pButtonRight.onclick = function()
		{
			Slideshow.switchImage(Slideshow.getNextImage());
			Slideshow.endCycle();
		}
		
		pButtonRight.onmouseover = function()
		{
			var szFilename = pButtonRight.getAttribute('src').replace('default', 'active');
			pButtonRight.setAttribute('src', szFilename);
		}
		
		pButtonRight.onmouseout = function()
		{
			var szFilename = pButtonRight.getAttribute('src').replace('active', 'default');
			pButtonRight.setAttribute('src', szFilename);
		}
	},
	
	beginCycle: function()
	{
		if(Slideshow.ActiveCycle == true)
		{
			Slideshow.endCycle();
			return;
		}
		
		Slideshow.ActiveCycle = true;
		
		if(Slideshow.FirstLoad)
		{
			Slideshow.FirstLoad = false;
			Slideshow.Cycle = setInterval(function()
			{
				Slideshow.switchImage(Slideshow.getNextImage());
				Slideshow.endCycle();
				Slideshow.beginCycle();
			}, 	3500);
		}
		else
		{
			Slideshow.Cycle = setInterval(function()
			{
				Slideshow.switchImage(Slideshow.getNextImage());
			}, 	2500);
		}
	},
	
	endCycle: function()
	{
		Slideshow.ActiveCycle = false;
		clearInterval(Slideshow.Cycle);
	},
	
	getPreviousImage: function()
	{
		var iImageIndex = 0;
		var pChild = $('slideButtons').down('img');
		
		while(pChild != Slideshow.ActiveButton)
		{
			pChild = pChild.next('img');
			iImageIndex++;
		}
		
		if(iImageIndex == 0)
		{
			iPrevIndex = Slideshow.Children;
		}
		else
		{
			iPrevIndex = (iImageIndex - 1);
		}
		
		var iChildIndex = 0;
		var pChild = $('slideButtons').down('img');
		
		while(iChildIndex != iPrevIndex)
		{
			pChild = pChild.next('img');
			iChildIndex++;
		}
		
		return pChild;
	},
	
	getNextImage: function()
	{
		var iImageIndex = 0;
		var pChild = $('slideButtons').down('img');
		
		while(pChild != Slideshow.ActiveButton)
		{
			pChild = pChild.next('img');
			iImageIndex++;
		}
		
		if(iImageIndex == Slideshow.Children)
		{
			iNextIndex = 0;
		}
		else
		{
			iNextIndex = (iImageIndex + 1);
		}
		
		var iChildIndex = 0;
		var pChild = $('slideButtons').down('img');
		
		while(iChildIndex != iNextIndex)
		{
			pChild = pChild.next('img');
			iChildIndex++;
		}
		
		return pChild;
	},
	
	switchImage: function(pImage)
	{
		if(Slideshow.Lock == true)
		{
			return;	
		}
		
		Slideshow.Lock = true;
		var iButtonIndex = 0;
		var pChild = $('slideButtons').down();
		
		if(pChild != pImage)
		{
			do
			{
				pChild = pChild.next('img');
				iButtonIndex++;
			}
			while(pChild != pImage);
		}
		
		var iImageIndex = 0;
		var pChild = $('slidePlaceholder').down('img');
		
		while(iImageIndex != iButtonIndex)
		{
			pChild = pChild.next('img');
			
			if(!pChild)
			{
				return;
			}
			
			iImageIndex++;
		}
		
		var pFadeOptions =
		{
			duration: 1,
			to: 0,
			afterFinish: function()
			{
				setTimeout(function() { Slideshow.showImage(pChild, iButtonIndex) }, 500);
			}
		}
		
		new Effect.Fade(Slideshow.ActiveImage, pFadeOptions);
	},
	
	showImage: function(pImage, iButtonIndex)
	{
		var pAppearOptions =
		{
			duration: 2,
			to: 1,
			afterFinish: function()
			{
				Slideshow.Lock = false;
			}
		}
		
		var pButton = $('slideButtons').down('img');
		var iIndex = 0;
		
		while(iIndex != iButtonIndex)
		{
			pButton = pButton.next('img');
			iIndex++;
		}
		
		var szFilename = pButton.getAttribute('src').replace('default', 'active');
		pButton.setAttribute('src', szFilename);
		
		var szFilename = Slideshow.ActiveButton.getAttribute('src').replace('active', 'default');
		Slideshow.ActiveButton.setAttribute('src', szFilename);
		
		Slideshow.ActiveButton = pButton;
		Slideshow.ActiveImage = pImage;
		
		pImage.show();
		pImage.setOpacity(0.01);
		
		pImage.onclick = function()
		{
			location.href = pImage.getAttribute('alt');
		}
		
		new Effect.Fade(pImage, pAppearOptions);
	}
}

Event.observe(window, 'load', Slideshow.init, false);
