/*
By:      Daniel Bueno da Silva
Data:    11/08/2011
Version: 0.1
*/

(function($){

	$.fn.Slider = function(settings){
		settings = $.extend({
			wbt            : "23px",
			hbt            : "22px",
			font_color_bt  : "#fff",
			font_size_bt   : "13px",
			padding_top_bt : "4px",
			radius         : null,
			border         : null,
			radius_bt      : null,
			mousewheel     : null,
			intro          : null,
			time           : 6000
		},settings);
		/*BEGIN SCROOLLER DESTAQUE*/
		var target = $(this);
		
		target.find(".bloco").css({
			'width':target.width()+'px',
			'height':target.height()+'px'
		});
		if(settings.radius){
			target.css({'border-radius':settings.radius});
		}
		if(settings.border){
			target.css({'border':settings.border});
		}
		
		if(target.find("#scroller .bloco").size() == 1){
			return(false);
		}

		var clone = target.find("#scroller .bloco:first").clone();
		clone.attr('id','bloco'+parseInt(target.find("#scroller .bloco").size()+1))
		clone.appendTo(target.find("#scroller"));
		
		target.count     = target.find("#scroller .bloco").size();
		target.wbloco    = target.find("#scroller .bloco").width();
		wscroller = target.count*target.wbloco;		
		
		target.find("#scroller").width(wscroller);			
		
		b = 0;
			
		$(document).ready(function(){
			/*if(settings.mousewheel){
				target.mousewheel(function(evt,delta){
					if(delta > 0){
						if(b < target.count-2){
							b++
							moverDestaque();
						}
					}else{
						if(b > 0){
							b--
							moverDestaque();
						}
					}
				});
			}*/
					
			for(i = 1; i<=target.count; i++){
				//left = parseInt((i-1)*target.wbloco);
				//target.find("#scroller .bloco").eq(i-1).css({'top':'0px','left':left+'px'});
				//var bt = $("<div class='bt-pag' id='bt"+i+"' style='left:"+((i-1)*26)+"px'>"+i+"</div>");
				if(i < target.count){
					var bt = $("<div class='bt-pag bt' id='bt"+i+"'>"+i+"</div>");
					target.find("#bts").append( bt );
				}
			}
			target.find("#bts .bt-pag").css({
				'width':settings.wbt,
				'height':settings.hbt,
				'color':settings.font_color_bt,
				'font-size':settings.font_size_bt,
				'padding-top':settings.padding_top_bt,
				'border-radius':settings.radius_bt				
			});
			if(settings.intro){
				target.find("#scroller").css("left",((target.wbloco*(target.count-1))*-1)+'px');
			}
			moverDestaque();
			
			target.find(".bt-pag").click(function(){
				b = ($(this).attr("id").replace("bt",""))-1;
				moverDestaque();
			})
			target.interval = setInterval(slider,settings.time);
		})
		function slider(){
			b++;
			moverDestaque();
		}
		function moverDestaque(){
			target.posx = (b*target.wbloco)*-1;
			if(b == target.count-1){
				b = 0;
				target.find("#scroller").animate({
					left : target.posx
				},1000,restart);
			}else{
				target.find("#scroller").animate({
				left : target.posx
				},1000,criarInterval);
			}
			
			destacarBotao();
		}
		function destacarBotao(){
			target.find(".bt-pag").each(function(){
				id = $(this).attr("id");
				if(id == "bt"+(b+1)){					
					$(this).removeClass('bt').addClass('bt-act');
				}else{
					$(this).removeClass('bt-act').addClass('bt');
				}
			})
		}
		function criarInterval(){
			clearInterval(target.interval)
			target.interval = setInterval(slider,settings.time);
		}
		function restart(){
			target.find("#scroller").css("left","0px");
			destacarBotao();
		}
		/*END SCROOLLER DESTAQUE*/
	}

})(jQuery);
