var mainDivId = 'all';
var mainDivWidth = 0;
var mainDivWidthCheck = 0;
var numberOfSubDivs = 0;
var subDivs; /* this will be a collection element */
var arrayOfObjects = new Array();
var mouseOverElement = false;

var actual = 0;
var step = 1;
var speed = 15;
var delay = 15;


window.onload = function()
{
	setMainDivWidth();
	countDivs();
	assignDivObjectsToArray();
	assignMouseEvents();
	setPassPoints();
	startScroll();
}


function setMainDivWidth()
{
	var allDiv = document.getElementById(mainDivId);
	mainDivWidth = (allDiv.offsetWidth);
}


function countDivs()
{
	var allDiv = document.getElementById(mainDivId);
	subDivs = allDiv.getElementsByTagName("div");
	numberOfSubDivs = subDivs.length;
}


function assignDivObjectsToArray()
{
	for (var i = 0; i < numberOfSubDivs; i++) 
	{
		var selfRefrenceToObject = document.getElementById(subDivs[i].id);
		arrayOfObjects[i] = new divElement(subDivs[i].id, subDivs[i].offsetWidth, selfRefrenceToObject, i);	
	}
}


function divElement(name, width, refrenceToSelf, indexInObjectsArray) {
    this._name = name;
	this._width = width;
	this._refrenceToSelf = refrenceToSelf;
	this._indexInObjectArray = indexInObjectsArray;
	this._indexOfNextObject = findNextElement(this._indexInObjectArray);
	this._passPoint = 0;
	this._visibleOnScreen = false;
	this._tick;
	this._refrenceToSelf.style.left = (mainDivWidth + "px");
}


divElement.prototype.move = function()
{	
	index = this._indexInObjectArray;

	if (mouseOverElement == true)
	{
		this._tick = setTimeout("arrayOfObjects[index].move()",delay);
		return;	
	}

	doCompare();
	nextElement = this._indexOfNextObject;
	var leftPos = parseInt(this._refrenceToSelf.style.left);
	this._refrenceToSelf.style.left = parseInt(this._refrenceToSelf.style.left) - step + "px";
	this._visibleOnScreen = true;

	if (leftPos >= this._passPoint)
	{
		this._tick = setTimeout("arrayOfObjects[index].move()",delay);
	}
	else if (leftPos > (this._passPoint - this._width))
	{
		this._tick = setTimeout("arrayOfObjects[index].move()",delay);
		arrayOfObjects[nextElement]._visibleOnScreen = true;
		arrayOfObjects[nextElement]._refrenceToSelf.style.left = parseInt(arrayOfObjects[nextElement]._refrenceToSelf.style.left) - step + "px";
	}
	else if (leftPos <= (this._passPoint - this._width))
	{
		this.reset();
		arrayOfObjects[nextElement]._tick = setTimeout("arrayOfObjects[nextElement].move()",delay);
	}
}


divElement.prototype.reset = function()
{
	this._refrenceToSelf.style.left = (mainDivWidth + "px");
	this._visibleOnScreen = false;
}


function findNextElement(intIndex)
{
	if ((intIndex + 1) == numberOfSubDivs)
	{
		return 0;
	}
	else
	{
		return intIndex + 1;
	}
}


function assignMouseEvents()
{
	for (m = 0; m < numberOfSubDivs; m++) 
	{
		subDivs[m].onmouseover = function(){mouseOverElement = true;};
		subDivs[m].onmouseout = function(){mouseOverElement = false;};
	}
}


var setPassPoints = function()
{	
	for (var g = 0; g < numberOfSubDivs; g++) 
	{
		var divWidthVar = arrayOfObjects[g]._width + Math.round(mainDivWidth / 10.5);
		if (divWidthVar > mainDivWidth)
		{
			arrayOfObjects[g]._passPoint = mainDivWidth - (arrayOfObjects[g]._width + Math.round(arrayOfObjects[g]._width / 5));	
		}
	}
	return 0;
}

var doCompare = function()
{
	var tempAllDiv = document.getElementById(mainDivId);
	mainDivWidthCheck = (tempAllDiv.offsetWidth);
	if (mainDivWidthCheck != mainDivWidth)
	{
		setPassPoints();
		mainDivWidth = mainDivWidthCheck;
		setCurrentLeft();
	}
}


var setCurrentLeft = function()
{
	for (var z = 0; z < numberOfSubDivs; z++)
	{
		if (arrayOfObjects[z]._visibleOnScreen != true)
		{
			arrayOfObjects[z]._refrenceToSelf.style.left = (mainDivWidth + "px");
		}
	}
}


function startScroll()
{
	arrayOfObjects[0].move();
}


