/*!
 * Slideshow of images tool
 * Written by Mark Davies
 * www.markdavies.com.au
 * 
 * Requires jQuery!
 * 
 */


(function (){
	
	var window = this,
		windowName = Math.random(1000).toString().slice(3),
		doc = document,
		loc = location,
		enc = encodeURIComponent,
		now = new Date().getTime(),
		trasitioning = false,
		imageTimeout = 0,
		loadingImage,
		imageIndex = 1,
		slideshow;
	
	// Default settings
	var slideshowOps = {
		// amount of time each image shows before the next one loads
		slideShowTime:4000,
		// amount of time the fade each image in and out
		fadeTime:200,
		// total number of images in the directory
		numberImages: 7,
		// image number to start at
		startImage: 1,
		// prefix to place before the image number, including uri
		imagePrefix: 'img/header_image_',
		// suffix, including file extension
		imageSuffix: '.jpg',
		// div that images will display in
		aimSelector:'#headerImage',
		// debugging
		debug: false
	}
	
	function isUndefined(variable) {
		return typeof variable === 'undefined';
	}
	
	function startSlideShow(){
		
		if(slideshowOps.debug) console.log('slideshow.js: startSlideShow');
		
		// Check that jQuery exists
		if (isUndefined(jQuery)) {
			alert('jQuery object not found!');
			return;
		}
		
		clearTimeout(imageTimeout);
		clearImageLoad();
		
		imageIndex = slideshowOps.startImage -1;
		//imageTimeout = setTimeout(loadNextImage, slideshowOps.slideShowTime);
		loadNextImage();
		
	}

	function clearImageLoad(){
		
		if(slideshowOps.debug) console.log('slideshow.js: clearImageLoad');
		
		$(loadingImage).unbind('load');
		
	}

	function loadNextImage(){
		
		if(slideshowOps.debug) console.log('slideshow.js: loadNextImage');
		
		imageIndex ++;
		if(imageIndex > slideshowOps.numberImages) imageIndex=1;
		
		loadingImage = new Image();
		
		$(loadingImage).load(imageLoadedHandler)
	    .attr('src', slideshowOps.imagePrefix+imageIndex+slideshowOps.imageSuffix);
	    
	    if(slideshowOps.debug) console.log('slideshow.js: '+slideshowOps.imagePrefix+imageIndex+slideshowOps.imageSuffix);
		
	}
	
	function imgTagCount(){
		return $(slideshowOps.aimSelector+' img').size();
	}

	function imageLoadedHandler(){

		if(slideshowOps.debug) console.log('slideshow.js: imageLoadedHandler '+(this == loadingImage));
	    
		if(this != loadingImage) return;
		
		if(imgTagCount() > 0){
			$(slideshowOps.aimSelector+' img').animate(
					{ opacity: 0}, 
				slideshowOps.fadeTime, 
		   		swapImage
			);
		}else{
			swapImage();
		}
		
	}
	
	function swapImage(){

		if(slideshowOps.debug) console.log('slideshow.js: swapImage');
		
		if(imgTagCount() > 0){
			$(slideshowOps.aimSelector+' img').replaceWith(loadingImage);
		}else{
			$(slideshowOps.aimSelector).append(loadingImage);
		}

		$(slideshowOps.aimSelector).animate(
				{ opacity: 0}, .01
		);
		
		$(slideshowOps.aimSelector).animate(
				{ opacity: 1}, 
			slideshowOps.fadeTime, 
	   		function(){
	   			imageTimeout = setTimeout(loadNextImage, 4000);
	   		}
		);
		
	}
	
	// This sets up the actual object
	window.slideshow = slideshow = {
		setOptions: function (options) {
			for (var i in options) {
				if (options.hasOwnProperty(i)) {
					slideshowOps[i] = options[i];
				}
			}
		},
		start: function () {
			startSlideShow();
		}
	};
	
})();


