/* Slider de notícias */

(function($)
{
	$.fn.newsSlider = function(options) 
	{
		var defaults = {
			speed: 5000,
			width: 400,
			height: 200,
			autoPlay: false,
			nextButton: null,
			prevButton: null,
			pauseButton: null,
      		pager: false
		};
		
		var settings = $.extend(defaults, options);

		var currentItem = 1;
		var currentObj = null;
		var slideInterval;
		var itemsCount = 0;
		var activeMenuItem = 1;

		return this.each(function(c) 
		{
			currentObj = $(this);
      
      console.log(currentObj);
      
			itemsCount = $('li', currentObj).size();
			
			Reset();
			
			ChangeMessages(currentObj);
			
			if (settings.autoPlay == true)
			{
				slideInterval = window.setInterval(Play, settings.speed);
			}
			
			$("#" + settings.nextButton).click(function() {
				
        		currentItem += 1;
        
				window.clearInterval(slideInterval)
				SlideImages();
				slideInterval = window.setInterval(Play, settings.speed);
				
				return false;
			});

			$("#" + settings.prevButton).click(function() {
				
				currentItem -= 1;
				window.clearInterval(slideInterval)
				SlideImages();
				slideInterval = window.setInterval(Play, settings.speed);
				
				return false;
			});
			
			$("#" + settings.pauseButton).click(function() {
				window.clearInterval(slideInterval)
				
				return false;
			});
			
			$("ul.pager li a",currentObj.parent()).click(function() {
				currentItem = parseInt($(this).html());
				window.clearInterval(slideInterval)
				SlideImages();
				slideInterval = window.setInterval(Play, settings.speed);
				
				return false;
			});


		});
		
		function Play() {
			currentItem++;
			SlideImages();
		}
		
		function SlideImages() {
	
			ChangeMessages();
			
			$("ul.pager li a", currentObj.parent()).removeClass("active");
					
			$(currentObj).animate({ left: GetLeftPosition() }, 500, function() {
			
				if (currentItem > itemsCount) 
				{
				  currentItem = 1;
				}
				else if (currentItem == 0) 
				{
				  currentItem = itemsCount;	
				}
			
				$(currentObj).css("left", GetLeftPosition());
				
				$("ul.pager li a.pager-" + currentItem, currentObj.parent()).toggleClass("active");
				
			}); 
		}
		
		function Reset() {
			currentObj.append($("li:nth-child(1)", currentObj).clone());
			currentObj.prepend($("li:nth-child(" + itemsCount + ")", currentObj).clone());
			
			var maxWidth = 0;
			
			settings.width = currentObj.width();
      
			$("li", currentObj).each(function(count) {
				
				$(this).css("float", "left");
				$(this).css("display", "block");
				$(this).css("overflow", "hidden");
				$(this).css("left", settings.width * count);
				
				$('.info', $(this)).hide();
				
				maxWidth += $(this).outerWidth();
			});
			
			currentObj.css("position", "relative");
			currentObj.css("overflow", "hidden");
			currentObj.css("width", maxWidth);
			
			currentObj.wrap("<div class='newsSlider' style='width: " + settings.width + "px; overflow: hidden' />");
			
			GenerateInfoContent();
			GeneratePagerContent();
			
			$(currentObj).css("left", GetLeftPosition());
		}
    
		function GeneratePagerContent() {
		
		  if (settings.pager == true)
		  {
			var pagerObjHtml = "";
			pagerObjHtml += "<ul class='pager'>";
			
			for (var i = 0; i < itemsCount; i++) {
			  pagerObjHtml += "<li>";
			  pagerObjHtml += "<a class='pager-" + parseInt(i+1) + "' href='#'>" + parseInt(i+1) + "</a>";
			  pagerObjHtml += "</li>";
			}
			
			pagerObjHtml += "</ul>";
			
			var parentObj = currentObj.parent();
			parentObj.append(pagerObjHtml);
			
			$("ul.pager li a.pager-1", parentObj).addClass("active");
		  }
		}
    
		function GenerateInfoContent() {
			var informationObjHtml = "";
			informationObjHtml += "<div class='item_count' />";      
			informationObjHtml += "<div class='item_info'>";
			informationObjHtml += "   <div class='item_title' />";
			informationObjHtml += "   <div class='item_subtitle'/>";
			informationObjHtml += "</div>";
			
			var parentObj = currentObj.parent();
			parentObj.append(informationObjHtml);
		}
		
		function GetLeftPosition() {	
			return settings.width * (currentItem) * (-1);
		}
    
		function ChangeMessages() 
		{
			$(".item_info", currentObj.parent()).fadeOut("fast", function() {
	  
	  			currentItem = currentItem > itemsCount ? 1 : currentItem;
	  
				$(".item_count", currentObj.parent()).html(currentItem + "/" + itemsCount);
				
				var item = parseInt(currentItem + 1);
				var html = $("li:nth-child(" + item + ") .info", currentObj).html();
				
				$(this).html(html);
				
				$(".item_info", currentObj.parent()).fadeIn("slow");

			});
		}
		
		
	};
	
})(jQuery);



