// JavaScript Document
function makeScrollbar(content,horizontal,ignoreMouse){
	
	var steps = (horizontal?(content.getScrollSize().x - content.getSize().x):(content.getScrollSize().y - content.getSize().y))
	
	if (steps<=0) {
		return;
	}
	var scrollbar=new Element("div").injectAfter(content);
	scrollbar.addClass("scrollbar-"+(horizontal?"hor":"ver"));
	var handle=new Element("div").inject(scrollbar);	
	handle.addClass("handle-"+(horizontal?"hor":"ver"));
	if (horizontal) {
		content.style.overflowX="hidden";
		scrollbar.style.width=content.getSize().x+"px";
	}else {
		content.style.overflowY="hidden";
		scrollbar.style.height=content.getSize().y+"px";
	}
	var slider = new Slider(scrollbar, handle, {	
		steps: steps,
		mode: (horizontal?'horizontal':'vertical'),
		onChange: function(step){
			// Scrolls the content element in x or y direction.
			var x = (horizontal?step:0);
			var y = (horizontal?0:step);
			content.scrollTo(x,y);
		}
	}).set(0);
	if( !(ignoreMouse) ){
		// Scroll the content element when the mousewheel is used within the 
		// content or the scrollbar element.
		$$(content, scrollbar).addEvent('mousewheel', function(e){	
			e = new Event(e).stop();
			var step = slider.step - e.wheel * 30;	
			slider.set(step);					
		});
	}
	// Stops the handle dragging process when the mouse leaves the document body.
	$(document.body).addEvent('mouseleave',function(){slider.drag.stop()});
}

window.addEvent("domready",function() {
	var scrollers=$$(".mooscroll");
	scrollers.each(function(el) {
		if (el.getStyle("overflow")=="scroll" || el.getStyle("overflow-x")=="scroll") {
			makeScrollbar(el,true,false);
		}
		if (el.getStyle("overflow")=="scroll" || el.getStyle("overflow-y")=="scroll") {
			makeScrollbar(el,false,false);
		}
	});
});
