YAHOO.namespace("EUKICI"); 

/**
* Create an accessible news scroller
* @requires yahoo
*/
YAHOO.EUKICI.NewsScroller = {
	canvas: null,
	items: null,
	index: 0,
	ceiling: 0,
	effects: {
		fade: {   
			opacity: { from: 1, to: 0 },   
			backgroundColor: { from: "549CCE", to: "FFFFFF" }
		},
		move: {
			left: { to: 0 }
		},
		duration: 5000
	},
	timer: null,
	init: function() {
		// Setup scroller
		var news = document.getElementById("scrolling-news");
		if (!news) { return; }

		this.items = news.getElementsByTagName("li");
		this.ceiling = this.items.length;

		// Stop if there are no items
		if (this.ceiling == 0) {
			return;
		}

		// Create the new scroller and hide the other now we know the browser supports JS
		this.canvas = document.createElement("div");
		this.canvas.id = "dynamic-scrolling-news";
		news.parentNode.insertBefore(this.canvas, news);
		news.style.display = "none";

		// Create initial news item
		var newItem = document.createElement("div");
		newItem.className = "dynamic-news-item";
		newItem.id = "dynamic-news-item-focus";
		newItem.innerHTML = this.items[0].innerHTML;
		this.canvas.appendChild(newItem);

		// Start scrolling if there are enough to scroll
		if (this.ceiling > 1) {
			this.timer = YAHOO.lang.later(this.effects.duration, this, "next", null, true);

			// Pause the animation if the cursor is over the content
			YAHOO.util.Event.on(this.canvas, "mouseover", function() {   
				var NewsScroller = YAHOO.EUKICI.NewsScroller;
				if (NewsScroller) {
					NewsScroller.timer.cancel();
				}
			});

			// Restore the animation 
			YAHOO.util.Event.on(this.canvas, "mouseout", function() {   
				var NewsScroller = YAHOO.EUKICI.NewsScroller;
				if (NewsScroller) {
					NewsScroller.timer = YAHOO.lang.later(NewsScroller.effects.duration, NewsScroller, "next", null, true);
				}
			});
		}
	},
	/**
	* Create a new item
	*/
	createItem: function() {
		var index = this.getNextIndex();
		var newItem = document.createElement("div");
		newItem.className = "dynamic-news-item";
		newItem.id = "dynamic-news-item-transition";
		newItem.innerHTML = this.items[index].innerHTML;
		this.canvas.appendChild(newItem);

		return newItem;
	},
	/**
	* Animate the items
	*/
	next: function() {
		var newItem = this.createItem();

		var fade = new YAHOO.util.Motion("dynamic-news-item-focus", this.effects.fade);  
		var move = new YAHOO.util.Motion("dynamic-news-item-transition", this.effects.move, 1, YAHOO.util.Easing.easeOut);  			
		move.onComplete.subscribe(function() {   
			var focusItem = document.getElementById("dynamic-news-item-focus");
			focusItem.parentNode.removeChild(focusItem);

			var newItem = document.getElementById("dynamic-news-item-transition");
			newItem.id = "dynamic-news-item-focus";
		});  

		move.animate();   
		fade.animate();
	},
	/**
	* Calculate the new news position, increment or start again
	*/
	getNextIndex: function(initial) {
		if (this.index + 1 == this.ceiling) {
			this.index = 0;
		} else {
			this.index++;
		}

		return this.index;
	}
};