var Newsticker = new Class({
	
	initialize: function(options)
	{
		this.box = options.container;

		if(this.box.getParent().getSize().y >= this.box.getParent().getScrollSize(this.box.getParent().getParent()).y)
		{
			/*
			options.prevButton.erase('href');
			options.nextButton.erase('href');
			*/
			options.prevButton.setStyle('display', 'none');
			options.nextButton.setStyle('display', 'none');
			return false;
		}
		
		this.fx = new Fx.Tween(this.box, {
			property: 'top',
			onComplete: this.fxComplete.bind(this)
		});
		
		this.enabled = true;
		this.autoScroll();
		
		var parentContainer = this.box.getParent().getParent();
		parentContainer.addEvent('mouseenter', this.stopScroll.bind(this));
		parentContainer.addEvent('mouseleave', this.autoScroll.bind(this));
		
		options.prevButton.addEvent('click', this.clickPrev.bind(this));
		options.nextButton.addEvent('click', this.clickNext.bind(this));
	},
	
	autoScroll: function()
	{
		this.fx.set('top', 0);
		this.timerId = this.next.periodical(4000, this);
	},
	
	stopScroll: function() {
		$clear(this.timerId);
	},
	
	clickPrev:function(e)
	{
		this.prev();
		return false;
	},
	clickNext:function(e)
	{
		this.next();
		return false;
	},
	
	next: function()
	{
		this.direction = 'up';
		var childs = this.box.getChildren();
		
		var firstElement = childs[0];
		
		this.box.grab(firstElement.clone());
		
		var nextElement = childs[1];
		var scrollAmount = nextElement.getPosition(this.box).y;

		this.fx.start(-scrollAmount);
	},
	
	prev: function()
	{
		this.direction = 'down';
	
		var lastElement = this.box.getLast();
		this.box.grab(lastElement.clone(), 'top');
		
		var childs = this.box.getChildren();
		this.fx.set(childs[1].getPosition(this.box).y * -1);
		this.fx.start(childs[0].getPosition(this.box).y);
	},
	
	fxComplete: function()
	{
		if(this.direction == 'up')
		{
			this.box.getFirst().destroy();
			this.fx.set('top', 0);
		} 
		else
		{
			this.box.getLast().destroy();
		}
	}
	
});