//	Version	Date		Author				Description
//	------- ----------- ------------------- ----------------------------------------------
//  1.00				Steven Dejan		Initial release
//	1.01	2006Jul06	Steven Dejan		Update to work in firefox

/****************** How to use the scroller: **************************

Set the following html block in the form, where you want the scroller to be displayed

<div class="body" style="height:<<<scrollerheight>>>;overflow:hidden;" onMouseOver="javascript:<<<hotjobscroller>>>.stopScrolling()" onMouseOut="javascript:<<<hotjobscroller>>>.startScrolling()">
	<div id="<<<firstHJDiv>>>"  style="height:<<<scrollerheight>>>;position:relative;overflow:hidden;"></div>
	<div id="<<<secondHJDiv>>>" style="height:<<<scrollerheight>>>;position:relative;overflow:hidden;"></div>
</div>

<<<scrollerheight>>>	Height that the scroller has to have
<<<hotjobscroller>>>	name of the scroller instance (see further)
<<<firstHJDiv>>>		name of the first div
<<<secondHJDiv>>>		name of the second div

Set this liddle bit of code beneeth the html block:

<script type="text/javascript">
	//make a new instance of the scroller (use this name in the html block where you see <<<hotjobscroller>>>)
	var hotjobscroller = new NewsScroller(); 
	
	//set name of the first div (see <<<firstHJDiv>>> in html block)
	hotjobscroller.firstDiv = "firstHJDiv";
	//set name of the second div (see <<<secondHJDiv>>> in html block)
	hotjobscroller.secondDiv = "secondHJDiv";
	
	//Set properties; here:
	// - set the height of the scroller (see <<<scrollerheight>>> in html block)
	// - set the number of items to show in the scroller at once
	hotjobscroller.scrollerheight = 125;
	hotjobscroller.itemsToShow = 1;
	//a complete list of changeble parameters: scrollerRate, scrollerStep, scrollerheight, pausebetweenimages, itemsToShow

	//add items to the scroller
	hotjobscroller.addItem("html content to be shown in the scroller");
	hotjobscroller.addItem("This can be all kinds of html");
	
	//Let the scroller scroll
	hotjobscroller.startTextScrolling();
</script>	

*/

var scrollers = new Array();

/*
* Constructor
*/
function NewsScroller(){
	scrollers[scrollers.length] = this;
	this.ind = scrollers.length-1;

	//set methods
	this.addItem = NewsScroller_addItem;
	this.startTextScrolling = NewsScroller_startVTextScroll;
	this.stopScrolling = NewsScroller_stopScrolling;
	this.startScrolling = NewsScroller_startScrolling;
	this.getNextItem = NewsScroller_getNextItem;

	//properties to set the divs
	this.firstDiv = "firstHJDiv";
	this.secondDiv = "secondHJDiv";

	//set default proerties
	this.scrollerRate=25;
	this.scrollerStep=5; //Max=5
	this.scrollerheight=200;
	this.pausebetweenimages=3000;
	this.itemsToShow = 1;

	//set system properties, do not change these, internal use!!!
	this.nextIndex = 0;
	this.scroll = true;
	this.first = true;
	this.vSlideContent=new Array();
}

/*
* Use this method to add a new item to the scroller.
*/
function NewsScroller_addItem(item){
	this.vSlideContent[this.vSlideContent.length] = item;
}

/*
* Use this method to start the scroller
*/
function NewsScroller_startVTextScroll(){
	//sort the array (twice for good random) before starting
	if(this.vSlideContent.length>1){
		this.vSlideContent.sort( randOrd );
		this.vSlideContent.sort( randOrd );
	}
//	alert("starting script");
	//test how much items the scroller has.
	if(this.vSlideContent.length>1){
		//Test if more than one item has to be shown
		if(this.itemsToShow>1){
			getElem(this.firstDiv).innerHTML = "<div style='width:100%;height:45%;overflow:hidden;'>" + this.getNextItem(0) + "</div><hr/><div style='width:100%;height:45%;overflow:hidden;'>" + this.getNextItem(0) + "</div>";
			getElem(this.secondDiv).innerHTML = "<div style='width:100%;height:45%;overflow:hidden;'>" + this.getNextItem(0) + "</div><hr/><div style='width:100%;height:45%;overflow:hidden;'>" + this.getNextItem(0) + "</div>";
		}else{
			getElem(this.firstDiv).innerHTML = this.getNextItem(0);
			getElem(this.secondDiv).innerHTML = this.getNextItem(0);
		}
		//start scrolling first div (second will follow)
		getElem(scrollers[this.ind].firstDiv).style.top=this.scrollerStep + "px";
//		alert("top 1.1");
//		alert(parseInt(getElem(scrollers[this.ind].secondDiv).parentNode.style.height,10)*-1 + "px");
		getElem(scrollers[this.ind].secondDiv).style.top=parseInt(getElem(scrollers[this.ind].secondDiv).parentNode.style.height,10)*-1 + "px";
//		alert("top 1.2");
		NewsScroller_moveUp(this.ind);
	}else{
		//If there is only one item, fill the div's and don't scrolling
		getElem(this.firstDiv).innerHTML=this.vSlideContent[0];
		getElem(scrollers[this.ind].firstDiv).style.top="0px";

	}
}

/*
* This function is used internally to manage the scrolling of the 2 items.
*/
function NewsScroller_moveUp(index){
	var ns = scrollers[index];
	div1 = getElem(ns.firstDiv);
	div2 = getElem(ns.secondDiv);

	//if the first panel reached top of the parent panel, pauze the scrolling
	if(parseInt(div1.style.top, 10)==ns.scrollerStep){
		//if this is the first time, don't renew the content
		if(ns.first){
			ns.first=false;
		//else get new content for the second item
		}else{
			//if more than 1 item has to be shown at the same time, fill the second item with new content (2 items)
			if(ns.itemsToShow>1){
				div2.innerHTML = "<div style='width:100%;height:45%;overflow:hidden;'>" + ns.getNextItem(0) + "</div><hr/><div style='width:100%;height:45%;overflow:hidden;'>" + ns.getNextItem(0) + "</div>";
			//else fill panel with 1 item
			}else{
				div2.innerHTML = ns.getNextItem(0);
			}
		}
		//freeze the 2 items to the starting position
		div1.style.top = "0px";
		//alert("top 3.1");
		div2.style.top = "0px";
		//alert("top 3.2");
		//after a smaal pauze, continue scrolling
		setTimeout("NewsScroller_moveUp("+index+")",ns.pausebetweenimages);
	//when the first panel is not out of the sight, scroll up
	}else if(parseInt(div1.style.top, 10)>(ns.scrollerheight*-1)){
		//only scroll if the user is not with his mouse over the panel
		if(ns.scroll){
			div1.style.top = (parseInt(div1.style.top, 10) - ns.scrollerStep) + "px";
			//alert(div1.style.height);
			div2.style.top = (parseInt(div2.style.top, 10) - ns.scrollerStep) + "px";
			//alert(div2.style.height);
		}
		setTimeout("NewsScroller_moveUp("+index+")",ns.scrollerRate);
	//the first panel is out of the view, so set it back to the bottom and refill its content
	}else{
		//chech if 1 or 2 items must be visible and refill content
		if(ns.itemsToShow>1){
			div1.innerHTML = "<div style='width:100%;height:45%;overflow:hidden;'>" + ns.getNextItem(0) + "</div><hr/><div style='width:100%;height:45%;overflow:hidden;'>" + ns.getNextItem(0) + "</div>";
		}else{
			div1.innerHTML = ns.getNextItem(0);
		}
		//set the first panel on the bottom, the second must be completely visible now
		div1.style.top = parseInt(document.getElementById('hjbody').style.height, 10) + "px";
		//alert(this.parentHeight + " | top 5.1");
		div2.style.top = parseInt((div2.parentNode.style.height), 10)*-1 + "px";
		//alert(parseInt((div2.parentNode.style.height), 10)*-1 + "px");
		//pauze and scroll again
		setTimeout("NewsScroller_moveUp("+index+")",ns.pausebetweenimages);
	}
}

/*
* This method makes the scroller start scrolling again
*/
function NewsScroller_stopScrolling(){
	this.scroll = false;
	//alert("stopping")
}

/*
* This method makes sure the scroller stops scrolling
*/
function NewsScroller_startScrolling(){
	this.scroll = true;
	//alert("mouse starting");
}

/*
* This method is used as a short handed version of 'document.getElementById()'
*/
function getElem(id) {
	//alert(id + " 1");
	return document.getElementById ? document.getElementById(id) : document.all[id];
}

/*
* This method returns the next item in the scroller's items range
*/
function NewsScroller_getNextItem(max){
	nextItem = this.vSlideContent[this.nextIndex];
	if(this.nextIndex==(this.vSlideContent.length-1)){
		this.nextIndex = 0;
	}else{
		this.nextIndex += 1;
	}
	//alert(nextItem + " 2");
	return nextItem;
}

/*
* This method returns a random integer, used to sort an array
*/
function randOrd(a, b){
	return (Math.round(Math.random())-0.5);
}