/**
 * Simple and clean jQuery fading gallery
 *
 * Build your html like this:
 * <ul id="#gallery">
 *     <li> Your Html content… </li>
 *     <li> <img src="image.jpg" /> </li>
 *     <li>
 *         <h2>Title</h2>
 *         <p>Some text</p>
 *     </li>
 * </ul>
 *
 * @usage
 * $('#slider').cleanGallery();
 *
 * The plugin sets only arbitrary css like position and lis-style, the rest is up to you.
 *
 * CleanGallery takes the following arguments:
 * @param timeOut        (int) time to wait before next slide
 * @param animationSpeed (int) Duration of the transition
 *
 * @author  Jacob Steringa <jacobsteringa@gamil.com>
 * @web     www.jacobsteringa.nl
 * @version 0.1
 * @license GPLv3 (http://www.gnu.org/licenses/gpl.txt)
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * USE AT YOUR OWN RISK!
 */

(function($) {
	$.fn.extend({
		cleanGallery: function(options) {
			var defaults = {
				timeOut: 4000,
				animationSpeed: 1000
			};
			
			var options = $.extend(defaults, options);
			
			return this.each(function() {
				var $element = $(this);
				
				var dimensions = {
					height: $element.height,
					width: $element.width
				};
				
				var items = [], index = 1;
				
				var start = function() {
					$(items[0]).show();
					
					if (items.length > 1)
						setInterval(loop, options.timeOut);
				};
				
				var loop = function() {
					var previousItem;
					((index - 1) == -1) ? previousItem = items.length - 1 : previousItem = index - 1;
					$(items[index]).fadeIn(options.animationSpeed);
					$(items[previousItem]).fadeOut(options.animationSpeed);
					if ((index + 1) == items.length) {
						index = 0;
					} else {
						index++;
					}
				};
				
				$element.css({
					'position': 'relative',
					'list-style': 'none'
				});
				
				$element.children().css({
					'position': 'absolute',
					'top': 0,
					'left': 0
				});
				
				$element.children().each(function(index, element) {
					items[index] = element;
				});
				
				$(items).hide();
				
				start();
			});
		}
	});
})(jQuery);
