/**
 * Perceptis News Ticker jQuery plugin
 *
 * Usage:
 * <code>
 * var mySettings = {};
 * $("#MyNewsTicker").pNewsTicker(mySettings);
 * </code>
 *
 * Settings:
 * <code>
 * startIndex: 0,						// index of the news item to display first
 * updateSeconds: 8,					// the number of seconds to wait before switching news items
 * animateInterval: 33,				// the number of milliseconds between animations
 * selectors: {
 *   news: '.NewsTickerItem',		// news item selector, normally an anchor element
 *   previous: '.Previous',		// previous button selector
 *   next: '.Next',					// next button selector
 *   pause: '.Pause'					// pause button selector
 * },
 * data: [								// array of news item objects
 *   0: {
 *     id: 1							// unique identifier (currently unused)
 *     title: ""						// text to display in the ticker
 *   },
 *   ...
 * ]
 * </code>
 */
(function($) {
	/**
	 * Initialise a news ticker on a given element
	 */
	$.fn.pNewsTicker = function(settings) {
		// merge user-defined settings with default config
		var config = $.extend({}, $.fn.pNewsTicker.defaults, settings);
		

		//some internal variables
		var newsTimer,
			newsElement = $(config.selectors.news, this),
			newsIndex = config.startIndex,
			lengthTimer,
			lengthCurrent;


		return this.each(function() {
			// set up the button click events
			$(config.selectors.previous, this).click(function(event) {
				event.preventDefault();
				previousNews();
			});
			
			$(config.selectors.next, this).click(function(event) {
				event.preventDefault();
				nextNews();
			});
			
			$(config.selectors.pause, this).click(function(event) {
				event.preventDefault();
				toggleNewsTicker();
			});
			
			// start the ticker
			startNewsTicker();
		});


		/**
		 * Start the news ticker
		 */
		function startNewsTicker() {
			// stop ticker in case it is already running
			stopNewsTicker();
			
			// set the refresh timer
			showNews();
			if (config.data.length > 1) {
				newsTimer = setInterval(nextNews, config.updateSeconds * 1000);
			}
		}
		
		
		/**
		 * Stop the news ticker from updating (note: does not stop the length timer)
		 */
		function stopNewsTicker() {
			if (newsTimer != null) {
				clearInterval(newsTimer);
				newsTimer = null;
			}
		}
		
		
		/**
		 * Start or stop the ticker as appropriate
		 */
		function toggleNewsTicker() {
			if(newsTimer != null) {
				stopNewsTicker();
			} else {
				newsIndex++;
				startNewsTicker();
			}
		}
		
		/**
		 * Display the current news item
		 */
		function showNews() {
			// get the current news object
			var newsItem = config.data[newsIndex];
			
			// set the link and body of the ticker element
			if (newsItem != null && newsItem.id != null && newsItem.title != null) {
				newsElement.attr('href', "latest-news.php?n=" + newsItem.id.toString());
				startLengthIncrease();		// reset the width and start the width timer
			} else {
				newsElement.html("There is no news at present.").attr('href', '#');
			}
		}
		
		
		/**
		 * Switch to a different news item
		 * Wrap-around is handled here
		 */
		function changeNewsIndex(delta) {
			newsIndex += delta;
			if (newsIndex >= config.data.length) {
				newsIndex = 0;
			} else if (newsIndex < 0) {
				newsIndex = config.data.length - 1;
			}
			showNews();
		}
		
		
		/**
		 * Switch to the next news item
		 */
		function nextNews() {
			changeNewsIndex(1);
		}
		
		
		/**
		 * Switch to the pewvious news item
		 */
		function previousNews() {
			changeNewsIndex(-1);
		}
		
		
		/**
		 * Start the ticker effect
		 */
		function startLengthIncrease() {
			if (lengthTimer != null) {
				clearInterval(lengthTimer);
				lengthTimer = null;
			}
			lengthCurrent = 0;
			lengthTimer = setInterval(lengthIncrease, config.animateInterval);
		}
		
		
		/**
		 * Increase the length of text displayed in the ticker
		 */
		function lengthIncrease() {
			if (lengthCurrent >= config.data[newsIndex].title.length) {
				clearInterval(lengthTimer);
				lengthTimer = null;
			} else {
				lengthCurrent++;
				newsElement.html(config.data[newsIndex].title.substring(0, lengthCurrent));
			}
		}


	};
	
	
	$.fn.pNewsTicker.defaults = {
		startIndex: 0,
		updateSeconds: 8,
		animateInterval: 33,
		selectors: {
			news: '.NewsTickerItem',
			previous: '.Previous',
			next: '.Next',
			pause: '.Pause'
		},
		data: []
	};
})(jQuery);
