(function($){  
	$.fn.slideshow = function(options) {  

		/* this sets the defaults and somehow puts them into options. whatever. */
		var defaults = {
			transition   : 'fade',			// fade
			speed        : 1000,			// any number
			duration     : 4000,			// any number
			automatic    : true,			// true, false
			pages        : '',				// a jQuery object containing anchors
			page_changes : 'onclick'		// onclick
		};
		var options = $.extend(defaults, options);
		
		
		/* this is what of the plugin is executed */
		return this.each(function() {
								  
			var container = $(this);
			var children = $(this).children();
			
			// set container position
			var position = container.css('position');
			if (position != 'absolute' && position != 'fixed') {
				container.css('position', 'relative');
			}
			
			// find current slide
			var active = container.find('.active');
			if (active.length == 0) {
				active = container.children(':first');
				active.addClass('active');
			}

			// find current link
			if (options.pages) {
				var pages = $(options.pages).find('a');
				pages.eq(children.index(active)).addClass('active');
			}
			
			// pile and hide slides
			children.each(function() {
				$(this).css({
					'position' : 'absolute',
					'top'      : '0px',
					'left'     : '0px',
					'z-index'  : 0
				});
				$(this).not(active).css({
					'opacity' : 0
				});
			});
			
			// find largest width and height
			var maxWidth = '0';
			var maxHeight = '0';
			children.each(function() {
				var thisWidth = $(this).innerWidth();
				var thisHeight = $(this).innerHeight();
				
				if (thisWidth > maxWidth) {
					maxWidth = thisWidth;
				}
				if (thisHeight > maxHeight) {
					maxHeight = thisHeight;
				}
			});
			// set container size
			container.css({
				width : maxWidth,
				height : maxHeight
			});
			
			// start automatic slideshow
			if (options.automatic) {
				var interval = setInterval(function() { transition(container); }, options.duration);
			}
			
			// pages onclick
			if (options.page_changes == 'onclick') {
				pages = $(options.pages).find('a');
				pages.each(function() {
					$(this).click(function(e) {
						// stop slideshow, switch to selected slide
						e.preventDefault();
						clearInterval(interval);
						if ($(this).text() != $(options.pages).find('a.active').text()) {
							var page = $(this);
							transition(container, page);
						}
						// restart slideshow
						if (options.automatic) {
							interval = setInterval(function() { transition(container); }, options.duration);
						}
					});
				});
			}
		
		}); 
		
		
		/* these are the functions used in the return */
		
		function transition(container, page) {
			
			// find current slide
			var active = container.find('.active');
			// find next slide
			if (page) {
				var next = container.children().eq($(options.pages).find('a').index(page));
			} else {
				var next = active.next();
				if (next.length == 0) {
					next = container.children(':first');
				}
			}
			
			
			// fade transition
			if (options.transition == 'fade') {
				active.css('z-index', 0);
				next
					.css('z-index', 5)
					.animate({'opacity' : 1}, options.speed, function() {
						active
							.animate({'opacity' : 0}, (options.speed/4))
							.removeClass('active')
						;
					})
					.addClass('active')
				;
			} // fade transition
			
			// pages transition
			if (options.pages) {
				
				// pages transition
				var pages = $(options.pages).find('a');
				var currPage = $(options.pages).find('a.active');
				if (page) {
					var nextPage = page;
				} else {
					var nextPage = pages.eq(pages.index(currPage)+1);
					if ((pages.index(currPage)+1) >= pages.length) {
						nextPage = pages.eq(0);
					}
				}
				currPage.removeClass('active');
				nextPage.addClass('active');

			} // page transition
			
		} // function transition()
	
	}; // slideshow plugin
})(jQuery);

