// Simple Slideshow plugin version 0.01

// Creates a slideshow based off of the markup:
// <ul>
//		<li>
//			<a><img alt="Caption" /></a>
//		</li>
//	</ul>

// Anchor makes slide clickable
// Image is the slide
// Alt attribute is used as the caption



(function($,undefined) {
			
	$.fn.simpleslider = function(options_arg) {
		var options = $.extend({}, $.fn.simpleslider.defaults, options_arg);
		
		
		
		return this.each(function(idx,itm) {
			$wrapper = $(this);
			
			$source = (options.contentSource != '') ? 
							(options.contentSource instanceof jQuery) 
								? options.contentSource : $(options.contentSource)
					: $wrapper.find('ul');
			
			$.fn.simpleslider.init($wrapper,$source,options);
			
		});
	};
	
	$.fn.simpleslider.init = function($wrapper,$source,options) {
		var	controlNumber = $source.find('li').length,
				recursiveName = 'simpleslider_'+Math.floor(Math.random()*10000);
		options.clicked = false; //Adding this to pause timer a full cycle
		$wrapper.data('options',options);
		//Hide the extra images if visible?
		if($source.find('li:visible').length) $source.find('li:visible').hide();
		$wrapper.data('paused',false);
		$wrapper.data('curSlide', options.startingSlide);
		if(options.pauseOnHover) {
			$wrapper.hover(function() {
				$(this).data('paused',true);
			},function() {
				$(this).data('paused',false);
			});
		}
	
		$wrapper.get(0).innerHTML = ''; 
		
		if(options.showCaption) {
			$source.find('li img').each(function() {
				var $slideImg = $(this);
				$slideImg.data('caption',$slideImg.attr('alt')).attr('alt','');
			});
		}

		if(options.showControls && $source.find('li').length > 1) {

			var $controls = $('<ul />');
			$controls.addClass(options.controlsClass);
			for(var i=1;i<=controlNumber;i++) {
				var newLI = $('<li><a href="#">'+i+'</a></li>');
				$controls.append(newLI);
			}
			
			$controls.find('li a').click(function(e) {
				var 	$wrapper = $(this).parent().parent().parent(),
						options = $wrapper.data('options'),
						$source = $(options.contentSource);
				options.clicked = true;
				$wrapper.data('options',options);
				$wrapper.data('curSlide',$(this).parent().index()-1);
				$.fn.simpleslider.changeImg($wrapper,$source,options,true);
				e.preventDefault();				
			});
		}	

		var $img = $('<img />');
		$wrapper.append($img);
		
		if(options.showCaption) {
			var $caption = $(options.captionWrapper);
			$caption.addClass(options.captionClass);
			$wrapper.append($caption);
		}
		if(options.showControls && $source.find('li').length > 1)  $wrapper.append($controls);
		$wrapper.data('curSlide',options.startingSlide-1);
		
		window[recursiveName] = function($wrapper,$source,options) {
				if(!$wrapper.data('paused') && !options.clicked) {
					$.fn.simpleslider.changeImg($wrapper,$source,options);
				}
				if(options.clicked) {
					options.clicked = false;
					$wrapper.data('options',options);
				}
				setTimeout(function() {
					window[recursiveName]($wrapper,$source,options);
				}, options.slideDuration);
			};
		if(options.slideDuration !== 0) {
			window[recursiveName]($wrapper,$source,options);
		}
	};
	
	$.fn.simpleslider.changeImg = function($wrapper,$source,options,clicked) {
		var 	nextIdx = (options.random)?$.fn.simpleslider.generateRandom($wrapper.data('curSlide'),$source.find('li').length):($wrapper.data('curSlide')+1>$source.find('li').length-1)?0:$wrapper.data('curSlide')+1,
				$slide = $source.find('li').eq(nextIdx),
				$newImg = $('<img />'),
				$slideImg = $slide.find('img'),
				$img = $wrapper.find('img'),
				clicked = (clicked != undefined) ? true : false;
		
				
		if((options.fixTransparencyIE && $.browser.msie) || (clicked)) {
			$img.attr('src',$slideImg.attr('src'));
			if(!options.showCaption) {
				$newImg.attr('alt',$slideImg.attr('alt'));
			}
			$wrapper.data('curSlide',nextIdx);
			if($img.parent().get(0).tagName === 'A') {
				var ctn = $img.parent().contents();
				$img.parent().replaceWith(ctn);
			}
		}else{
			$newImg.attr('src',$slideImg.attr('src'));
			$newImg.css('z-index',0);
			if(!options.showCaption) {
				$newImg.attr('alt',$slideImg.attr('alt'));
			}
			$wrapper.prepend($newImg);
			$img.fadeOut('slow',function() {
				$wrapper.data('curSlide',nextIdx);
				$newImg.css('z-index',$img.css('z-index'));
				if($img.parent().get(0).tagName == "A") {
					$img.parent().remove();					
				}else{				
					$img.remove();
				}							
				$img = $newImg;
			});
		}
		
		if(options.showControls && $source.find('li').length > 1) {
			var $controls = $wrapper.find('ul');
			$controls.find('li').find('a').removeClass(options.activeControlClass);
			$controls.find('li').eq(nextIdx).find('a').addClass(options.activeControlClass);
		}
		
		if($slide.find('a').length) {
			if((options.fixTransparencyIE && $.browser.msie) || (clicked)) {
				$img.wrap('<a href="'+$slide.find('a').attr('href')+'"></a>');
			}else{
				$newImg.wrap('<a href="'+$slide.find('a').attr('href')+'"></a>');
			}
		}
		if(options.showCaption) {
			$wrapper.find('.'+options.captionClass).text($slideImg.data('caption'));
		}
		if(options.transitionComplete != undefined) {
			options.transitionComplete($wrapper,$source,$slide);
		}
	};

	$.fn.simpleslider.generateRandom = function(curSlideIdx,totalSlides) {
		var nextIdx = Math.floor(Math.random()*totalSlides);
		if(nextIdx === curSlideIdx) {
			return $.fn.simpleslider.generateRandom(curSlideIdx,totalSlides);
		}else{
			return nextIdx;
		}
	};

	$.fn.simpleslider.defaults = {
		contentSource: '',
		pauseOnHover: true,
		slideDuration: 4800,
		startingSlide: 0,
		showControls: true,
		controlsClass: 'simpleSliderControls',
		captionWrapper: '<h3></h3>',
		showCaption: true,
		captionClass: 'simpleSliderCaption',
		activeControlClass: 'active',
		fixTransparencyIE: false,
		transitionComplete: undefined,
		random: false
	};
	
})(jQuery);


