function getSty(obj, id)
{
	return obj ? obj.style : null;
}

function x(obj, p)
{
	if (!isNaN(p)) 
	{
		getSty(obj).left=p+'px';
	}
	else
	{
		return parseInt(getSty(obj).left);
	}
}

function y(obj, p)
{
	if (!isNaN(p)) 
	{
		getSty(obj).top=p+'px';
	}
	else
	{
		return parseInt(getSty(obj).top);
	}
}

function w(obj, p)
{
	if (p)
	{
		getSty(obj).width=p+'px';
	}
	else
	{
		return obj.offsetWidth;
	}
}

function isQuoteVisible(id)
{
	var isVisible = false;
	for (var i=0; i<visQuotes && !isVisible; i++)
	{
		isVisible = (items[i] == id);
	}
	return isVisible;
}

function isAreaUsed(newTop, newHeight, oldIndex)
{
	var isUsed = false;
	for (var i=0; i<visQuotes && !isUsed; i++)
	{
		if (tops[i] && heights[i] && oldIndex != i)
		{
			isUsed = !(tops[i] > (newTop + newHeight + 2) || (tops[i] + heights[i]) < newTop - 2);
		}
	}
	return isUsed;
}

// *** QUOTATION SCROLLER ENGINE ***
function checkDivs()
{
	for (var i = 0; i < visQuotes; i++)
	{
		// Get a layer object if it doesn't exist, and work with it.
		if (!sDivs[i])
		{
			sDivs[i] = document.getElementById("quote"+i);
		}
		// Initialize directions array
		if (!sDirs[i])
		{
			sDirs[i] = 1;
		}

		// If it's moved offscreen to the left/right (or starting), set things in motion...
		if (sDirs[i] == 1 && x(sDivs[i]) < (0 - w(sDivs[i])) || sDirs[i] == -1 && x(sDivs[i]) > w(document.getElementById('scroller')))
		{
			// Choose a quote which is not visible already.
			var newquotenr = Math.floor(Math.random() * quotes.length);
			var iter = 0;	// Endlosschleife verhindern, in dem nach 1000 Iterationen abgebrochen wird
			while (isQuoteVisible(newquotenr) && iter < 1000)
			{
				newquotenr = Math.floor(Math.random() * quotes.length);
				iter++;
			}
			items[i] = newquotenr;
			sDirs[i] = dirs[newquotenr];

			// New random value to choose quote size+speed.
			var rnd = Math.random();

			// Scrolling increment varies from minSpeed to maxSpeed.
			speed[i] = minSpeed + rnd*(maxSpeed-minSpeed);

			// Likewise calculate a new fontsize and position.
			var fontSize = parseInt(minSize + rnd*(maxSize-minSize));

			// Position and layer it according to its size & speed (faster = higher).
			var topMax = maxTop - minTop - (document.all ? 5 : 20) - fontSize;
			var newTop = topMax * Math.random();
			iter = 0;
			while (isAreaUsed(newTop, fontSize, i) && iter < 1000)
			{
				newTop = topMax * Math.random();
				iter++;
			}
			// Top und Height inklusiv einem 2px Rand festhalten, damit die anderen Einträge nicht auf der gleiche Ebene laufen
			tops[i] = newTop-2;
			heights[i] = fontSize+4;
			y(sDivs[i], newTop);
			//getSty(sDivs[i]).zIndex = speed[i];

			// Choose a color (based on the random speed)
			var col = (sDirs[i] > 0) ? colors[Math.floor(rnd*colors.length)] : colors2[Math.floor(rnd*colors2.length)];

			// Format with SPAN for proper browsers.
			var str = '<span style="font-size: '+fontSize+'px; color: #'+col+'">'+quotes[newquotenr]+'</span>';

			// Write the content to a div, ensuring it doesn't wrap. Perhaps make this a link etc?
			x(sDivs[i], -2000);
			sDivs[i].innerHTML='<nobr>' + str + '</nobr>';
			
			// Off to the right/left it goes -- qPos records the current position as a floating point number.
			qPos[i] = (sDirs[i] > 0 ? (Math.max(0, w(document.getElementById('scroller')))) : (0-w(sDivs[i]))) + 50*Math.random()*sDirs[i];
		}

		// All items: Keep them moving left/right.
		qPos[i] -= speed[i] * sDirs[i];
		x(sDivs[i], qPos[i]);
	}

	// Again!
	setTimeout('checkDivs()', 50);
}

// *** START EDITING HERE ***
var quotes = new Array(), dirs = new Array(), c = 0;
fillQuotes();

// Just leave these, some global arrays are needed to remember quote details.
var sDivs = new Array(visQuotes);
var speed = new Array(visQuotes);
var qPos = new Array(visQuotes);
var tops = new Array(visQuotes);
var heights = new Array(visQuotes);
var items = new Array(visQuotes);
var sDirs = new Array(visQuotes);
for (var i=0; i<visQuotes; i++)
{
	tops[i] = -1;
	heights[i] = 0;
}

// Finally trigger quotation scrolling on page load.
appendOnloadHandler(checkDivs);
