/***************************************************************************
 *
 *   Ticker Scrolling Script
 *   Paul Saulnier - http://www.PaulSaulnier.ca
 *
 *   Please ask for permission before using this script on another web site
 *
 ***************************************************************************/

	var nSect = 0;
	var minTop = 0;
	var maxTop = 0;
	var nItem = -1;
	var timeout = 0;
	var pauseScroll = 0;
	var runningChange = 0;
	var count = 0;
	var moveDelta = 215;

	function pauseTickerScroll(){
		pauseScroll = 1;
	}
	function resumeTickerScroll(){
		pauseScroll = 0;
	}	
	function scrollUpCountdown(id, count, slideChange, reverse ){
		if ( pauseScroll == 1 ) {
			setTimeout("scrollUpCountdown('"+id+"', "+(count)+", "+slideChange+", "+reverse+");");		
		}else{
			var step = 3;
			var top = document.getElementById(id).style.top.replace("px","");
			if ( top == 'NaN' ) { top = 0; }

			var content = document.getElementById("tickerBlockContent"+nSect);

			if ( reverse != 1 && (content.offsetTop+content.offsetHeight) <= 0 ) {
				changeSlide();
				return;
			}
			/* DISABLED WITH SEEMINGLY NO NEGATIVE EFFECTS *
			if ( false && !slideChange && (content.offsetTop+content.offsetHeight) <= 200 && content.offsetTop <= 0 ) {
				runningChange = 0;
				clearTimeout(timeout);
				timeout = setTimeout("changeTickerBlock()",10000);
				return;
			} */

			if ( count <= 0 || ( reverse == 1 && (top) >= 0) ) {
				clearTimeout(timeout);
				timeout = setTimeout("changeTickerBlock()",10000);
				runningChange = 0;

			}else{
				if ( reverse == 1 ) {
					document.getElementById(id).style.top = (top*1 + step*1)+"px";
				}else{
					document.getElementById(id).style.top = (top - step)+"px";
				}

				setTimeout("scrollUpCountdown('"+id+"', "+(count-step)+", "+slideChange+","+reverse+");");
			}
		}		
	}

	// Skips ticker blocks
	function skipTickerBlock(delta){
		if ( runningChange == 1 ) { return; }
		clearTimeout(timeout);
		runningChange = 0;

		if ( delta == -1 ) {
			// Going back one block. First see if we can simply move the position up
			var moveBy = moveDelta / 2;
			var id = "tickerBlockContent"+nSect;
			var top = document.getElementById(id).style.top.replace("px","")*1;
			top = top + moveBy;
			if ( top >= moveBy ) {
				if ( nSect > 1 ) {
					if ( nSect > 0 ) {
						document.getElementById("tickerBlockContent"+nSect).style.visibility = "hidden";
						document.getElementById("tickerBlockContent"+nSect).style.position = "absolute";
						document.getElementById("tickerBlockContent"+nSect).style.top = "0px";
					}
					nSect = nSect - 1;
					nItem = 1;
					document.getElementById("tickerBlockContent"+nSect).style.top = "0px";
					document.getElementById("tickerBlockContent"+nSect).style.visibility = "visible";
					document.getElementById("tickerBlockContent"+nSect).style.position = "relative";
					document.getElementById("tickerBlockTitle").innerHTML = 
						document.getElementById("tickerBlockContentTitle"+nSect).innerHTML;
				}
			}else{
				// Can stay on the same section, just move to a new block
				//if ( top <= moveBy && top > 0 ) top = 0;
	//			document.getElementById(id).style.top = top+"px";
				scrollUpCountdown(id,  moveBy, 0, 1);
			}
			clearTimeout(timeout);
			timeout = setTimeout("changeTickerBlock()",10000);

		}else if ( delta == 1 ) {
			changeTickerBlock();
			
		}
		
	}

	function changeTickerBlock(){
		if ( runningChange == 1 ) { return; }
		runningChange = 1;
		if (timeout != 0 ) { clearTimeout(timeout); }

		if ( minTop <= 0 ) {
			minTop = document.getElementById("tickerBlockContentItem1.1").offsetTop;
			maxTop = minTop + moveDelta;	
		}

		//
		// Determine whether we need to advance the slide or not
		//
		var content = document.getElementById("tickerBlockContent"+nSect);

		if ( nSect < 1 || ((content.offsetTop+content.offsetHeight) <= (maxTop - 15) ) ) {
			// No additional items here, advance slide
			if ( nSect < 1 ) {
				changeSlide();
			}else{
				scrollUpCountdown("tickerBlockContent"+nSect, moveDelta, 1);

			}
		}else{
			// Using the same slide, but new items
			scrollUpCountdown("tickerBlockContent"+nSect, 200, 0);
		}

	}	
	function changeSlide(){
		if ( nSect > 0 ) {
			document.getElementById("tickerBlockContent"+nSect).style.visibility = "hidden";
			document.getElementById("tickerBlockContent"+nSect).style.position = "absolute";
			document.getElementById("tickerBlockContent"+nSect).style.top = "0px";
		}
		nSect +=1;
		if ( !document.getElementById("tickerBlockContent"+nSect) ) nSect = 1;
		document.getElementById("tickerBlockContent"+nSect).style.top = "230px";
		document.getElementById("tickerBlockContent"+nSect).style.visibility = "visible";
		document.getElementById("tickerBlockContent"+nSect).style.position = "relative";
		document.getElementById("tickerBlockTitle").innerHTML = 
			document.getElementById("tickerBlockContentTitle"+nSect).innerHTML;
		scrollUpCountdown("tickerBlockContent"+nSect, 230, 0);
	}
	function startTickerScroll(){
	       // quit if this function has already been called
	       if (arguments.callee.done) return;
	
	       // flag this function so we don't do the same thing twice
	       arguments.callee.done = true;
		clearTimeout(timeout);
		timeout = setTimeout("changeTickerBlock()",500);
	}


   /* for Mozilla */
   if (document.addEventListener) {
       document.addEventListener("DOMContentLoaded", startTickerScroll, false);
   }

   /* for Internet Explorer */
   /*@cc_on @*/
   /*@if (@_win32)
       document.write("<script defer src=includes/ie_tickeronload.js><"+"/script>");
   /*@end @*/

   /* for other browsers */
   window.onload = startTickerScroll;