$(document).ready(function()
{
	$(".content-news-ticker").each(function()
	{
		$(this).find(".paging").show();
		$(this).find(".paging a:first").addClass("active");
		
		$(this).find(".image_reel a").append($(".image_reel a span"));
		$(this).find(".image_reel span").css({"opacity": "0"});
		
		var windowWidth    = $(this).find(".image_reel span").width();
		var imageSum       = $(this).find(".image_reel img").size();
		var imageReelWidth = windowWidth * imageSum;
		
		$(this).find(".image_reel").css({"width": imageReelWidth});
		
		
		//Paging + Slider Function
		rotate = function()
		{	
			var triggerID         = $active.attr("rel") - 1; //Get number of times to slide
			var imageReelPosition = triggerID * windowWidth; //Determines the distance the image reel needs to slide
	
			$(".paging a").removeClass('active'); //Remove all active class
			
			$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
			
			$(".image_reel").animate(
			{ 
				left: -imageReelPosition
			}, 500);
			
		}; 
		
		//Rotation + Timing Event
		rotateSwitch = function()
		{	
			//Set timer - this will repeat itself every 3 seconds	
			play = setInterval(function()
			{
				$active = $('.paging a.active').next();
				
				//If paging reaches the end...
				if( $active.length === 0)
				{ 
					$active = $('.paging a:first'); //go back to first
				}
				
				rotate(); //Trigger the paging and slider function
				
			}, 5000); //Timer speed in milliseconds (3 seconds)
		};
		
		rotateSwitch(); //Run function on launch
	});
	
	$(".image_reel a").hover(function()
	{
		$(this).find("span").stop().animate({opacity: 0.8}, 200).show();
	},
	function()
	{	
		$(this).find("span").stop().animate({opacity: 0}, 200);
	});

	$(".paging a").click(function()
	{
		var triggerID = $(this).attr("rel") -1;
		var imgWidth = $(this).parent().parent().find("img").width();
		var image_reelPosition = triggerID * imgWidth;
		
		if($(this).hasClass("active"))
		{
			
		}
		else
		{
			$(this).parent().parent().find(".paging a").removeClass("active");
			$(this).addClass("active");
			$(this).parent().parent().find(".image_reel").animate({left: -image_reelPosition}, 300);
		}
		
		return false;
	});
});
