/*
	Slideshow - jQuery plugin
	
	Copyright (c) Justin Sepulveda
	
	Dual licensed under the MIT and GPL licenses:
	http://www.opensource.org/licenses/mit-license.php
	http://www.gnu.org/licenses/gpl.html

	Date: 2007.05.24
	Version: 1.0
 */

(function ($) {
	
	$.fn.slideshow = function (initial, settings) {

		if (typeof initial === 'object') { settings = initial; }

		settings = $.extend({
		
			initial: (initial && typeof initial === 'number' && initial > 0) ? --initial : 0,
			selectedClass: '.selected',
			tabClass: '.tab',
			controlClass: '.control',
			slideClass: '.panel',
			photoClass: '.photo',
			backId: '#previous-slide',
			forwardId: '#next-slide',
			pauseId: '#pause-slide',
			playId: '#play-slide',
			controlsId: '#controls-slideshow',
			menuId: '#menu-slideshow',
			fxShowSpeed: 'normal',
			fxHideSpeed: 'slow',
			fxFade: null,
			setHeight: false,
			setRotate: true,
			rotateTimer: 5000
			
		 }, settings || {});

		return this.each(function () {
			
			var container = this;

			var tab = settings.tabClass;
			var tabSet = $(tab, container);
			
			var slide = settings.slideClass;
			var slideSet = $(slide, container);
			var slideCount = slideSet.size();

			var control = settings.controlClass;
			var controlSet = $(control, container);
		
			var photo = settings.photoClass;
			var photoSet = $(photo, container);
		
			var pause = settings.pauseId.replace('#', '');
			var play = settings.playId.replace('#', '');
		
			var selected = settings.selectedClass.replace('.', '');
			
			var showFx = {};
			var hideFx = {};
			var showSpeed = settings.fxShowSpeed;
			var hideSpeed = settings.fxHideSpeed;
			
			var current = 0;
			var interval = 0;
				
			slideSet.parent().css('position', 'relative');
			slideSet.css('position', 'absolute');
			slideSet.children().css('position', 'relative');
			photoSet.css('z-index', '1');
			
			function setHeight() {
					
				var heights = $.map(slideSet.get(), function(el) {

					var h = $(el).height();
					return h;

				}).sort(function (a, b) {
					return b - a;
				});

				slideSet.parent().height(heights[0]);
				
			}
			
			function hideslide() {
				
				var hideThis = photoSet.filter(':visible');
				
				hideThis.siblings().hide();
				photoSet.filter(':visible').animate(hideFx, hideSpeed);
			
			}
			
			function showslide() {

				photoSet.eq(current).siblings().show();
				photoSet.eq(current).animate(showFx, showSpeed);

				$(tab, container).removeClass(selected);
				$(tab, container).eq(current).addClass(selected);
				
			}	
			
			function rotateslide() {
										
				var old = $(slideSet).index($(photoSet.filter(':visible').parent())[0]);

				current = (old + 1) % slideCount;
			
				hideslide();
				showslide();	

			}	
		
			if (settings.setHeight) { setHeight(); }

			if (settings.setRotate) { interval = setInterval(rotateslide, settings.rotateTimer); }

			if (settings.fxFade) {
				
				showFx.opacity = 'show';
				hideFx.opacity = 'hide';
				
			} else {
				
				showFx.opacity = 'show';
				hideFx.opacity = 'hide';
				showSpeed = 1;
				hideSpeed = 1;
				
			}
		
			photoSet.not(':eq(' + settings.initial + ')').hide().siblings().hide();
			$(tab).eq(settings.initial).addClass(selected);
				
			tabSet.find('>a').click(function () {
			
				if (settings.setRotate) { clearInterval(interval); }
				if (photoSet.filter(':visible').length > 1 || $(this.parentNode).is('.' + selected)) { return false; }

				var clicked = $(this.hash);
				current = $(slideSet).index($(clicked)[0]);
				
				hideslide();
				showslide();
						
				$(settings.pauseId).removeAttr('id').attr('id', play);				
			
				return false;

			});
			
			controlSet.find('>a').click(function () {
		
				if (settings.setRotate) { clearInterval(interval); }
				if (photoSet.filter(':visible').length > 1) { return false; }

				var controlId =  '#' + $(this.parentNode).attr('id');
				var old = $(slideSet).index($(photoSet.filter(':visible').parent())[0]);
				
				switch(controlId) {
				
					case settings.backId:
						
						current = (old - 1) % slideCount;
						if (current === -1) { current = slideCount - 1; }
						
						hideslide();
						showslide();
						
						$(settings.pauseId).removeAttr('id').attr('id', play);
										
						break;
													
					case settings.forwardId:
						
						current = (old + 1) % slideCount;
						
						hideslide();
						showslide();
						
						$(settings.pauseId).removeAttr('id').attr('id', play);
						
						break;
									
					case settings.pauseId:
					
						clearInterval(interval);
					  	$(settings.pauseId).removeAttr('id').attr('id', play);
						
						break;						
								
					case settings.playId:    

						interval = setInterval(rotateslide, settings.rotateTimer);
						$(settings.playId).removeAttr('id').attr('id', pause);
						
						break;

				}
			
				
				return false;		
			
			});
		
		});
	};
	
})(jQuery);