document.observe('dom:loaded', function () { new Newsticker($("recent"), 8) });


Newsticker = Class.create();
Newsticker.prototype = {
	
	transition: function()
	{
		if(typeof this.items[this.previousItem] != 'undefined')
			Element.hide(this.items[this.previousItem]);
		new Effect.BlindDown(this.items[this.currentItem]);		
	},
	
	
	
	
	
	
	
	initialize: function(container, intervalSec)
	{
		this.container = container;
		this.intervalMs = intervalSec*1000||6000;
		this.interval = null;
		this.items = $A(this.container.getElementsByTagName('li'));
		this.numOfItems = this.items.length;
		this.currentItem = 0;
		this.previousItem = null;
		
		this.container.addClassName('newsticker');
		this.hideItems();
		this.tick();
		this.startInterval();
		
		/**Event.observe(this.container, 'mouseover', this.mouseOverListener.bindAsEventListener(this));
		Event.observe(this.container, 'mouseout', this.mouseOutListener.bindAsEventListener(this));**/
	},
	
	mouseOverListener: function(e)
	{

		var reltg = e.relatedTarget ? e.relatedTarget : e.fromElement;
		while(reltg && reltg != this.container)
			reltg = reltg.parentNode;
		if(reltg == this.container) return;
		
		this.stopInterval();
	},
	
	mouseOutListener: function(e)
	{
		var reltg = e.relatedTarget ? e.relatedTarget : e.toElement;
		while(reltg && reltg != this.container)
			reltg = reltg.parentNode;
		if(reltg == this.container) return;
		
		this.startInterval();
	},
	
	startInterval: function()
	{
		this.interval = setInterval(this.tick.bind(this), this.intervalMs);
	},
	
	stopInterval: function()
	{
			this.interval = clearInterval(this.interval)
	},
	
	
	tick: function()
	{
		this.transition();
		
		if(this.currentItem < this.numOfItems - 1) {
			this.previousItem = this.currentItem;
			this.currentItem = this.currentItem + 1;
		} else {
			this.currentItem = 0;
			this.previousItem = this.numOfItems - 1;
		}
	},
	
	hideItems: function()
	{
		this.items.each(function(element) {
			Element.hide(element);
		}); 
	}
	
	
}
