oRcsf.RegisterObject('Listviewer');
var RCSF_Project_Listviewer =
{
	   miListItemHeight 	: Number
	  ,miTotalHeight		: Number
	  ,miItemsPerRow		: 1
	  ,miStartOffset		: Number
	  ,msListName			: 'listitem'
	 
	,OnDomLoaded : function()
	{
		if($('btn_down')) $('btn_down').observe('click',this.ScrollDown.bind(this));
		if($('btn_up')) $('btn_up').observe('click',this.ScrollUp.bind(this));
	}

	/*
	 * Scroll the news list to the given ID
	 */
	,ScrollListToId : function(sId,iPosition)
	{
		if(Object.isUndefined(iPosition)) iPosition = 0;
		document.observe('dom:loaded',this.ScrollTo.bind(this,sId,iPosition));
		
		if(sId != 0)
			document.observe('dom:loaded',this.SetActive.bind(this,sId));
	}
	
	,SetActive : function(sId)
	{
		$(this.msListName + '_' + sId).addClassName('selected');
		
	}
	
	,ScrollTo : function(sId,iPosition) 
	{

		var a_nodes = $$('.list_item');

		var first_position 	= Element.viewportOffset(a_nodes.first()).top;
		var scroll_position = sId != 0 ? Element.viewportOffset($(this.msListName + '_' + sId)).top : first_position;
		
		var diff 			= -(scroll_position - first_position);
		
		var i_rows = Math.ceil(a_nodes.size() / this.miItemsPerRow);
		
		var i_visible_rows = Math.round($('listlink').getHeight() / this.miListItemHeight);

		this.miTotalHeight = i_rows * this.miListItemHeight * -1;
		
		diff = Math.max(diff, this.miTotalHeight);

		this.miStartOffset = i_visible_rows * this.miListItemHeight;
		
		this.miCurrentPosition = diff - this.miStartOffset;

		if(diff == 0)
			$$('#btn_up a').first().hide();
		
		if(this.miCurrentPosition <= this.miTotalHeight)
		{
			$$('#btn_down a').first().hide();
			
			if(i_rows < i_visible_rows)
				this.miCurrentPosition = -this.miStartOffset;
			else
				this.miCurrentPosition = this.miTotalHeight
		}
		
		if(!Object.isUndefined(iPosition) && iPosition < 0) this.miCurrentPosition = iPosition;
		$('listlist').setStyle({'top': this.miCurrentPosition + this.miStartOffset  + 'px'
								,'height' : -this.miTotalHeight + 'px'});
								
		if(this.miCurrentPosition == this.miStartOffset * -1)
			$$('#btn_up a').first().hide();
	}
	
	,ScrollUp : function()
	{
		if(this.miCurrentPosition < this.miStartOffset * -1)
		{
			$$('#btn_down a').first().show();
			
			this.miCurrentPosition += this.miListItemHeight;
			Effect.MoveBy('listlist',this.miListItemHeight,0,{duration : 0.5,queue : 'end'})
		}
		
		if(this.miCurrentPosition == this.miStartOffset * -1)
			$$('#btn_up a').first().hide();
	}
	
	,ScrollDown : function()
	{
		if(this.miCurrentPosition > this.miTotalHeight)
		{
			$$('#btn_up a').first().show();	
			this.miCurrentPosition -= this.miListItemHeight;
			
			new Effect.MoveBy('listlist',-this.miListItemHeight,0,{duration : 0.5,queue : 'end'})
		}
		
		if(this.miCurrentPosition <= this.miTotalHeight)
			$$('#btn_down a').first().hide();
	}
	
	/*
	 * 
	 */
	,LinkTo : function(oEl) 
	{
		this.moCore.Redirect(oEl.href + '/' + this.miCurrentPosition)
	}
};