(function($){

	$.fn.SlideNews = function(settings){
		settings = $.extend({

		},settings);
		var target = $(this);
		$(function(){
			
			var clone = target.find("#scroller .bloco:first").clone();
			clone.appendTo(target.find("#scroller"));
			wbloco = target.find("#scroller .bloco").width();
			count = target.find("#scroller .bloco").size();
			wscroller = count*wbloco;
			target.find("#scroller").width(wscroller);	

			click_enable = true;
			$("#seta-destaque-right").click(function(){
				if(click_enable){
					clearInterval(interval);
					scrollDestaque("left")
				}
			})
			$("#seta-destaque-left").click(function(){
				if(click_enable){
					clearInterval(interval);
					scrollDestaque("right")
				}
			})
			play_slide = true;
			$("#bt_pause_play").click(function(){
				if(play_slide){
					play_slide = false;
					click_enable = false;
					clearInterval(interval);
					$(this).css('background-position','13px 0px');
				}else{
					play_slide = true;
					click_enable = true;
					criarInterval();
					$(this).css('background-position','0px 0px');
				}
			})
			interval = setInterval(slider,5000);
			nbloco = 1;
		
		})
		function slider(){
			if(nbloco < count){
				scrollDestaque("left");
			}else{
				/*target.find("#scroller").css('left',wbloco+'px');
				target.find("#scroller").animate({
					left : '0px'
				},500,function(){
					click_enable = true;
					criarInterval();
				})*/
				scrollDestaque("left");
				nbloco = 1;
			}	
		}
		function scrollDestaque(dir){
			//alert()
			click_enable = false;
			if(dir == "left"){
				if(parseInt(target.find("#scroller").css('left')) > ((wscroller-wbloco)*-1)){
					posx = parseInt(target.find("#scroller").css('left'))-wbloco;
					nbloco++;
				}else if(parseInt(target.find("#scroller").css('left')) == ((wscroller-wbloco)*-1)){
					target.find("#scroller").css('left','0px');
					posx = (wbloco*-1);
					nbloco = 1;
				}
			}
			if(dir == "right"){
				if(parseInt(target.find("#scroller").css('left')) < 0){			
					posx = parseInt(target.find("#scroller").css('left'))+wbloco;
					nbloco--;
				}else if(parseInt(target.find("#scroller").css('left')) == 0){
					target.find("#scroller").css('left',((wscroller-wbloco)*-1)+'px');
					posx = (((wscroller-wbloco)*-1)+wbloco);
					nbloco = count-1;
				}
			}
			target.find("#scroller").animate({
					left : posx+'px'
				},500,function(){
					click_enable = true;
					if(nbloco < count){
						criarInterval();
					}else{
						//restart();
					}
				})
		}
		function criarInterval(){
			clearInterval(interval)
			interval = setInterval(slider,5000);
		}
	}

})(jQuery);




