// JavaScript Document
function whiteScroller(a_childHeight,a_parentContainer,a_parentList,a_moreButt,a_childrenList)
{
	var t = this;
	t.childHeight = a_childHeight;	
	t.parentContainer = a_parentContainer;
	t.parentList = a_parentList;
	t.moreButt = a_moreButt;
	t.childrenList = a_childrenList;
	t.currentItem = 0;
	t.childCount = t.childrenList.length;
	t.animation = new Fx.Style(a_parentList, 'margin-top', {wait:true,duration:300});

	//Gets viewport height and the amount of children it can show at that point
	t.arrangeItems = function()
	{
		var totalHeight = window.getHeight(); //mootools return height call
		//alert("totalHeight  "+totalHeight);
		var currentHeight = 0;
		t.moreButt.setStyle("display", "none");
		for( var x=0;x<t.childCount;x++ )
		{
			var child = t.childrenList[x];			
			if(currentHeight + t.childHeight >= totalHeight)
			{	
				t.moreButt.setStyle("display", "block");
				currentHeight -= t.childHeight;
				//alert("currentHeight  "+currentHeight);
				break;
			}else
			{
			    currentHeight += /*child.offsetTop +*/ child.offsetHeight //+ t.parentList.offsetTop;
			}
		}
		//alert("currentHeight 3  "+currentHeight);
		//alert("t.parentList.offsetTop  "+t.parentList.offsetTop);
		//alert("child.offsetTop  "+child.offsetTop);
		//alert("parent list height " + t.parentList.offsetHeight + " will go to " + currentHeight+"  parent container  "+t.parentContainer.offsetHeight);
		t.setContainerHeight(currentHeight);
	}
	
	t.moreButtonVisibility =  function()
	{
	  return (t.moreButt.getStyle("display") == "block")  
	}
	
	//Sets the height of parent divs
	t.setContainerHeight = function(height)
	{
		t.parentContainer.setStyle("height", height);
	}
	
	//checkItemReize
	t.checkItemResize = function()
	{
	    t.arrangeItems();
		var maxItem = t.childCount - t.getCurrentChildrenAmount();	
		var gotoItem = t.currentItem > maxItem ? maxItem : t.currentItem ;
		//gotoItem = t.getTotalHeight() <= window.getHeight() ? 0 : gotoItem;
		if(gotoItem!=t.currentItem)t.scrollItems(gotoItem);
		
	}
	t.getTotalHeight = function()
	{
	     t.arrangeItems();
	    //alert("t.moreButtonVisibility()   "+t.parentContainer.offsetHeight+"  child  "+t.childHeight+"  lsist height  "+ t.parentList.offsetHeight);
	    return t.moreButtonVisibility() ? t.parentContainer.offsetHeight /*- t.childHeight*/ : t.parentList.offsetHeight; 
	}
	//getCurrentChildrenAmount
	t.getCurrentChildrenAmount = function() 
	{
		//var myParentHeight = t.moreButtonVisibility() ? (t.parentContainer.offsetHeight) - t.childHeight : t.parentContainer.offsetHeight; 
		return Math.floor(t.getTotalHeight()/t.childHeight);
	}
	// Checks item currently visible at the top of the list
	t.getActualCurrentItem = function() 
	{
		return Math.round(Math.abs(t.parentList.offsetTop/t.childHeight));
	}
	
	// Next button
	t.gotoNextItems = function() 
	{
		var nextItem = t.currentItem + t.getCurrentChildrenAmount();
		t.scrollItems(nextItem);
	}
	// Previous button
	t.gotoPrevItems = function() 
	{
		var nextItem = t.currentItem - t.getCurrentChildrenAmount();
		t.scrollItems(nextItem);
	}
	
	
	// Do the animation
	t.scrollItems = function(gotoItem)
	{
	    t.arrangeItems();
		var maxItem = t.childCount - t.getCurrentChildrenAmount();	
		gotoItem = gotoItem <=0? 0 : gotoItem >maxItem ? maxItem : gotoItem ;
		var actualItem = t.getActualCurrentItem();
		if(gotoItem!=t.currentItem || actualItem!=gotoItem)
		{
			t.currentItem = gotoItem;
			var gotoPoint = -t.childHeight*t.currentItem;
			t.animation.start(t.parentList.offsetTop, gotoPoint);
		}
	}
}