
// WebTicker by Mioplanet
// www.mioplanet.com

	// NOTE: For now, I'm only creating a single clone of the content. 
	// If the width of the content is less than the width of its bounding element, 
	// this will not appear to be an infinite loop.
	// In order to do this, you need to get the width of the bounding element and 
	// duplicate the ticker's content so that the collective widths of all the copies
	// is greater than the width of the bounding element. Then you need to keep track
	// of where all of them are.

TICKER_CONTENT = document.getElementById("TICKER").innerHTML;
 
TICKER_RIGHTTOLEFT = true;
TICKER_SPEED = 1;
TICKER_STYLE = "font-family:Arial; font-size:12px; color:#444444";
TICKER_PAUSED = false;

TICKER_WIDTH = document.getElementById("TICKER").parentNode.style.width;

CONTENT_WIDTH = 0;

ticker_start("TICKER");
	
function ticker_start(id) {
	var offsetCounter = 0;
	var tickerSupported = false;

	var tickerElement = document.getElementById(id);
	
//	window.alert("scrollLeft: " + tickerElement.scrollLeft + ", scrollWidth: " + tickerElement.scrollWidth + "\n offsetLeft: " + tickerElement.offsetLeft + ", offsetWidth: " + tickerElement.offsetWidth + "\n style left: " + tickerElement.style.left);

	// IE
	if (navigator.userAgent.indexOf("MSIE")!=-1 && navigator.userAgent.indexOf("Opera")==-1) 
	{
		tickerElement.innerHTML = "<DIV nowrap='nowrap' style='border: 0; padding: 0; margin: 0;'><SPAN style='"+TICKER_STYLE+"' ID='TICKER_BODY'></SPAN></DIV>";
		tickerSupported = true;
	}
	// Firefox
	if (navigator.userAgent.indexOf("Firefox")!=-1 || navigator.userAgent.indexOf("Safari")!=-1 || navigator.userAgent.indexOf("Opera")!=-1 || navigator.userAgent.indexOf("Mozilla")!=-1) 
	{
		tickerElement.innerHTML = "<TABLE cellspacing='0' cellpadding='0'><TR><TD nowrap='nowrap'><SPAN style='"+TICKER_STYLE+"' ID='TICKER_BODY'>&nbsp;</SPAN></TD></TR></TABLE>";
		tickerSupported = true;
	}
	
	if (!tickerSupported) 
	{
		tickerElement.outerHTML = ""; 
	}
	else 
	{
		document.getElementById("TICKER_BODY").innerHTML = TICKER_CONTENT;
		tickerElement.style.display  = "block";
		tickerElement.style.position = "absolute";

//		window.alert("scrollLeft: " + tickerElement.scrollLeft + ", scrollWidth: " + tickerElement.scrollWidth + "\n offsetLeft: " + tickerElement.offsetLeft + ", offsetWidth: " + tickerElement.offsetWidth + "\n style left: " + tickerElement.style.left);

		CONTENT_WIDTH = tickerElement.scrollWidth;

		tickerElement.style.left = 0 + 'px';
		
		tickerElement.parentNode.style.position = "relative";
		tickerElement.parentNode.style.top      = '0px';
		tickerElement.parentNode.style.left     = '0px';
		tickerElement.parentNode.style.overflow = "hidden";
		tickerElement.parentNode.style.width    = TICKER_WIDTH;
		
		var shadow = document.getElementById("TICKER_SHADOW");
		
		shadow.ID               = "TICKER_SHADOW";
		shadow.style.display    = "block";
		shadow.style.position   = "absolute";

		shadow.innerHTML        = tickerElement.innerHTML;
		shadow.style.left       = (TICKER_RIGHTTOLEFT ? PixelToInt(tickerElement.style.left) + tickerElement.offsetWidth : PixelToInt(tickerElement.style.left) - shadow.offsetWidth) + 'px';
		shadow.style.top        = '0px';

		tickerElement.parentNode.appendChild(shadow);
		TICKER_tick();
	}
}


function TICKER_tick() {
	
	var tick_array = new Array(2);
	tick_array[0] = document.getElementById("TICKER");
	tick_array[1] = document.getElementById("TICKER_SHADOW");
	
	for(i=0; i<tick_array.length; i++)
	{
		element = tick_array[i];
		prev = tick_array[((tick_array.length - 1) - i) % tick_array.length];
		
		if (element == null)
			continue;
		
		// ScrollWidth is the total width of the element, including area hidden behind scrollbars.
		// ScrollLeft is the difference between the actual left side of the element, and the left edge of the element that's currently in view.
//		document.getElementById("infolabel").innerHTML = "<h4>TICKER</h4>offsetwidth = " + tick_array[0].clientWidth + " scrollwidth = " + tick_array[0].scrollWidth + " left = " + tick_array[0].style.left + "<br /><h4>TICKER_SHADOW</h4>offsetwidth = " + tick_array[1].clientWidth + " scrollwidth = " + tick_array[1].scrollWidth + " left = " + tick_array[1].style.left;
		
		
		if(!TICKER_PAUSED) 
		{
			position = PixelToInt(element.style.left) + TICKER_SPEED * (TICKER_RIGHTTOLEFT ? -1 : 1);
			element.style.left = position + 'px';
		}
	
		if(TICKER_RIGHTTOLEFT && (PixelToInt(element.style.left) <= -CONTENT_WIDTH)) 
		{
			position = CONTENT_WIDTH;
			element.style.left = position + 'px';
		}
		
		if(!TICKER_RIGHTTOLEFT && PixelToInt(element.style.left) >= PixelToInt(TICKER_WIDTH)) 
		{
			position = prev.offsetLeft - CONTENT_WIDTH;
			element.style.left = position + 'px';
		}
	}
	
	window.setTimeout("TICKER_tick()", 30);
}

function PixelToInt(value)
{
	if (typeof(value) < 0)
		return 0;
		
	return parseInt(value.substr(0, value.length - 2));
}