/* 
Copyright 2008 Achim Gerber */


var ie=document.all
var dom=document.getElementById
var tickerarray=new Array();

function jhfticker(name, objParentUL){
	var c=this; 
	c.name=name; 
	c.mc=0; 

	c.objParentUL = objParentUL;
	
	c.scrollerdelay = parseInt(c.objParentUL.getAttribute('jhfticker_delay')); // time in ms
	c.scrollerwidth = parseInt(objParentUL.parentNode.offsetWidth);
    c.scrollerheight = parseInt(objParentUL.parentNode.offsetHeight);
	c.scroller_entry_top_offset = parseInt(c.objParentUL.getAttribute('jhfticker_entry_top_offset')); //120;
	c.scroller_bottom_clipping = parseInt(c.objParentUL.getAttribute('jhfticker_bottom_clipping')); //254;
	c.iScrollNodeAmmount = parseInt(c.objParentUL.getAttribute('jhfticker_ScrollNodeAmmount')); //1;
	
	//local variables
	c.toScrollLiNodes=new Array();
	c.iCurrentScrollNode=0;
	c.iScrollCounter=0;
	if (c.iScrollNodeAmmount==null) c.iScrollNodeAmmount=1;
	c.CurrentScrollNodes=new Array();
	c.bTickerMoving = false;
	c.waitID = null;
	c.moveID = null;

	if (objParentUL.nodeName == 'UL') {
		for (var objChild = objParentUL.firstChild; objChild; objChild = objChild.nextSibling)
		   if (objChild.nodeName == 'LI') {
			   this.addItem(objChild);		
		   }
	}
	this.startScroll();	

	this.fktWait = function() {
		var c = this; theDelay = c.scrollerdelay; theHeight = c.scrollerheight; 

		iDeltaY = 2;
		iTimeDeltaY = 20;
		
    	// change the moving items list
		c.iScrollCounter = 0;
		c.iCurrentScrollNode++;
		if (c.iCurrentScrollNode >= c.mc) c.iCurrentScrollNode = 0;
		iNextNode =  c.iCurrentScrollNode;
		
		for (iLiCount = 0; iLiCount < c.mc; iLiCount++) {
			c.toScrollLiNodes[iLiCount].style.visibility="hidden";
		}
		
		for (iLiChangeCount = 0; (iLiChangeCount < c.mc) && (iLiChangeCount < c.iScrollNodeAmmount); iLiChangeCount++){							
			if (iNextNode >= c.mc) iNextNode = 0;
			
			c.CurrentScrollNodes[iLiChangeCount]=c.toScrollLiNodes[iNextNode];
			c.CurrentScrollNodes[iLiChangeCount].style.top= c.scroller_entry_top_offset +((iLiChangeCount) * c.scrollerheight)+"px";
			c.CurrentScrollNodes[iLiChangeCount].style.height=c.scroller_bottom_clipping  -  parseInt(c.CurrentScrollNodes[iLiChangeCount].style.top)   +"px";
			c.CurrentScrollNodes[iLiChangeCount].style.visibility="visible";	
			

			
			iNextNode++;			
		}
		c.bTickerMoving = true;
		c.moveID = setTimeout(function(){c.fktMove();},iTimeDeltaY);
		return;
			
	}

	this.fktMove = function() {
		var c = this; theDelay = c.scrollerdelay; theHeight = c.scrollerheight; 

		iDeltaY = 2;
		iTimeDeltaY = 20;
	
		iFirstTop= parseInt(c.CurrentScrollNodes[0].style.top);
	
		if (c.bTickerMoving && iFirstTop<=iDeltaY){
			//debugger;
			// the move has reached its destination
			c.CurrentScrollNodes[0].style.top=0+"px"
			
			for (iLiCount = 0; (iLiCount < c.mc) && (iLiCount < c.iScrollNodeAmmount); iLiCount++){	
				if (iLiCount < c.iScrollNodeAmmount) {
					c.CurrentScrollNodes[iLiCount].style.top=parseInt(c.CurrentScrollNodes[0].style.top) + (iLiCount * c.scrollerheight)+"px";
				}
			}					
			c.bTickerMoving = false;
			c.waitID = setTimeout(function(thisObj){c.fktWait();},theDelay);

			return;
			
		} else {
			// the Nodes are moving
			c.iScrollCounter++;
 			c.CurrentScrollNodes[0].style.height=c.scroller_bottom_clipping - iFirstTop +"px";

			iDeltaY = 10 - (c.iScrollCounter);
			if (iDeltaY < 2) iDeltaY = 2;

			c.CurrentScrollNodes[0].style.top=iFirstTop-iDeltaY+"px";
			c.CurrentScrollNodes[0].style.height=c.scroller_bottom_clipping - iFirstTop +"px";
			for (iLiCount = 0; (iLiCount < c.mc) && (iLiCount < c.iScrollNodeAmmount); iLiCount++){	
				if (iLiCount < c.iScrollNodeAmmount) {
					c.CurrentScrollNodes[iLiCount].style.top=parseInt(c.CurrentScrollNodes[0].style.top) + (iLiCount * c.scrollerheight)+"px";
				}
			}
			c.bTickerMoving = true;
			c.moveID = setTimeout(function(){c.fktMove();},iTimeDeltaY);
			return;
		}
	}
	
	return this;
}

jhfticker.prototype.addItem = function(objLiNode){
	var c = this;
	c.toScrollLiNodes[c.mc]=objLiNode;
	c.mc++;
}

jhfticker.prototype.startScroll = function(){
	var c = this;
	c.toScrollLiNodes[0].style.top = 0+"px";
	
	for (iLiCount = 0; iLiCount < c.mc; iLiCount++){
	
		if (iLiCount < c.iScrollNodeAmmount) {
			c.CurrentScrollNodes[iLiCount]=c.toScrollLiNodes[iLiCount];
			c.CurrentScrollNodes[iLiCount].style.top=parseInt(c.CurrentScrollNodes[0].style.top) + (iLiCount * c.scrollerheight)+"px";
			c.CurrentScrollNodes[iLiCount].style.height=c.scroller_bottom_clipping  -  parseInt(c.CurrentScrollNodes[iLiCount].style.top)   +"px";
			c.toScrollLiNodes[iLiCount].style.visibility="visible";
		} else {
			c.toScrollLiNodes[iLiCount].style.visibility="hidden";
		}
	}	
	
	c.bTickerMoving = false;	
	c.waitID = setTimeout(function(){c.fktWait();},c.scrollerdelay);
}

objULs = document.getElementsByTagName('UL')

for (iCount = 0; iCount < objULs.length ; iCount++) {
	
	if (0 < parseInt(objULs[iCount].getAttribute('jhfticker_delay'))){
		//it is a jhfticker ul
			
		bTickerAlreadyCreated = false;
		for (iTickerCount = 0; iTickerCount < tickerarray.length; iTickerCount++){
			if (tickerarray[iTickerCount].objParentUL == objULs[iCount])
				bTickerAlreadyCreated = true;
			
		}
		
		if (bTickerAlreadyCreated == false)
			tickerarray[tickerarray.length] = new jhfticker("jhfticker_123", objULs[iCount]);	
	}
	
}

