/* Horizontal Tiny Scrolling - a smooth scrolling script for horizontal websites
(the brother of the vertical "Tiny Scrolling")
by Marco Rosella - http://www.centralscrutinizer.it/en/design/js-php/horizontal-tiny-scrolling
                v0.1 - March 31, 2006
*/

window.onload = function() {
	HtinyScrolling.init(); MouseScrollInit();
	}

var HtinyScrolling = {
	speed : 100,      //set here the scroll speed: when this value increase, the speed decrease. 
	maxStep: 150,	 //set here the "uniform motion" step for long distances
	brakeK: 3,		 //set here the coefficient of slowing down
	hash:null,		
	currentBlock:null,
	requestedX:0,
	init: function() {
			var lnks = document.getElementsByTagName('a');   
			for(var i = 0, lnk; lnk = lnks[i]; i++) {   
				if ((lnk.href && lnk.href.indexOf('#') != -1) &&  ( (lnk.pathname == location.pathname) ||
				('/'+lnk.pathname == location.pathname) ) && (lnk.search == location.search)) {  
				lnk.onclick = HtinyScrolling.initScroll;   		
				}   
			}    
	},
	getElementXpos: function(el){
			var x = 0;
			while(el.offsetParent){  
				x += el.offsetLeft;    
				el = el.offsetParent;
			}	return x;
	},		
	getScrollLeft: function(){
			if(document.all) return (document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft;
			else return window.pageXOffset;   
	},	
	initScroll: function(e){
			var targ;  
			if (!e) var e = window.event;
			if (e.target) targ = e.target;
			else if (e.srcElement) targ = e.srcElement;   
			HtinyScrolling.hash = targ.href.substr(targ.href.indexOf('#')+1,targ.href.length); 
			HtinyScrolling.currentBlock = document.getElementById(HtinyScrolling.hash);   
			if(!HtinyScrolling.currentBlock) return;
			HtinyScrolling.requestedX = HtinyScrolling.getElementXpos(HtinyScrolling.currentBlock); 
			HtinyScrolling.scroll();  
			return false;
	},
	scroll: function(){
			var left  = HtinyScrolling.getScrollLeft();
			if(HtinyScrolling.requestedX > left) {  
			var offset = Math.min(Math.abs(Math.round((HtinyScrolling.requestedX-left)/ HtinyScrolling.brakeK)), HtinyScrolling.maxStep);
			} else { var offset = - Math.min(Math.abs(Math.round((HtinyScrolling.requestedX-left)/ HtinyScrolling.brakeK)), HtinyScrolling.maxStep);
			} window.scrollTo(left + offset, 0);  
			if(Math.abs(left-HtinyScrolling.requestedX) <= 1 || HtinyScrolling.getScrollLeft() == left) {
				window.scrollTo(HtinyScrolling.requestedX, 0);
				if(!document.all || window.opera) location.hash = HtinyScrolling.hash;
				HtinyScrolling.hash = null;
			} else 	setTimeout(HtinyScrolling.scroll,HtinyScrolling.speed);
	}		
}

/* an extra: Horizontal Mouse Scrolling */
/* it doesn't work with Opera, that hasn't a event associated to the mouse wheel */

function MouseScrollInit(){	
	if (window.addEventListener ) {
	window.addEventListener("DOMMouseScroll", MouseScroll, false);
	} else document.attachEvent("onmousewheel", MouseScroll); 
}

function MouseScroll(e) {
	if (!e) var e = window.event;
	if (e.wheelDelta <= 0 || e.detail>=0){  
	window.scrollBy(80,0);
	} else  window.scrollBy(-80,0) ; 
	} 
