/* Tiny Scrolling - a smooth navigation between internal links and their destinations
by Marco Rosella - http://www.centralscrutinizer.it/en/design/js-php/tiny-scrolling
based on the works by Travis Beckham and Brian McAllister.
                v0.3 - March 27, 2006
*/

	ts_speed = 50;     //set here the scroll ts_speed: when this value increase, the ts_speed decrease. 
	ts_maxStep = 220;	 //set here the "uniform motion" step for long distances
	ts_brakeK = 3;		 //set here the coefficient of slowing down
	ts_hash = null;		
	ts_currentBlock = null;
	ts_requestedY = 0;
	
	
	

	function ts_getElementYpos(el){
			var y = 0;
			while(el.offsetParent){  
				y += el.offsetTop;    
				el = el.offsetParent;
			}	return y;
	}	
	
	
		
	function ts_initScroll() {

			ts_currentBlock = document.getElementById("myrequestsdiv");   
			//alert(ts_currentBlock);
			//if(!ts_currentBlock) return;
			ts_requestedY = ts_getElementYpos(ts_currentBlock); 
			//alert(ts_requestedY);
			ts_scroll();  
			//return false;
	}
	
	
	
	function ts_getScrollTop(){
			if(document.all) return (document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
			else return window.pageYOffset;   
	}
	
	function ts_getWindowHeight(){
			if (window.innerHeight)	return window.innerHeight;
			if(document.documentElement && document.documentElement.clientHeight) return document.documentElement.clientHeight;
	}
	
	function ts_getDocumentHeight(){
			if (document.height) return document.height;
			if(document.body.offsetHeight) return document.body.offsetHeight;
	}
	

	
	function ts_scroll(){
			var top  = ts_getScrollTop();
			//alert(top);
			if(ts_requestedY > top) {  
				var endDistance = Math.round((ts_getDocumentHeight() - (top + ts_getWindowHeight())) / ts_brakeK);
				endDistance = Math.min(Math.round((ts_requestedY-top)/ ts_brakeK), endDistance);
				var offset = Math.max(2, Math.min(endDistance, ts_maxStep));
			} 
			else { var offset = - Math.min(Math.abs(Math.round((ts_requestedY-top)/ ts_brakeK)), ts_maxStep);
			} 
			
			window.scrollTo(0, top + offset);  
			
			if(Math.abs(top-ts_requestedY) <= 1 || ts_getScrollTop() == top) {
				window.scrollTo(0, ts_requestedY);
				if(!document.all || window.opera) location.ts_hash = ts_hash;
				ts_hash = null;
				} 
				else {setTimeout(ts_scroll,ts_speed);}
	}		
