/* 92702/common/scripts/ticker.js */
ensurePackage('guardian.r2');

guardian.r2.newsTicker = function () {
	var speed = 3000; //change every 5 secs; TAKE INTO ACCOUNT THE FADE TIME THOUGH!
	var count = 0;
	var trendCount = 0;
	var paused = false;
	var newsHeadlines = jQ('ul#ticker > li'); //get the headlines
	var trends = jQ('li.trending ul li');
	var trendTimer;
	var tickerStrap = jQ("#newsticker .ticker-heading strong");
	var instance = this;
	
	this.changeStory = function(direction, hover) {
		var currentItem = jQ(newsHeadlines).eq(count);
		var newItem;

		timer = window.clearTimeout(timer);
		switch (direction) {
			
			case 'back' :  //previous
				if(paused) { 
					paused = false; //make sure we unpause when clicking next or previous
				}
				//decrement the count; go to the end if we've reached the first one
				(count === 0) ? count = newsHeadlines.length-1 : count--;
				//get the previous item 
				newItem = newsHeadlines.eq(count);
				if(newItem.hasClass('trending')) {
					changeStrap('Current topics:');
					trends.show();
				} else {
					changeStrap('Breaking news:');
				}
				break;		

			case 'pause' : //next
				if(!paused) {
					paused = true;
					if(currentItem.hasClass('trending')) {
						trendTimer = window.clearTimeout(trendTimer);
						trends.show();
					}
					break;
				} else {
					changeStrap('Breaking news:');
					paused = false;
				}
				

			default : 	//next - or the default
				if(paused) { 
					paused = false;
				}
				//increment the count; set to 0 if we're at the end
				(count === newsHeadlines.length-1) ? count = 0 : count++;
				//get the next one
				newItem = newsHeadlines.eq(count);
				if(direction === "forward") {
					if(newItem.hasClass('trending')) {
						changeStrap('Current topics:');
						trends.show();
					} else {
						changeStrap('Breaking news:');
					}
				}

				break;
		}
		if(!paused) {
			if( newItem.hasClass('trending') && (!direction || direction =="pause") ) {
				currentItem.fadeOut(250, function() {
					trends.hide();
					changeStrap('Current topics:');
					newItem.fadeIn(250);
					trendingTicker();
				});
				
			} else {
				currentItem.fadeOut(250, function() {
					newsHeadlines.hide();
					newItem.fadeIn(250);				
				});
				timer = window.setTimeout(function() {instance.changeStory();}, speed);
			}
		}
	};

	this.hoverPause = function(ev) {
		if(ev=="in") {
			if(trendTimer) {
				trendTimer = window.clearTimeout(trendTimer);
				trends.show();
			} else if(timer) { 
				timer = window.clearTimeout(timer);
			}
		} else {
			timer = window.setTimeout(function() {instance.changeStory('pause');}, 2000);
		}
	};

	function trendingTicker() {
		trends.eq(trendCount).fadeIn(250);
		
		if(trendCount < trends.length) {
			trendCount++;
			trendTimer = window.setTimeout(trendingTicker, speed);
		} else {
			//go back to the main ticker
			trendCount = 0;
			instance.changeStory();
			trends.hide();
			changeStrap('Breaking news:');			
		}
	}
	
	function changeStrap(newText) {
		if(tickerStrap.text() != newText) {
			tickerStrap.hide();
			tickerStrap.text(newText);
			tickerStrap.fadeIn(250);
		}
	}
	
	//set up event handlers
	jQ('p#ticker-controls input').click( function() {		
		var direction = jQ(this).attr('alt');
		instance.changeStory(direction);
	});

	newsHeadlines.hover(function() { 
		instance.hoverPause('in');
	})
	.hover(function() {
		instance.hoverPause('out');
	});
	
	//set the timer running
	var timer = window.setTimeout ( function() {instance.changeStory();}, speed);
};	

jQ(document).ready(function() {
	var myTicker = new guardian.r2.newsTicker();
});

