if (!slideshow) {
	var slideshow = new Object();
}

slideshow = {
	/**
	 * slideshow variables
	 */
	fadeTime : 5000, // in milliseconds

	imagesUrlArray : new Array(),
	imagesArray : new Array(),
	currentImage : 0,

	/**
	 * fade between images
	 */
	fade : function() {
		// set the image we'll show now
		if (slideshow.currentImage < slideshow.imagesArray.length - 1) slideshow.currentImage++;
		else slideshow.currentImage = 0;

		if (slideshow.imagesArray[slideshow.currentImage]) {
			if (slideshow.imagesArray[slideshow.currentImage].complete) {
				// prepare new image in the back
				$('#headerSlideshowOrig').attr('src', slideshow.imagesUrlArray[slideshow.currentImage]);

				// fade out current img
				$('#headerSlideshowAlt').fadeOut('slow',
					// move new image to the front and make that img visible again
					function() {
						$('#headerSlideshowAlt').attr('src', $('#headerSlideshowOrig').attr('src'));
						setTimeout("$('#headerSlideshowAlt').show()", slideshow.fadeTime / 2);
					}
				);

				// let's have our next image in <fadeTime>
				setTimeout(slideshow.fade, slideshow.fadeTime);
			} else {
				// img not yet loaded, try again in 10ms
				setTimeout(slideshow.fade, 10);
			}
		}
	},

	/**
	 * IE fixes for this project: elements that should overlay the slideshow (z-index) do not do this (IE z-index bug creates new stacking order), now they will!
	 */
	ieFix : function() {
		// check if IE
		if (!$.support.leadingWhitespace) {
			$('body > *').each(function(elm) {
				// don't copy slideshow
				if ($(this).attr('id') != 'headerSlideshowAlt' && $(this).attr('id') != 'headerSlideshowOrig') {
					var offset = $(this).offset();
					// create new element on the same position
					$(this).clone().appendTo('body').css('position', 'absolute').css('left', offset.left).css('top', offset.top).css('z-index', '10');
					// hide old one and prepare for removal (do not change id yet, positioning of elements may depend on css applied to an id)
					$(this).attr('class', 'to_be_removed').css('visibility', 'hidden');
				}
			});
			// it's all been positioned, remove the old ones now
			$('.to_be_removed').each(function(elm) {
				$(this).remove();
			});
		}
	},

	/**
	 * initialise slideshow
	 */
	init : function() {
		slideshow.imagesUrlArray = $('#headerSlideshow').attr('rel').split(',');
		slideshow.preload();

		// create second image, to overlay (smooth transitions)
		var position = $('#headerSlideshow').offset();
		$('body').append('<img id="headerSlideshowAlt" src="' + $('#headerSlideshow').attr('src') + '" alt="" />');
		$('#headerSlideshowAlt').css('position', 'absolute').css('left', position.left).css('top', position.top).css('padding-left', '30px').css('z-index', '1');
		// and let's not use the original one, just to be sure of the positions
		$('body').append('<img id="headerSlideshowOrig" src="' + $('#headerSlideshow').attr('src') + '" alt="" />');
		$('#headerSlideshowOrig').css('position', 'absolute').css('left', position.left).css('top', position.top).css('padding-left', '30px');
		// now make the original one invisible
		$('#headerSlideshow').css('visibility', 'hidden');

		slideshow.ieFix();
		setTimeout(slideshow.fade, slideshow.fadeTime);
	},

	/**
	 * preload images
	 */
	preload : function() {
		// if this is possible in the current browser
		if (document.images)
		{
			for (var i = 0; i < slideshow.imagesUrlArray.length; i++) {
				slideshow.imagesArray[i] = new Image();
				slideshow.imagesArray[i].src = slideshow.imagesUrlArray[i];
			}
		}
	}
};

$(document).ready(function() { slideshow.init(); });
