/**
* Scroller script
* Enabled the horizontal scrolling of HTML in a DIV
*/

/**
* the object that contains the scroller
* @var object
*/
var containerObj = null;
/**
* the object that contains the content of the scroller
* @var object
*/
var contentObj = null;
/**
* the number of pixels that the scroller will move by each step
* @var int
*/
var speed = 4;
/**
* the increment to move the banners, this is either +/- speed
* @var int
*/
var increment = 0;
/**
* when true the scrolling will stop
* @var bool
*/
var stopScrolling = false;
/**
* the id of the timer
* @var int
*/
var timerID = null;
/**
* the width of the banners, this is how how much far the banners scroll per movement
* @var int
*/
var bannerWidth = 64;
/**
* the tamount of milliseconds between each step of the animation
* @var int
*/
var animTimePeriod = 20;



/**
* Initialises the objects
*/
function initialiseScroller(containerObjName, contentObjName) {

	// get references to html elements
	containerObj = document.getElementById(containerObjName);
	contentObj = document.getElementById(contentObjName);

	// get list of all img's in the content area
	if (contentObj.getElementsByTagName) {
		var imgElements = contentObj.getElementsByTagName("img");
	} else if (contentObj.all) {
		var imgElements = contentObj.all.tags("img")
	}
	// calculate the total length of all the images, and the starting left position
	totalLength = 0;
//	left = 0;
	for (i = 0; i < imgElements.length; i++) {
		totalLength += imgElements[i].width;
	}
//	left = ((parseInt(totalLength / bannerWidth) / 2) - 1) * bannerWidth

	totalLength = (imgElements.length / 2) * bannerWidth

	contentObj.style.width = totalLength +"px"
//	contentObj.style.left = -left +"px"
}


/**
* Stop scrolling at current position
*/
function stopScroll() {
	stopScrolling = true
}


/**
* Start the scroller scrolling right
*/
function startScrollRight() {
	
	stopScrolling = false
	if (timerID) clearTimeout(timerID);
	increment = -speed;
	scrollBanners()
}


/**
*
*/
function scrollBanners() {

	// if trying to scroll right, but already on far right banner then stop
	if (increment < 0 && (parseInt(contentObj.style.left) + parseInt(contentObj.style.width)) == bannerWidth) {
		return;
	}
	
	// if trying to scroll left, but already on far left banner then stop
	if (increment > 0 && parseInt(contentObj.style.left) >= 0) {
		return;
	}
	// move the banners
	contentObj.style.left = parseInt(contentObj.style.left) + increment + "px"

//	debug();

	if (stopScrolling == true && parseInt(contentObj.style.left) % bannerWidth == 0) {
//	if (parseInt(contentObj.style.left) % bannerWidth == 0) {
		// stop scrolling
	} else {
		timerID = setTimeout('scrollBanners()', animTimePeriod);
	}
}


/**
* Start the scroller scrolling left
*/
function startScrollLeft(indefinate) {
	
	stopScrolling = false
	if (timerID) clearTimeout(timerID);
	increment = speed;
	scrollBanners()
}


/**
* Display debug data to the screen
*/
function debug() {

	var txt = "stopScrolling = "+stopScrolling +"<br>"
	txt += "totalLength = "+ totalLength +"<br>"
	txt += parseInt(contentObj.style.left) % bannerWidth == 0 +"<br>";
	txt += "\na = "+ parseInt(parseInt(contentObj.style.left) + parseInt(contentObj.style.width)) +"<br>"

	txt += parseInt(contentObj.style.left) + parseInt(contentObj.style.width) +"<br>";

	txt += "contentObj.style.width = "+ contentObj.style.width +"<br>"
	txt += "contentObj.style.left = "+parseInt(contentObj.style.left) +"<br>"
	txt += "contentObj.style.left % 468 = "+parseInt(contentObj.style.left) % bannerWidth +"<br>"


	document.getElementById("debug").innerHTML = txt
}
