/*
Copyright (c) 2011 Paris Holley
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation;
version 3.0.
This library 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. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, visit
http://www.gnu.org/licenses/lgpl.html
*/


(function( $ ){
	$.fn.nakedSlideshow = function(options){
		var settings = {
			interval: 5000,
			next: null,
			prev: null,
			circular: false,
			controls : null,
			controlsActiveClass: 'active',
			inactive : function(children, callback, first){
				children.css('display', 'none');
		
				callback();
			},
			active: function(child, index){
				child.css('display', 'block');
			}
		};
		
		if( options ){
			$.extend( settings, options );
		}
		
		var child = -1;
		
		var children = this.children();
		
		var scroll = function(){
			if( ++child >= children.length ){
				if( settings.circular ){
					child = 0;
				}else{
					child--;
					return;
				}

			}
				
			settings.inactive(children, function(){
				settings.active($(children[child]), child);
				updateControllers(child);
			}, false);
		};
		
		var intervalId;

		function startTimer(){
			if( intervalId ){
				clearInterval(intervalId);
			}

			scroll();
			
			intervalId = setInterval(scroll, settings.interval);
		}

		function updateControllers(index){
			$(settings.controls).removeClass(settings.controlsActiveClass);
			$($(settings.controls).get(index)).addClass(settings.controlsActiveClass);
			
			if( child > 0 || settings.circular ){
				$(settings.prev).addClass(settings.controlsActiveClass);
			}else{
				$(settings.prev).removeClass(settings.controlsActiveClass);
			}
			
			if( child + 1 < children.length || settings.circular ){
				$(settings.next).addClass(settings.controlsActiveClass);
			}else{
				$(settings.next).removeClass(settings.controlsActiveClass);
			}
		}

		function changeSlide(index){
			child = index - 1;
			startTimer();
			updateControllers(index);
		}

		
		settings.inactive(children, function(){
			startTimer();

			if( settings.controls ){
				$(settings.controls).each(function(index){
					$(this).click(function(){
						changeSlide(index);
						
						return false;
					});
				});
			}
			
			if( settings.prev ){
				$(settings.prev).click(function(){
					if( child - 1 >= 0 ){
						changeSlide(--child);
					} else if( settings.circular && child - 1 == 0){
						child = 0;
						changeSlide(child);
					}	

					return false;
				});
			}

			if( settings.next ){
				$(settings.next).click(function(){
					if( child + 1 < children.length ){
						changeSlide(++child);
					} else if( settings.circular && child + 1 == children.length ){
						child = 0;
						changeSlide(child);
					}	

					return false;
				});
			}
		}, true);
	};
})(jQuery);

