/*
Just add a tag with id "ticker" and put some html in it. 
This content of the tag will be scrolling in the direction you want.

ATTRIBUTES:				POSSIBLE VALUES:
Direction				"Horizontal" and "Vertical"
Speed					Speed < 0 < Speed
Debug					"True" or "False"
						When True, the overflow will be default and the borders will be visible...
						
Examples:
Direction="horizontal" Speed="20":		Scrolling from Right to Left
Direction="horizontal" Speed="-20":		Scrolling from Left to Right
Direction="vertical" Speed="20":		Scrolling from Bottom to Top
Direction="vertical" Speed="-20":		Scrolling from Top to Bottom

TODO:
- Build-in RSS-Feeds. 
  Adding an RSS-Attribute and an XSLT-Attribute to the tag will render the RSS-Feed and put it in the ticker.
*/

	var Ticker = null;
	var Direction = null;
	var Speed = 0;
	var IsPositiveSpeed = true;
	var Content;
	var ChildCount = 0;
	var Debug;
	
	var objChild = null;
	var intChildHeight = 0;
	var intChildWidth = 0;
	var objFirstChild = null;
	var objLastChild = null;
	var intChildCounter = 1;
	var moveWindowTimeout = null;
	var blnTickerStarted = false;
	
	var cScrollAmount = 1;

	function FindTicker()
	{
		if ((typeof(Ticker) != 'object') || (Ticker == null))
		{
			Ticker = document.getElementById("ticker");
			window.setTimeout("FindTicker()", 10);
		}
		else
		{
			InitTicker();
		}
	}
	
	function InitTicker()
	{
		SetDebugMode();
		SetStyle();
		SetDirection();
		SetSpeed();
		SetContent();
		switch (Direction.toUpperCase())
		{
			case "HORIZONTAL":
				AddChild();
				CalculateChildCount();
				for (var x=1; x < ChildCount; x++)
				{
					AddChild();
				}
				break;
			case "VERTICAL":
				AddChild();
				CalculateChildCount();
				//alert(ChildCount);
				for (var x=1; x < ChildCount; x++)
				{
					AddChild();
				}
				break;
			default:
				break;
		}

		if ((Speed != null) && (Direction != null))
			blnTickerStarted = true;
			
		MoveTicker();
		
		if (window.addEventListener) // Mozilla, Netscape, Firefox
		{
			Ticker.addEventListener('mouseover', DoTicker, false);
		}
		else
		{
			Ticker.attachEvent('onmouseover', DoTicker);
		}

	}
	
	function MoveTicker()
	{
		if (blnTickerStarted)
		{
			var obj, objLast, objFirst;
			for (var x = 0; x < Ticker.childNodes.length; x++)
			{
				obj = Ticker.childNodes[x];
				
				switch (Direction.toUpperCase())
				{
					case "HORIZONTAL":
						if (isNaN(parseInt(obj.style.left))) 
						{ 
							//this is the beginning of the scrolling part... something like initialization...
							obj.style.left = "0px";
						}
						else
						{
							if (IsPositiveSpeed)
							{
								//positive speed, so scrolling up...
								if ((obj.offsetLeft + obj.offsetWidth) < 0) 
									obj.style.left = parseInt(obj.style.left) + ((ChildCount) * parseInt(intChildWidth)) + "px";
								else
									obj.style.left = (parseInt(obj.style.left) - cScrollAmount) + "px";
							}
							else
							{
								//negative speed, so scrolling down...
								if (obj.offsetLeft >= Ticker.offsetWidth) 
									obj.style.left = parseInt(obj.style.left) - ((ChildCount) * parseInt(intChildWidth)) + "px";
								else
									obj.style.left = (parseInt(obj.style.left) + cScrollAmount) + "px";
							}
						}
						break;
					case "VERTICAL":
						if (isNaN(parseInt(obj.style.top))) 
						{ 
							//this is the beginning of the scrolling part... something like initialization...
							obj.style.top = 0 + "px";
						}
						else
						{
							if (IsPositiveSpeed)
							{
								//positive speed, so scrolling up...
								if ((obj.offsetTop + obj.offsetHeight) < 0) 
									obj.style.top = parseInt(obj.style.top) + ((ChildCount) * parseInt(intChildHeight)) + "px";
								else
									obj.style.top = (parseInt(obj.style.top) - cScrollAmount) + "px";
							}
							else
							{
								//negative speed, so scrolling down...
								if (obj.offsetTop >= Ticker.offsetHeight) 
									obj.style.top = parseInt(obj.style.top) - ((ChildCount) * parseInt(intChildHeight)) + "px";
								else
									obj.style.top = (parseInt(obj.style.top) + cScrollAmount) + "px";
							}
						}
						break;
				} //switch
			} //for
	
			//recall this function to keep the ticker moving...
			moveWindowTimeout = window.setTimeout("MoveTicker()", parseInt(Speed));
		}
	}
	
	function SetDebugMode()
	{
		var tmpDebug = Ticker.getAttribute("DEBUG");
		if (tmpDebug != null)
		{
			switch (tmpDebug.toUpperCase())
			{
				case "TRUE": Debug = true; break;
				case "FALSE": Debug = false; break;
				default: Debug = false; break;
			}
		}
		else
		{
			//attribute not found, but it's not mandatory
			//so set this property to false.
			Debug = false;
		}
	}
	
	function SetStyle()
	{
		if (!Debug)
			Ticker.style.overflow = 'hidden';
		else
			Ticker.style.border	= '1px solid #00f'; //blue border
			
		Ticker.style.position = 'relative';
	}
	
	function SetDirection()
	{
		Direction = Ticker.getAttribute("DIRECTION");
		if (Direction != null)
		{
			switch (Direction.toUpperCase())
			{
				case "HORIZONTAL":
				case "VERTICAL":
					//alert(Direction);
					//everything is oke now...
					break;
				default:
					alert("Please use \"HORIZONTAL\" or \"VERTICAL\" for the attribute \"DIRECTION\" in your Ticker-Tag!");
			}
		}
		else
		{
			alert('Please specify the attribute \"DIRECTION\" in your Ticker-Tag!\nThe possible values are \"HORIZONTAL\" or \"VERTICAL\"!');
		}
	}
	
	function SetSpeed()
	{
		Speed = Ticker.getAttribute("SPEED");
		if (Speed != null)
		{
			if ((isNaN(Speed)) || (Speed == ""))
			{
				alert("Please use signed integer values for the attribute \"SPEED\" in your Ticker-Tag!");
			}
			else
			{
				if (Speed < 0)
				{
					Speed = -1 * Speed;
					IsPositiveSpeed = false;
				}
				else
				{
					IsPositiveSpeed = true;
				}
			}
		}
		else
		{
			alert('Please specify the attribute \"SPEED\" in your Ticker-Tag!\nUsing negative values and Direction \"HORIZONTAL\" scrolls from left to right.\nUsing positive values and Direction \"HORIZONTAL\" scrolls from right to left.\nUsing negative values and Direction \"VERTICAL\" scrolls from top to bottom.\nUsing positive values and Direction \"VERTICAL\" scrolls from bottom to top.');
		}
	}

	function SetContent()
	{
		Content = Ticker.innerHTML;
	}
	
	function AddChild()
	{
		if (objChild == null)
		{
			Ticker.innerHTML = "";
			switch (Direction.toUpperCase())
			{
				case "HORIZONTAL":
					objChild = document.createElement("SPAN");
					objChild.style.whiteSpace = "nowrap";
					break;
				case "VERTICAL":
					objChild = document.createElement("DIV");
					break;
				default:
					break;
			}
			objChild.innerHTML = Content;
			if (Debug)
				objChild.style.border = "1px solid red";
			objChild.style.position = "relative";
		}

		var obj = objChild.cloneNode(true);
		obj.id = "child_" + intChildCounter;
		if (Debug)
			obj.innerHTML = intChildCounter + "<BR>" + obj.innerHTML;
		intChildCounter++;

		switch (Direction.toUpperCase())
		{
			case "VERTICAL":
				Ticker.appendChild(obj);
				Ticker.childNodes[Ticker.childNodes.length-1].style.position = "absolute";

				if (Ticker.childNodes.length == 1)
				{
					//first element
					Ticker.childNodes[0].style.left = 0 + "px";
					Ticker.childNodes[0].style.top = 0 + "px";
				}
				else
				{
					//next elements
					//Ticker.childNodes[Ticker.childNodes.length-1]    = The last element
					Ticker.childNodes[Ticker.childNodes.length-1].style.left = parseInt(Ticker.childNodes[Ticker.childNodes.length-2].offsetLeft) + "px";
					Ticker.childNodes[Ticker.childNodes.length-1].style.top = parseInt(Ticker.childNodes[Ticker.childNodes.length-2].offsetTop) + parseInt(intChildHeight) + "px";
				}

				break;
			case "HORIZONTAL":
				obj.style.top = 0;
				Ticker.appendChild(obj);
				Ticker.childNodes[Ticker.childNodes.length-1].style.position = "absolute";
				
				if (Ticker.childNodes.length == 1)
				{
					Ticker.childNodes[0].style.left = 0 + "px";
					Ticker.childNodes[0].style.top = 0 + "px";
				}
				else
				{
					Ticker.childNodes[Ticker.childNodes.length-1].style.left = parseInt(Ticker.childNodes[Ticker.childNodes.length-2].offsetLeft) + parseInt(intChildWidth) + "px";
					Ticker.childNodes[Ticker.childNodes.length-1].style.top = parseInt(Ticker.childNodes[Ticker.childNodes.length-2].offsetTop) + "px";
				}

				break;
			default:
				break;
		}
		
		//we need the width of a childNode
		intChildWidth = parseInt(Ticker.childNodes[0].offsetWidth);
		intChildHeight = parseInt(Ticker.childNodes[0].offsetHeight);
	}
	
	function CalculateChildCount()
	{
		switch (Direction.toUpperCase())
		{
			case "HORIZONTAL":
				var tmpTickerWidth = Ticker.offsetWidth;
				var tmpChildWidth = Ticker.childNodes[0].offsetWidth;
				//round to the ceiling
				ChildCount = parseInt(tmpTickerWidth / tmpChildWidth) + 2;
				break;
			case "VERTICAL":
				var tmpTickerHeight = Ticker.offsetHeight;
				var tmpChildHeight = Ticker.childNodes[0].offsetHeight;
				//round to the ceiling
				ChildCount = parseInt(tmpTickerHeight / tmpChildHeight) + 2;
				break;
			default:
				break;
		}
	}
	
	function DoTicker(e)
	{
		if (Ticker)
		{
			var intTickerLeft = parseInt(Ticker.offsetParent.offsetLeft) + parseInt(Ticker.offsetLeft) + "px";
			var intTickerTop = parseInt(Ticker.offsetParent.offsetTop) + parseInt(Ticker.offsetTop) + "px";
			var intTickerWidth = parseInt(Ticker.offsetWidth) + "px";
			var intTickerHeight = parseInt(Ticker.offsetHeight) + "px";
	
			if (blnTickerStarted)
			{
				if (moveWindowTimeout == null)
				{
					if ((intTickerLeft > e.clientX) || ((intTickerLeft + intTickerWidth) < e.clientX) || (intTickerTop > e.clientY) || ((intTickerTop + intTickerHeight) < e.clientY))
						MoveTicker();
				}
				else
				{
					if ((intTickerLeft < e.clientX) && ((intTickerLeft + intTickerWidth) > e.clientX) && (intTickerTop < e.clientY) && ((intTickerTop + intTickerHeight) > e.clientY))
					{
	//					alert('pausing');
						window.clearTimeout(moveWindowTimeout);
						moveWindowTimeout = null;
					}   
				}
			}
		}
	}

	if (window.addEventListener) // Mozilla, Netscape, Firefox
	{
		window.addEventListener('load', FindTicker, false);
		window.addEventListener('mousemove', DoTicker, false);
	}
	else
	{
		window.attachEvent('onload', FindTicker);
		document.attachEvent('onmousemove', DoTicker);
	}

