/*
-------------------------------------------------------------------------------------------------
	event handler setup
-------------------------------------------------------------------------------------------------
*/
function addOnloadEvent(newFunction)
{
	if (typeof window.addEventListener != "undefined" ) {
		window.addEventListener( "load", newFunction, false );
	} else if (typeof window.attachEvent != "undefined") {
		window.attachEvent( "onload", newFunction );
	} else {
		if ( window.onload != null ) {
			var prevOnload = window.onload;
			window.onload = function ( e ) 
			{
					prevOnload( e );
					window[newFunction]();
			};
		} else {
			window.onload = newFunction;
		}
	}
}
/*
-------------------------------------------------------------------------------------------------
	scroLL
-------------------------------------------------------------------------------------------------
*/
function scrollParameters (stepY, initalDelay, timerDelay, scrollOnlyIfLonger)
{
	// page count is : 2 + Math.floor(Harea/Hcontent) --->  1 + .....   (to simplify indexing) 
	// page index    : 0...N-1						  --->  0...N
	// for content longer then the scroll area pageN = 1, otherwise pageN > 1
	this.divInternal = null;
	this.divExternal = null;
	this.setArea = function (extName, intName) {
			this.divExternal = new dhtml_objectByID (extName);
			this.divInternal = new dhtml_objectByID (intName);		
	}
	this.Pages = new Array();
	this.stepY = stepY				//Specify speed of scroll. Larger=faster (ie: 5)
	this.cache = stepY;	
	this.delay0 = initalDelay;		//Specify intial delay before scroller starts scrolling (in miliseconds):
	this.delay  = timerDelay;
	this.contentH= 0;
	this.areaH = 0;
	this.fpageN= function() { return (1+Math.floor( this.areaH / this.contentH)) };	 
	this.pageN = 1;
	// firstPage, lastPage			// added at runtime
	this.startScroller = 	function () {	
			this.areaH = this.divExternal.height();
			this.contentH = this.divInternal.height();
			this.pageN = this.fpageN();
			// exit if content is shorter and flag is set
			if ((scrollOnlyIfLonger) && (this.pageN>1)) return false;
			var text1 = this.divInternal.HTML();
			var text = '';	
			for(i=0; (i<=this.pageN); i++){
				text += 
					'<DIV ID="newsInt'+i+'" style="position:absolute; border:0; padding-left:0px; margin:0px;">'+
						//<div style="background-color: #CCCC00;">this.Pages:'+i+'</div>
						'<div>' + text1 + '</div>'+
					'</DIV>';
			}
			this.divExternal.HTML( text );
			this.firstPage = 0; this.lastPage = this.pageN;
		//alert(this.pageN);
			//arrange the set of pages  ;
			for(i=0; (i<=this.pageN); i++){
				this.Pages[i] = new dhtml_objectByID ('newsInt'+i);
				this.Pages[i].top( (i==0)?0:this.Pages[i-1].top()+this.Pages[i-1].height());
				//vm('<br>'+i+'.....'+this.Pages[i].top()+'.....'+this.Pages[i].rectToString());
			}
		
			this.divExternal.obj.onmouseover = suspendScroller;
			this.divExternal.obj.onmouseout  = restartScroller;
			
			if (this.contentH!=0)
				setTimeout("scrollDiv()", this.delay0)			
	}

}

function suspendScroller ()
{
	scroll.stepY = 0;
	// window.status = 'suspendScroller'
}

function restartScroller ()
{
	scroll.stepY = scroll.cache;
	// window.status = 'restartScroller.....'+scroll.cache
}

function reverseScroller ()
{
	// reverse scroll is not covered-implemented in scrollDiv !!!!!
	scroll.cache = -scroll.cache;
	scroll.stepY = scroll.cache;
	window.status = 'reverseScroller.....'+scroll.cache;
}

function scrollDiv()
{
	//vm('',1);
	//vm('pages='+scroll.pageN+'.....H/h='+scroll.areaH+'/'+scroll.contentH);
	//vm('1st='+scroll.firstPage+'.....Lst='+scroll.lastPage); vm('cache='+scroll.cache);
	for(i=0; (i<=scroll.pageN);i++){
		scroll.Pages[i].top( scroll.Pages[i].top() - scroll.stepY );
		//vm('page-'+i+' '+ (i==scroll.firstPage?'F':(i==scroll.lastPage?'L':'*'))+ scroll.Pages[i].top() );
		//vm( (i=scroll.lastPage?'L':'-'))+i+'.....' );	//+scroll.Pages[i].top()
		
	}

	
	//vm('.....firstPage='+scroll.firstPage+'.....lastPage='+scroll.lastPage);
	//vm('.....y='+scroll.Pages[scroll.lastPage].top()+'------>'+scroll.areaH);
	if (scroll.cache>0) {
		if (scroll.Pages[scroll.lastPage].top()+scroll.contentH+5<=scroll.areaH) {
			// move top page to end
			scroll.Pages[scroll.firstPage].top( scroll.Pages[scroll.lastPage].top() + scroll.Pages[scroll.lastPage].height() );
			scroll.lastPage = scroll.firstPage;
			scroll.firstPage = (scroll.firstPage<scroll.pageN)?scroll.firstPage+1: 0;
			//vm('firstPage='+scroll.firstPage+'.....lastPage='+scroll.lastPage);
			//alert('switch++')
		}
	} else {
		if (scroll.Pages[scroll.firstPage].top()>=0) {
			// move bottom page to top
			scroll.Pages[scroll.lastPage].top( scroll.Pages[scroll.firstPage].top() - scroll.Pages[scroll.lastPage].height() );
			scroll.firstPage = scroll.lastPage;
			scroll.lastPage = (scroll.lastPage>0)?scroll.lastPage-1: scroll.pageN;
			//vm( 'firstPage='+scroll.firstPage+'.....lastPage='+scroll.lastPage) ;
			//alert('switch--')

		}
	}
	
	setTimeout("scrollDiv()" , scroll.delay)
}

