/**
 * Divsion Scroller
 *
 */


function DivScroller(id, speed, initialScroll, initialScrollLimit)
{
	isCSS = (document.body && document.body.style) ? true : false;
	isW3C = (isCSS && document.getElementById) ? true : false;
	if(! isW3C){
		return;
	}
	
	this.divObj = document.getElementById(id);
	this.lastMousePos = 0;
	this.currentMousePos = 0;
	this.speed = speed;
	this.initialScrollLimit = initialScrollLimit;
	 
	if (this.divObj ){
       	this.divObj.style.overflow = 'hidden';
		this.divObj.speed = speed;
		this.divObj.scrollCursor = 0;
		this.divObj.intervalIDDown = null;
		this.divObj.intervalIDUp = null;
		this.divObj.scrollingDown = false;			
	}	

	
	this.divObj.onmousemove = function(evt){
		evt = (evt) ? evt : ((window.event) ? window.event : null);
	
		if(evt){
			var elem = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
		
			if(elem){
				this.currentMousePos = evt.clientY
				if(this.currentMousePos < this.lastMousePos){
					scrollUp(this, this.speed);
				}
				else if(this.currentMousePos > this.lastMousePos) {
					scrollDown(this, this.speed);
				}
				
				//alert("Direction: " + direction + " Current: " + currentMousePos + " Last: " + lastMousePos);
				this.lastMousePos = this.currentMousePos;
			}
		}
	}	
	
	
	//time out func to scroll down list
	this.timeoutFuncDown = function(){
			var self = this;
			this.divObj.intervalIDDown = setInterval(function(){scrollDown(self.divObj, 4, self.initialScrollLimit);}, 2);
	}
	
	
	//time out func to scroll up list
	this.timeoutFuncUp = function(){
		var self = this;
		this.divObj.intervalIDUp = setInterval(function(){scrollUp(self.divObj, 4, self.initialScrollLimit);}, 2);
	}
	
	
	if(initialScroll){
		
		if(! this.initialScrollLimit){
			alert("Initial Limit Required");
			return;
		}	
		
		var self = this;
		setTimeout(function(){self.timeoutFuncDown();},2000);
		setTimeout(function(){self.timeoutFuncUp();}, 3000);
	}
	
}	
	
	
	
function scrollDown (divObj, speed, initialScrollLimit){
		
	oldScrollTop = divObj.scrollTop;
	divObj.scrollCursor += speed;
	divObj.scrollTop = divObj.scrollCursor;
	
	//If scrolltop not changed then at bottom
	if(divObj.scrollTop == oldScrollTop){
		divObj.scrollCursor = divObj.scrollTop;
	}
	
	if(initialScrollLimit){

		if(divObj.scrollCursor >= initialScrollLimit || divObj.scrollTop == oldScrollTop){
			divObj.scrollingDown = false;
			clearInterval(divObj.intervalIDDown);
		}
		else{
			divObj.scrollingDown = true;
		}
	}
}

function scrollUp (divObj, speed, initialScrollLimit){
	
	if(divObj.scrollingDown && initialScrollLimit){
		return;
	}
	
	divObj.scrollCursor -= speed;
	if(divObj.scrollCursor < 0){
		divObj.scrollCursor = 0;
		if(initialScrollLimit){
			clearInterval(divObj.intervalIDUp);
		}
	}
	divObj.scrollTop = divObj.scrollCursor;
	
}


