
(function($) {
$.fn.ListaConteudoRotativo = function (options) {
	
	var opts = $.extend({
		timer: 4000,
		fadeSpeed: "normal",
		autoPlay: true,
		classAt: "dstBtAt",
		classOver: "dstBtOver",
                classItemConteudo: ".dstItem",
                classListaItensConteudo: ".dstItens",
                classPaginacao: ".btRpt"

	}, options);
	
	this.each(function(){
		
		//Configs iniciais
		var Interval;
		var Father		= $(this);
		var selItem		= 1;
		var nItens 		= $(opts.classItemConteudo, Father).size();
		
		$(opts.classItemConteudo, Father).hide();
		//$('.dstItem:first', Father).show();
		
		//$(opts.classListaItensConteudo, Father).css('position', 'relative');
		$(opts.classItemConteudo, Father).css('position', 'absolute');
		
		//----------------
		
		//Funcoes		
		function showDst (n){
			if (opts.autoPlay) {stopDst();}
			
			$(opts.classItemConteudo, Father).fadeOut(opts.fadeSpeed);
			$(opts.classItemConteudo+":eq("+(n-1)+")", Father).fadeIn(opts.fadeSpeed);
			
				selItem = n;
				$(opts.classPaginacao, Father).removeClass(opts.classAt);
				$(opts.classPaginacao+":eq("+(n-1)+")", Father).addClass(opts.classAt);
				if (opts.autoPlay) {playDst(opts.timer);}
		}
		
		function nextDst () {

			if (selItem > nItens-1) {
				n = 1;
			} else {
				n = selItem + 1;
			}

			showDst(n);

		}
		
		function playDst (t) {
			Interval = setTimeout(nextDst, t);
		}
		
		function stopDst () {
			clearTimeout(Interval);
		}
		//-------
		
		//Crio os Botoes
		var btModel = $(opts.classPaginacao+":last", Father);
		for (i=0; i<nItens-1; i++) {btModel.clone().insertAfter(btModel);}
		
		var lblBt = 0;
		$(opts.classPaginacao, Father).each(function(){
			lblBt++;
			$(this).html(lblBt);
			$(this).click(function(){
				n = parseFloat($(this).html());
				if (selItem != n) {
					showDst(n);
				}
			});
			
			$(this).hover(function(){
				$(this).addClass(opts.classOver);
			},
			function(){
				$(this).removeClass(opts.classOver);
			});
		});
		//--------------
		
		showDst (1);
		if (opts.autoPlay) {playDst(opts.timer);}
		
		
	});
}
})(jQuery);
