

var NewsModule = Class.create({
	
	element: null,
	menu: null,
	current_index: null,
	rotate_intervalID: null,
	
	initialize: function( element )
	{
		this.element = $(element);
		if (!this.element) throw(Effect._elementDoesNotExistError);
		
		this.setup();
	},
	
	setup: function()
	{
		this.menu = $(this.element.getElementsBySelector('ul.menu')[0]);
		var scope = this, i = 0, nbElmts = this.menu.childElements().length, liHeight = Math.floor(this.element.getHeight()/nbElmts);
		
		$A(this.menu.childElements()).each(
			function(li)
			{
				li.setStyle( {height:liHeight+'px', lineHeight:liHeight+'px' } );
				$(li.getElementsByTagName('a')[0]).onclick = function()
				{
					scope.scroll( this );
					return false;
				};
				i++;
			}
		);
		
		// Init arrow & content scroll:
		this.setPosition(nbElmts-1);
		
		setTimeout( function() {
			this.scroll( this.menu.getElementsByTagName('a')[0] )
		}.bind(this), 1.5*1000 );
	},
	
	scroll: function(anchor)
	{
		if ( !Object.isElement(anchor) )
			throw (Effect._elementDoesNotExistError);
		
		var li = $(anchor).up().up(), liHeight = li.getHeight(), y, yScroll;
		
		// Find index of the current anchor:
		this.current_index = this.menu.childElements().indexOf(li);
		
		// Calculate y position of arrow background & scrollOffset of content:
		y = Math.floor( (liHeight*(this.current_index+1)) - (liHeight/2) - (this.arrowHeight/2) );
		yScroll = $(this.infoContainerID).getHeight() * this.current_index;
		
		// Animate:
		new Effect.ScrollVertical( this.infoContainerID, {to: yScroll} );
		new Effect.MoveBackground( this.arrowID, {yto: y} );
	},
	
	setPosition: function( index )
	{
		var liHeight = this.menu.down().getHeight(), y, yScroll;
		y = Math.floor( (liHeight*(index+1)) - (liHeight/2) - (this.arrowHeight/2) );
		yScroll = $(this.infoContainerID).getHeight() * index;
		
		$(this.arrowID).setStyle({ backgroundPosition: '0px '+y+'px' });
		$(this.infoContainerID).scrollTop = yScroll;
	}
});

// Constants:
Object.extend( NewsModule.prototype, {
	arrowHeight: 25,
	arrowID: 'infoBox_arrow',
	infoContainerID: 'infoContainer'
});
