var ScrollThreshold = new function() {
	this.init = function(paneId) {
		var pane = document.getElementById(paneId) ;

   		if(pane.stopScroll) return ;//make register only once

   		pane.stopScroll = false;
   		pane.cummulateDelta = 0 ;
		pane.onscroll = function(event) {
		  if( this.scrollTop + this.offsetHeight >= this.scrollHeight ) {
		    this.stopScroll = true;
		  }else{
		    this.stopScroll = false;
		  }
		  //document.title = 'st:' + this.scrollTop + ' sh: ' + this.scrollHeight + ' ot:' + this.offsetTop + ' oh: ' + this.offsetHeight + ' flag '+ this.cummulateDelta;
		};
		if (pane.addEventListener) {
			pane.addEventListener('DOMMouseScroll', wheel, false);
		}
		pane.onmousewheel = wheel;
	};

	function wheel(event){
		var delta = 0;
		if (!event) event = window.event;
		if (event.wheelDelta) {
			delta = event.wheelDelta/120; 
			if (window.opera) delta = -delta;
		} else if (event.detail) {
			delta = -event.detail/3;
		}
		//document.title = 'st:' + this.scrollTop + ' sh: ' + this.scrollHeight + ' ot:' + this.offsetTop + ' oh: ' + this.offsetHeight + ' flag '+ this.cummulateDelta;
		//delta = -1 is down, +1 is up	
		if (delta > 0) { this.stopScroll= false; this.cummulateDelta=0; return; } 
		if(this.stopScroll && this.cummulateDelta < 5 ) {//scroll > 5 times, release
	       	this.cummulateDelta = this.cummulateDelta + 1;
	       	event.returnValue = false;
	        if (event.preventDefault) {
				event.preventDefault();
	        }
		} else {
			this.cummulateDelta=0;
			this.stopScroll = false;
	        event.returnValue = true;
		}
	}
};

