(function($){

	$.fn.SlideNews = function(settings){
		settings = $.extend({
			slide : true
		},settings);
		var target = $(this);
		$(function(){
			if(target.find("#scroller .bloco").size() < 2){
				target.find("#seta_news_right").hide();
				target.find("#seta_news_left").hide();
				settings.slide = false;
			}
			target.find("#scroller .bloco").width(target.width())
			target.find("#scroller .bloco").height(target.height())
			var clone = target.find("#scroller .bloco:first").clone();
			clone.appendTo(target.find("#scroller"));
			target.wbloco = target.find("#scroller .bloco").width();
			target.count = target.find("#scroller .bloco").size();
			target.wscroller = target.count*target.wbloco;
			target.find("#scroller").width(target.wscroller);	

			var click_enable = true;
			target.find("#seta_news_right").click(function(){
				if(click_enable){
					if(settings.slide){
						clearInterval(target.interval);
					}
					scrollDestaque("left")
				}
			})
			target.find("#seta_news_left").click(function(){
				if(click_enable){
					if(settings.slide){
						clearInterval(target.interval);
					}
					scrollDestaque("right")
				}
			})
			play_slide = true;
			target.find("#bt_pause_play").click(function(){
				if(play_slide){
					play_slide = false;
					click_enable = false;
					if(settings.slide){
						clearInterval(target.interval);
					}
					$(this).css('background-position','13px 0px');
				}else{
					play_slide = true;
					click_enable = true;
					criarInterval();
					$(this).css('background-position','0px 0px');
				}
			})
			if(settings.slide){
				target.interval = setInterval(slidenews,5000);
			}
			target.nbloco = 1;
		
		})
		function slidenews(){
			if(target.nbloco < target.count){
				scrollDestaque("left");
			}else{
				scrollDestaque("left");
				target.nbloco = 1;
			}	
		}
		function scrollDestaque(dir){
			//alert()
			click_enable = false;
			if(dir == "left"){
				if(parseInt(target.find("#scroller").css('left')) > ((target.wscroller-target.wbloco)*-1)){
					posx = parseInt(target.find("#scroller").css('left'))-target.wbloco;
					target.nbloco++;
				}else if(parseInt(target.find("#scroller").css('left')) == ((target.wscroller-target.wbloco)*-1)){
					target.find("#scroller").css('left','0px');
					posx = (target.wbloco*-1);
					target.nbloco = 1;
				}
			}
			if(dir == "right"){
				if(parseInt(target.find("#scroller").css('left')) < 0){			
					posx = parseInt(target.find("#scroller").css('left'))+target.wbloco;
					target.nbloco--;
				}else if(parseInt(target.find("#scroller").css('left')) == 0){
					target.find("#scroller").css('left',((target.wscroller-target.wbloco)*-1)+'px');
					posx = (((target.wscroller-target.wbloco)*-1)+target.wbloco);
					target.nbloco = target.count-1;
				}
			}
			target.find("#scroller").animate({
					left : posx+'px'
				},500,function(){
					click_enable = true;
					if(target.nbloco < target.count){
						criarInterval();
					}else{
						//restart();
					}
				})
		}
		function criarInterval(){
			if(settings.slide){
				clearInterval(target.interval)
				target.interval = setInterval(slidenews,5000);
			}
		}
	}

})(jQuery);




