/**
 * The HorizontalScroller JavaScript class handles scrolling a specified parent-child DOM
 * element pair at the specified pixel increment and every specified millisecond interval
 * on the X axis.
 *
 * This class has been designed to work on DOM compliant browsers.
 *
 * @author Robert Alfaro <robert.alfaro@rhombus.com.au>
 * @version $Id: HorizontalScroller.js,v 1.2 2005/07/19 03:13:26 roberta Exp $
 * @copyright HorizontalScroller is copyright (C) 2005 rhombus solutions. All rights reserved.
 * @license The software is redistributable under the LGPL
 *    - http://www.opensource.org/licenses/lgpl-license.php
 *
 * The usage of this class is as follows
 * 1. Add the <script> tag referencing this file in the <HEAD> portion your document,
 * 2. Define the DOM elements that will contain your scroller in your document
 *    <div id="horiscroller">
 *        <div id="horiscroller_contents"></div>
 *    </div>
 * 3. Instantiate the JavaScript object. For example,
 *    var scroller = new HorizontalScroller( scrollerId, contentsId, delay, increment );
 */

HorizontalScrollers = {};

function HorizontalScroller( scrollerId, contentId, delay, increment )
{
	this.classname = "HorizontalScroller";
	this.nameIdentifier = "HorizontalScrollers." + scrollerId;
	this.scrollerElementId = scrollerId;
	this.scrollerElement = undefined;
	this.contentElementId =contentId;
	this.contentElement =undefined;
	this.delay = delay;
	this.increment = increment;
	this.initialised = false;
	this.scrollTimer = null;
	// Reset dimension and position data
	this.scrollerWidth = 0;
	this.contentElementWidth = 0;
	// Define class methods
	this.initialise = initialise;
	this.setDelay = setDelay;
	this.setIncrement = setIncrement;
	this.startScrolling = startScrolling;
	this.stopScrolling = stopScrolling;
	this.seekStart = seekStart;

	// Initialise scroller
	this.initialise();
}

function initialise()
{
	var scrollerElement = document.getElementById(this.scrollerElementId);
	var contentElement = document.getElementById(this.contentElementId);
	if ( scrollerElement != undefined && contentElement != undefined )
	{
		HorizontalScrollers[this.scrollerElementId] = this;
		this.scrollerWidth = ( scrollerElement.offsetWidth > 0 ? scrollerElement.offsetWidth : 0 );
		this.contentWidth = ( contentElement.offsetWidth > 0 ? contentElement.offsetWidth : 0 );
		this.scrollerElement = scrollerElement;
		this.contentElement = contentElement;
		this.seekStart();
		this.initialised = true;
	}
	else
	{
		alert("<b>An error has occured initilising the horizontal scroller. Either scroller and/or content element not defined.</b>");
	}
}

/* Sets the delay in milliseconds before the next scroll increment */
function setDelay( delay )
{
	this.delay = delay;
}

/* Sets the pixel increment for the scroller */
function setIncrement( increment )
{
	this.increment = increment;
}

/* Starts the scrolling in the specified direction */
function startScrolling( direction )
{
	if ( this.scrollTimer )
	{
		clearInterval(this.scrollTimer);
	}
	var leftPos = parseInt(this.contentElement.style.left);
	if ( direction == "left" )
	{
		if ( leftPos >= ( (this.contentWidth - this.scrollerWidth) * (-1) ) )
		{
			this.contentElement.style.left = leftPos - this.increment + "px";
		}
	}
	else if ( direction == "right" )
	{
		if ( leftPos <= 0 )
		{
			this.contentElement.style.left = leftPos + this.increment + "px";
		}
	}
	else
	{
		alert("The specified scroll direction " + direction + " was invalid. Allowed direction values are 'left' and 'right'.");
	}
	this.scrollTimer = setInterval( this.nameIdentifier + ".startScrolling('" + direction + "')", this.delay );
}

/* Stops scrolling by clearing scroll timer */
function stopScrolling()
{
	if ( this.scrollTimer != undefined )
	{
		clearInterval(this.scrollTimer);
	}
	this.scrollTimer = null;
}

/* Seeks to the start of the scroller */
function seekStart()
{
	this.contentElement.style.left = "0px";
}

