(function($)	{
	
	//Set a .js class to HTML element
	if( !$('html').is('.js') ) { $('html').addClass('js'); }	
	
	$(document).ready(function()	{ 					
		ticker.init("#tickerWrap");  //News ticker
	});	
	
	
	// News ticker
	 var ticker = { 
		controls: '',
		initDelay: 5000, 
		initDuration: 500,
		newsList: '',
			
		init: function(cssSelector)	{			
			el = $(cssSelector);
			ticker.newsList = $('.tickerNews',el);
			ticker.newsList.find('li a').attr('tabindex',-1);
			ticker.newsList.find('li:first').css('display','block').addClass('active').find('a').attr('tabindex',0);
			
			//Create ticker controls
			ticker.controls += '<ul class="tickerControls">';
			ticker.controls += '<li class="control off"><a href="#">Play</a></li>';
			ticker.controls += '<li class="prev"><a href="#">Previous</a></li>';				
			ticker.controls += '<li class="next"><a href="#">Next</a></li>';
			ticker.controls += '</ul>'; 				
			el.append(ticker.controls);				
			ticker.controls = $('.tickerControls');
			
			var navButtons = $('.next a, .prev a',ticker.controls); //Next/Prev buttons
			var controlButton = $('.control a',ticker.controls); //Stop button
			
			
			// Backwards navigation
			navButtons.click(function()	{
				stopAnimation();
				navigateNews( $(this) );
				return false;
			});
			ticker.newsList.find('li a').mouseover(function()	{
				stopAnimation();
			});	
			ticker.newsList.find('li a').mouseout(function()	{
				controlButton.text('Stop').parent().addClass('off');
				interval = setInterval(function() {
					navigateNews($(this));			
				}, ticker.initDelay);
			});	
			
			
			var interval;
			controlButton.click(function()	{
				if( $(this).parent().is('.off') )	{ 
					stopAnimation();
					controlButton.text('Play');
				} else {					
					// Change play to "pause" image
					controlButton.text('Stop').parent().addClass('off');
				
					// Show the next link
					navigateNews($(this));
				
					// Start playing the animation
					interval = setInterval(function() {
						navigateNews($(this));
					}, ticker.initDelay);
				}
				return false;
			});
			
			
			//Navigate next/prev
			var animating = false;
			var navigateNews = function(o)	{
				// Check if no animation is running. If it is, prevent the action
				if(animating) {
					return;
				}
				animating = true;
	
				var $active = $('li.active',ticker.newsList);
				if ( $active.length == 0 ) {
					$active = $('li:last',ticker.newsList);
				}
				var $next =  $active.next().length ? $active.next() : $('li:first',ticker.newsList);
				var $prev =  $active.prev().length ? $active.prev() : $('li:last',ticker.newsList);
				
				var $action =  o.parent('li').is('.prev') ? $action = $prev : $action = $next;	
				$active.fadeOut(function() {
					$active.removeClass('active').find('a').attr('tabindex',-1);
					setTimeout(function() {
						$action.css({"display" : "block"});
						$action.addClass('active').find('a').attr('tabindex',0);
						animating = false;
					}, 100);
				});		   
			};			
			
			
			//Stop rotation
			var stopAnimation = function()	{
				controlButton.parent().removeClass('off');
				clearInterval(interval);	
			};
			
			// We should statically set the first image
			//navigateNews($(this));
	
			
			// Start playing the animation
			interval = setInterval(function() {
				navigateNews($(this));
			}, ticker.initDelay);
	
		}
	};
	// End: News ticker	
	
	
})(jQuery)
	


