// TODO: document this
function FeaturedItemsScroller
(
	sName,
	containerDivId,
	scrollDivId,
	leftId,
	rightId,
	autoPlay,
	playDelay
)
{
	m_sName = sName; // Instance name 

	// get the elements we are concerned with
	objContainer = document.getElementById(containerDivId);	// the containing div
	objDiv = document.getElementById(scrollDivId);			// the div that will be scrolled
	objLeft = document.getElementById(leftId);				// the item that will act as the "scroll left" button
	objRight = document.getElementById(rightId);			// the item that will act as the "scroll right" button
	
	// attach events to the buttons
	objLeft.onclick = CFeaturedItemScroller_ScrollLeft;
	objRight.onclick = CFeaturedItemScroller_ScrollRight;
	
	/***************private vars**************/
	
	var items = objDiv.getElementsByTagName("div");	// collection of items
	var itemCount = items.length;					// number of items that are being display
	
	var itemWidth = items[0].offsetWidth+2;			// width of each item - hardcoded margin of 2px - this value can be manually hardcoded.
	var divWidth = itemWidth*items.length + 100;	// the total width of the scrolling div + some extra for good luck!
	
	var itemsToScroll = itemCount - (objContainer.offsetWidth / itemWidth);	// the number of items to scroll - these are the items that are not displayed (overflowed) in the containing div
	
	var autoPlayInterval; // holdsautoplay interval thread - this is assigned in the Init(), and is cleared when autoplay is interrupted
	
	// keeps track of how many times (or items) we have scroll towards the right.  
	// timesScrolled should never be greater than itemsToScroll
	var timesScrolled = 0;

	// keeps track of how many pixels have scrolled
	var pixelsScrolled = 0;
	
	/*************END private vars************/
	
	// scrolls the objContainer to reveal items on the right
	this.MoveRight = function()
	{
		if(pixelsScrolled < itemWidth)
		{
			//objContainer.scrollLeft++;
			//pixelsScrolled++;
			
			var amountToScroll = Math.round((itemWidth-pixelsScrolled)*.5);
			objContainer.scrollLeft += amountToScroll;
			pixelsScrolled += amountToScroll;
			setTimeout(sName + '.MoveRight()', 30);
		}
		else
		{	
			pixelsScrolled = 0;
		}
	}
	
	// scrolls the objContainer to reveal items on the left
	this.MoveLeft = function()
	{
		if(pixelsScrolled < itemWidth)
		{
			//objContainer.scrollLeft--;
			//pixelsScrolled++;
			
			var amountToScroll = Math.round((itemWidth-pixelsScrolled)*.5);
			objContainer.scrollLeft -= amountToScroll;
			pixelsScrolled += amountToScroll;
			
			setTimeout(sName + '.MoveLeft()', 30);
		}
		else
		{
			pixelsScrolled = 0;
		}
	}
	
	// scrolls the object container to the start
	this.BackToStart = function()
	{
		if(objContainer.scrollLeft > 0)
		{
			var amountToScroll = Math.round((objContainer.scrollLeft)*.5);
			objContainer.scrollLeft -= amountToScroll;
			btsTo = setTimeout(sName + '.BackToStart()', 0);
		}
	}

	// navigate left event
	function CFeaturedItemScroller_ScrollLeft()
	{
		clearInterval(autoPlayInterval);// stop the autoplay;
		
		if(timesScrolled > 0)
		{
			//objContainer.scrollLeft+= -itemWidth;
			//setTimeout(sName + ".MoveLeft()", 0);
			eval(sName + ".MoveLeft()");
			timesScrolled--;
			return false;
		}
		else
		{
			return false;
		}
	}
	
	// navigate right event
	function CFeaturedItemScroller_ScrollRight()
	{
		clearInterval(autoPlayInterval);// stop the autoplay;
		
		if(timesScrolled < itemsToScroll)
		{
			//objContainer.scrollLeft+= itemWidth;
			//srTo = setTimeout(sName + ".MoveRight()", 1);
			eval(sName + ".MoveRight()");
			timesScrolled++;
			return false;
		}
		else
		{
			return false;	
		}
	}
	
	// navigate all the way to the first item
	function CFeaturedItemScroller_BackToStart()
	{
		BackToStart();
		timesScrolled = 0;
		return false
	}
	
	// progresss to the next item, and back to the start when it reaches the last item
	this.CFeaturedItemScroller_Play = function()
	{
		if(timesScrolled < itemsToScroll)
		{
			this.MoveRight();
			timesScrolled++;
		}
		else
		{
			this.BackToStart();
			timesScrolled=0;
		}
	}
	
	function Init()
	{
		// set the div that will be scrolled to it's maximum width so that its elements do not wrap to a new line
		objDiv.style.width = divWidth + "px";
		if(autoPlay)
		{
			autoPlayInterval = setInterval(sName +".CFeaturedItemScroller_Play()", playDelay);
		}
	}
	
	Init();
}
