var slider;
var iPhone = navigator.appVersion.indexOf('iPhone') >= 0;
var ie = navigator.appVersion.indexOf('MSIE') >= 0;

function makeScrollbar(content, scrollbar, handle, horizontal, ignoreMouse){

	// resize the handle to give a more accurate impression
//	handle.style.height = Math.round( content.getSize().y / content.getScrollSize().y * scrollbar.getSize().y ) + 'px';

	// hide scrollbar if not needed
	if( content.getScrollSize().y <= content.getSize().y ) {
		scrollbar.style.display = 'none';
		return;
	}
	
	var steps = (horizontal?(content.getScrollSize().x - content.getSize().x):(content.getScrollSize().y - content.getSize().y))
	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);
		}
	});
	if( !(ignoreMouse) ){
		// Scroll the content element when the mousewheel is used within the 
		// content or the scrollbar element.
		
		var mouseWheelHandler = function(e){
			e = new Event(e).stop();		
			var step = slider.step - e.wheel * 30;	
			slider.set(step);					
		};
	
		
		$(content, scrollbar).addEvent('mousewheel', mouseWheelHandler );
		$(content, scrollbar).addEvent('DOMMouseScroll', mouseWheelHandler );
		
	}
	
	
	
	// Stops the handle dragging process when the mouse leaves the document body.
//	$(document.body).addEvent('mouseleave',function(){ document.title = handle.event.button;  slider.drag.stop(); });
}

function scrollItemIntoView(image) {
//	var duration = image.getPosition($('body-content')).x - $('body-content').scrollLeft;
	var duration = iPhone ? 700 : 750;
//	alert(duration); return
	
	var transition = Fx.Transitions.Sine.easeInOut;
	var scroll = new Fx.Scroll($('body-content'), { 
		wait: false, 
		duration: duration, 
		transition: transition,
		onComplete: function() {
			slider.set($('body-content').scrollLeft)
		}
	});
	
	if(slider.stepWidth) var draggerLeft = Math.round( slider.stepWidth * ( $(image.id).getPosition($('body-content')).x + (ie ? $('body-content').scrollLeft : 0) ) );
	
	var slide = new Fx.Morph( $('dragger'), { 
		duration: duration, 
		transition: transition
	} );
	
	if(!ie)
		scroll.toElement( $(image.id) );
	else
		scroll.start( $(image.id).getPosition($('body-content')).x + $('body-content').scrollLeft, 0);
	
	slide.start( { left: draggerLeft } );
}

function updateScrollbar(horizontal) {
	makeScrollbar( $('body-content'), $('scrollbar'), $('dragger'), horizontal, false );
}

function scrollMinus(elem) { scroll(elem, 'minus'); }
function scrollPlus(elem) { scroll(elem, 'plus'); }

function scroll(elem, direction) {
	var duration = iPhone ? 700 : 750;
	var transition = Fx.Transitions.Sine.easeInOut;
	var scroll = new Fx.Scroll( $('body-content'), { 
		wait: false, 
		duration: duration, 
		transition: transition		
	});
	
	var m = direction == 'plus' ? 1 : -1;
	
	var steps; var x; var y; var pos; var dst;
	if(elem.className.indexOf('horizontal') > 0) {
		steps = 5;
		y = 0;
		x = $('body-content').scrollLeft + m * Math.round( $('body-content').scrollWidth / steps ) ;
		x = direction == 'plus' ? Math.min( $('body-content').scrollWidth - $('content').getSize().x, x) : Math.max(0, x);
		pos = Math.round( slider.stepWidth * x );
		dst = { left: pos };  
	} else {
		steps = 3;
		x = 0;
		y = $('body-content').scrollTop + m * Math.round( $('body-content').scrollHeight / steps );
		y = direction == 'plus' ? Math.min( $('body-content').scrollHeight - $('content').getSize().y, y) : Math.max(0, y);
		pos = Math.round( slider.stepWidth * y );
		dst = { top: pos };
	}
	
	//alert(x + ", " + pos);

	var slide = new Fx.Morph( $('dragger'), { duration: duration, transition: transition } );

	scroll.start( x, y );
	slide.start( dst );
}