// slideshow.js

// slideshow
(function($) {
	$.fn.slideshow = function(path, images, settings) {
		var i = 0;
		var size = images.length;
		
		var slideshow = $(this);
		var images = images;
		var image = new Image();
		
		var cycles = 0;
		var pool = new Array();
		
		// settings
		settings = typeof(settings) == 'undefined' ? {} : settings;
		
		// total time of one shown image, given in seconds
		settings.view_duration = typeof(settings.view_duration) == 'number' ? settings.view_duration * 1000 : 5000;
		
		// show images randomly
		settings.view_random = typeof(settings.view_random) == 'boolean' ? settings.view_random : false;
		
		// only show all images once a whole turn
		settings.view_unique = typeof(settings.view_unique) == 'boolean' ? settings.view_unique : false;
		
		// the amount of whole turns, 0 is infinite
		settings.view_limit = typeof(settings.view_limit) == 'number' ? settings.view_limit : 0;
		
		// maximum duration of the slide effect, in milliseconds
		settings.effect_duration = typeof(settings.effect_duration) == 'number' ? settings.effect_duration : 500;
		
		// the type of the slide effect, 'fade', 'slide', 'flip', 'instant'
		settings.effect_type = typeof(settings.effect_type) == 'string' ? settings.effect_type : 'fade';
		
		// slide the photo
		slide = function() {
			if(image.complete) {
				$(slideshow).find('img:first').clone().insertAfter(slideshow.find('img:first')).hide(0);
				$(slideshow).find('img:last').attr('src', image.src);
				$(slideshow).find('img:last').fadeIn(settings.effect_duration, function() {
					$(slideshow).find('img:first').remove();
				});
				
				i = getNextId();
				image.src = path + images[i];
			} else {
				//alert('Image #' + i + ' seems to be to large to load.');
			}
		};
		
		// get the next photo
		getNextId = function() {
			// random
			if(settings.view_random == true) {
				
				// view every image only once a cycle
				if(settings.view_unique == true) {
					id = 0;
				} else {
					id = Math.round(Math.random() * (size - 1));
					if(id == i) {
						id = getNextId();
					}
				}
				
			// default
			} else {
				id = (i >= size - 1) ? 0 : i + 1;
			}
			
			return id;
		}
		
		// init
		i = getNextId();
		image.src = path + images[i];
		
		window.setInterval(slide, settings.view_duration);
	};
})(jQuery);

