/*
	** author
	** testet on:
*/

	//constants
		fontWidthPerc = 0.75		//estimated percentage of width relative to height of font. use to calculate ticker width
							//choose to big -> ticker gets to long and horizontal scroll bars will appear
							//choose to small -> text in ticker will swap (troubles with long words in ticker
		fontNetscapePerc = 0.9		//estimated percentage Netscape displays fonts smaller than MSIE

	//global variables
		helpclipleft = 0			//clip value at left side of ticker
		helpclipright  = 0		//clip value at right side of ticker
		actContent = 0			//actual index in content array
		actCharIndex = 0			//actual index of character in active content array
		actStartIndex = 0			//in case of ticker content is HTML - start Index of pure Text for scrolling
		actEndIndex = 0			//in case of ticker content is HTML - end Index of pure Text for scrolling
		actLength=0				//length of pure text part in content
		

	//starts ticker
		function startTicker () {
			//for browser which can display layers and are able to clip them correctly
			if (Number(navigator.appVersion.substring(0,1)) >= 4 && !(navigator.appVersion.indexOf ('Macintosh')>-1 && navigator.appName.indexOf('Explorer')>-1)) {
				initializeTicker()
				setInterval("rollTicker()",tintervall)
			}
		}

	//initializes ticker parameter
		function initializeTicker () {
			resetTicker()
			
			//additional settings whick only have to be performed once
			setVisibility ("show")
			setTopPosition (ttop)
			setBgColor (tbgcolor)
		}

	//resets ticker parameter
		function resetTicker () {
			helpclipleft = 0
			helpclipright = 0
			setLeftPosition (tright)
			setWidth (0)
			setClipLeft(0, "rect (0px 0px " + theight +"  px 0px)")
			setClipRight (0, "rect (0px 0px " + theight +"  px 0px)")
			setActData ();
			setHTMLContent ()
		}

	//moves ticker
		function rollTicker () {

			//get current position
			currLeft = getLeftPosition()
			currRight = getRightPosition()
			
			//ticker has not finished round
			if (currRight >= tleft) {

				//move ticker
				setLeftPosition (currLeft - tstep)

				//set ticker content dynamically to avoid horizontal scroll bar in MSIE
				if ((currRight - tfontSize*getFontSize()) < tright)
					setHTMLContent ()

				//enlarge ticker 
				if ((currRight >= tright) || (actCharIndex < actLength -1))
					clipLayer ("right")

				//shrink ticker
				if (currLeft <= tleft)
					clipLayer ("left")
			}

			//enf of ticker has reached left border
			else {
				//load new content into ticker
				actContent ++
				if (actContent == content.length)
					actContent = 0

				//reset ticker parameter
				resetTicker()
			}
		}

	//clips layer depending on position
		function clipLayer (mode) {
			if (mode == "left") {
				helpclipleft = helpclipleft + tstep
				clip = "rect(0px " + helpclipright +" px "+ theight+"px "+helpclipleft+"px)"
				setClipLeft (helpclipleft, clip)
			}
			else {
				helpclipright = helpclipright + tstep
				clip = "rect(0px " + (helpclipright) +  "px "+ theight+"px "+ helpclipleft + " px)"
				setClipRight (helpclipright, clip)
			}
		}

//browser specific functions

	//sets new left position of Layer
		function setLeftPosition (newLeft) {
			if (document.all)
				document.all.ticker.style.left = newLeft
			else
				document.getElementById("ticker").style.left = newLeft
		}

	//sets new top position of Layer
		function setTopPosition (newTop) {
			if (document.all)
				document.all.ticker.style.top = newTop
			else
				document.getElementById("ticker").style.top = newTop
		}

	//sets new width of layer
		function setWidth (newWidth) {
			if (document.all){
				document.all.ticker.style.width = newWidth
			}else{
				document.getElementById("ticker").style.width = newWidth				
			}
		}

	//sets clip of layer
		function setClipLeft (clipNumber, clipText) {
			if (document.all) {
				document.all.ticker.style.clip = clipText
			}
			else {
				document.getElementById("ticker").style.clip.left = clipNumber
			}
		}

	//sets clip of layer
		function setClipRight (clipNumber, clipText) {
			if (document.all) {
				document.all.ticker.style.clip = clipText
			}
			else {
				document.getElementById("ticker").style.clip.right = clipNumber
			}
		}

	//sets visibility of layer
		function setVisibility (newVisibility) {
			if (document.all) 
					document.all.ticker.style.visible =  newVisibility
			else 
					document.getElementById("ticker").style.visible = newVisibility
		}

	//set new background Color of Layer
		function setBgColor (newBgColor) {
			if (newBgColor) {
				if (document.all)  
					document.all.ticker.style.backgroundColor = newBgColor
				else
					document.getElementById("ticker").style.backgroundColor = newBgColor
			}
		}

	//returns left position of Layer
		function getLeftPosition () {
			if (document.all) {
				currLeft = document.all.ticker.style.left
				currLeft = currLeft.substr (0,currLeft.length-2)	//cut off "px"
			}
			else {
				currLeft = document.getElementById("ticker").style.left
				currLeft = currLeft.substr (0,currLeft.length-2)	//cut off "px"
			}
			return currLeft
		}

	//returns font size of Layer
		function getFontSize () {
			if (document.all) {
				currSize = document.all.ticker.style.fontSize
				currSize = currSize.substr (0,currSize.length-2)	//cut off "px"
			}
			else {
				currSize = document.getElementById("ticker").style.fontSize
				currSize = currSize.substr (0,currSize.length-2)
			}
			if (!currSize || Number(currSize)=="NaN" || Number(currSize)==0){
				currSize=tfontSize
			}
			return (Number(currSize) * fontWidthPerc) 		
		}

	//returns width
		function getWidth () {
			//currWidth=0
			if (document.all) {
				currWidth = document.all.ticker.style.width
				currWidth = currWidth.substr (0,currWidth.length-2)	//cut off "px"
			}
			else { 	
				currWidth = document.getElementById("ticker").style.width
				currWidth = currWidth.substr (0,currWidth.length-2)	//cut off "px"
			}
			return currWidth
		}

	//returns right position of Layer
		function getRightPosition () {
			return (Number(getLeftPosition()) + Number(getWidth()))
		}

	//sets global data concerning actual ticker content
		function setActData () {
			actCharIndex  = 0
			actStartIndex = content[actContent].indexOf(tstartmarker)+6
			actEndIndex = content[actContent].lastIndexOf(tendmarker)+1
			actLength = content[actContent].length - actStartIndex - (content[actContent].length-actEndIndex);
		}

	//writes HTML content into ticker
		function setHTMLContent () {
			//if end of content string is not reached
			if (actCharIndex < actLength -1) {
				actCharIndex ++;
				if (document.all) {
					document.all.ticker.innerHTML =  content[actContent].substr(0,actStartIndex) + content[actContent].substr(actStartIndex,actCharIndex) + content[actContent].substr(actEndIndex,actLength-actEndIndex)
					setWidth (Number(getWidth()) + Number(getFontSize())) //can only be read if it is set manually :-(
				}
				else {
					document.getElementById("ticker").innerHTML =  content[actContent].substr(0,actStartIndex) + content[actContent].substr(actStartIndex,actCharIndex) + content[actContent].substr(actEndIndex,actLength-actEndIndex)
					/*document.ticker.document.write(content[actContent].substr(actStartIndex,actCharIndex));
					document.ticker.document.write(content[actContent].substr(actEndIndex,actLength-actEndIndex));
					document.ticker.document.close();
					setWidth (Number(getWidth()) + Number(getFontSize()*fontNetscapePerc)) //can only be read if it is set manually :-(*/
					setWidth (Number(getWidth()) + Number(getFontSize()));
				}

			}
		}

	//writes HTML content into ticker
		function setMarqueeContent () {
			if (document.all) {
				document.writeln("<MARQUEE scrollamount=\"" + tstep + "\" scrolldelay= \"" + tintervall +"\">");
				for (i=0; i< content.length; i++) {
					document.write("		                      ");  //seperator between contents
					document.write(content[i])
				}
				document.writeln("</MARQUEE>");
			}
		}
