(function() {

	// set up commonly used functions	
	var addEvent = function(obj, eventName, observer) { window.addEventListener ? obj.addEventListener(eventName, observer, false) : obj.attachEvent('on' + eventName, observer); }
	var $ = function(id) { return document.getElementById(id); }

	// 
	addEvent(window, 'load', so_init); 
	
	var imgs = new Array(), current = 0, timer = null;
	
	function so_init()
	{
		imgs = $('slideshow-container').getElementsByTagName('li');
		for(i=1; i<imgs.length; i++) 
		{
			imgs[i].xOpacity = 0;
		}
		imgs[0].style.display = "block";
		imgs[0].xOpacity = .999999;
				
		timer = setTimeout(so_xfade, 4500);

		// set up event handlers to handle pause and unpause when the mouse is moved over the image
		addEvent($('slideshow'), 'mouseover', so_pause); 
		addEvent($('slideshow'), 'mouseout', function () { if (timer) { return; } timer = setTimeout(so_xfade, 1000); }); 
		
		// set up next/prev buttons
		addEvent($('ssb-next'), 'click', so_showNext);
		addEvent($('ssb-prev'), 'click', so_showPrev);
	}

	function so_pause()
	{
		// stop the timer
		clearTimeout(timer); 
		timer = 0;
		
		// make the more opaque image the only image shown

		next = imgs[current + 1] ? current + 1 : 0; 
		
		// if the current image is more opaque, stay on the current image
		if (imgs[current].xOpacity > imgs[next].xOpacity)
		{
			imgs[next].xOpacity = 0;
			imgs[next].style.display = 'none';
			
			imgs[current].xOpacity = 0.999999; 

			setOpacity(imgs[current]);
			setOpacity(imgs[next]);
		}
		else
		{
			imgs[current].xOpacity = 0;
			setOpacity(imgs[current]);
			imgs[current].style.display = 'none';
			
			current = next;
			
			imgs[current].xOpacity = 0.999999; 
			setOpacity(imgs[current]);
		}
	}
	function highlightNextButton()
	{
		try 
		{
			$('ssb-next').style.backgroundPosition = '100% 100%';
			setTimeout(fixPagingBackgrounds, 500); 
		}
		catch(err)
		{
		}		
	}
	function highlightPrevButton()
	{
		try
		{
			$('ssb-prev').style.backgroundPosition = '0 100%';
			setTimeout(fixPagingBackgrounds, 500); 
		}
		catch(err)
		{
		}
	}	
	function fixPagingBackgrounds()
	{
		try 
		{
			$('ssb-prev').style.backgroundPosition = '0 0';
			$('ssb-next').style.backgroundPosition = '100% 0';
		}
		catch(err)
		{
		}
	}
	function so_showNext()
	{
		highlightNextButton();
		
		next = imgs[current + 1] ? current + 1 : 0;
		so_show(next); 
	}
	function so_showPrev()
	{
		highlightPrevButton();
		
		next = imgs[current - 1] ? current - 1 : imgs.length - 1;
		so_show(next); 
	}
	function so_show(idx)
	{
		imgs[current].xOpacity = 0;
		setOpacity(imgs[current]);
		imgs[current].style.display = 'none';
		
		current = idx;
		
		imgs[current].style.display = 'block';
		imgs[current].xOpacity = 0.999999;
		setOpacity(imgs[current]);
	}
	function so_xfade(disable) 
	{
		cOpacity = imgs[current].xOpacity;
		nIndex = imgs[current+1]?current+1:0;
	
		nOpacity = imgs[nIndex].xOpacity;
		
		cOpacity-=.05; 
		nOpacity+=.05;

		// this is the start of a new fade in cycle		
		if (imgs[nIndex].style.display == 'none')
		{
			highlightNextButton();
			imgs[nIndex].style.display = "block";
		}
		
		imgs[current].xOpacity = cOpacity;
		imgs[nIndex].xOpacity = nOpacity;
		
		setOpacity(imgs[current]); 
		setOpacity(imgs[nIndex]);
		
		if (cOpacity <= 0) 
		{
			imgs[current].style.display = "none";
			current = nIndex;
			timer = setTimeout(so_xfade, 4500);
		} 
		else 
		{
			timer = setTimeout(so_xfade,50);
		}
	}
	function setOpacity(obj) 
	{
		if(obj.xOpacity > 0.999999) 
		{
			obj.xOpacity = 0.999999;
			return;
		}
		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = "alpha(opacity=" + (obj.xOpacity * 100) + ")";
	}
	
})();