(function($) {
 
$.fn.AustralisSlider = function(settings) {
	var opts = {'nextBtn': '#nextBtn','prevBtn': '#prevBtn','pauseBtn': '#playPause','speed':500,'pause':5000};
	if (settings) opts = $.extend(opts, settings);
		
	//Código específico para cada slider encontrado.
	var obj= this;
	this.each(function() {
		var timer = null;
		var div = $(this);
		var childrens = div.children().length;
		var current = -1;
		
		//inicializar procesos
		div.children().hide();
		show(0);
		
		//Function that handles the slider timer.
		function show(index){
			if(current != index){
				current = index;
				div.children(":eq("+index+")").fadeIn(opts.speed).siblings().fadeOut(opts.speed);
			}
			next = nextIndex();
			
			clearTimeout(timer);
			timer = setTimeout(function(){
				show(next);
			},opts.pause+opts.speed);
		}
		function nextIndex(){
			return (current < childrens-1)?current+1:0;
		}
		function prevIndex(){
			return (current > 0)?current-1:childrens-1;
		}
		function play(){
			show(current);
			$(opts.pauseBtn).removeClass("paused");
		}
		function pause(){
			clearTimeout(timer);
			$(opts.pauseBtn).addClass("paused");
		}
		
		//Handle Controls Events
		$(opts.nextBtn).click(function(){
			show(nextIndex());
		});
		$(opts.prevBtn).click(function(){
			show(prevIndex());
		});
		$(opts.pauseBtn).click(function(){
			if($(opts.pauseBtn).hasClass("paused")){
				play();	
			}else{
				pause();
			}
		});
		
	});
	return this;
}; 
})(jQuery);
