$j(function() {

	var endPos = $j('#scrollContainer').width() - $j('#scrollContent').width();
	var scrollDuration = Math.round(endPos/(mc_scrollSpeed*15) * -1000) ; //0 - 100, 0 is slowest

	$j('#scrollRewind').click(function(){ $j('#scrollContent').css('margin-left','0'); $j('.ui-slider-handle').css('left','0'); return false; });
	
	$j('#scrollPlay').click(function(){
		animateScroll();
		return false;
	});
	
	$j('#scrollStop').click(function(){
		$j('#scrollContent, .ui-slider-handle').stop();
		$j('#scrollPlay').show(); $j('#scrollStop').hide();
		return false;
	});
	
	$j('#scrollEnd').click(function(){
		$j('#scrollContent').css('margin-left', endPos + 'px');
		$j('.ui-slider-handle').css('left','100%');
		return false;
	});

	//scrollpane parts
	var scrollPane = $j( "#scrollContainer" ),
		scrollContent = $j( "#scrollContent" );
	
	//build slider
	var scrollbar = $j( "#slider" ).slider({
		slide: function( event, ui ) {
			if ( scrollContent.width() > scrollPane.width() ) {
				scrollContent.css( "margin-left", Math.round(
					ui.value / 100 * ( scrollPane.width() - scrollContent.width() )
				) + "px" );
			} else {
				scrollContent.css( "margin-left", 0 );
			}
		}
	});

	//append icon to handle
	var handleHelper = scrollbar.find( ".ui-slider-handle" )
	.mousedown(function() {
		scrollbar.width( handleHelper.width() );
	})
	.mouseup(function() {
		scrollbar.width( "100%" );
	})
	.append( "<span class='ui-icon ui-icon-grip-dotted-vertical'></span>" )
	.wrap( "<div class='ui-handle-helper-parent'></div>" ).parent();
	
	//change overflow to hidden now that slider handles the scrolling
	scrollPane.css( "overflow", "hidden" );
	
	function animateScroll () {
		$j('#scrollPlay').hide(); $j('#scrollStop').show();
		$j('#scrollContent').animate({'margin-left': endPos+'px'},scrollDuration,'linear',function(){
			$j('#scrollPlay').show(); $j('#scrollStop').hide();
		});
		$j('.ui-slider-handle').animate({'left':'100%'},scrollDuration,'linear');
	}
	
	
	//size scrollbar and handle proportionally to scroll distance
	function sizeScrollbar() {
		var remainder = scrollContent.width() - scrollPane.width();
		var proportion = remainder / scrollContent.width();
		var handleSize = scrollPane.width() - ( proportion * scrollPane.width() );
		if(remainder>0){ //only enable slider if content is wider than container
			scrollbar.find( ".ui-slider-handle" ).css({
				width: handleSize,
				"margin-left": -handleSize / 2
			});
		}else{
			$j('#slider').slider('disable');
		}
		handleHelper.width( "" ).width( scrollbar.width() - handleSize );
	}
	
	//reset slider value based on scroll content position
	function resetValue() {
		var remainder = scrollPane.width() - scrollContent.width();
		var leftVal = scrollContent.css( "margin-left" ) === "auto" ? 0 :
			parseInt( scrollContent.css( "margin-left" ) );
		var percentage = Math.round( leftVal / remainder * 100 );
		scrollbar.slider( "value", percentage );
	}
	
	//if the slider is 100% and window gets larger, reveal content
	function reflowContent() {
			var showing = scrollContent.width() + parseInt( scrollContent.css( "margin-left" ), 10 );
			var gap = scrollPane.width() - showing;
			if ( gap > 0 ) {
				scrollContent.css( "margin-left", parseInt( scrollContent.css( "margin-left" ), 10 ) + gap );
			}
	}
	
	//change handle position on window resize
	$j( window ).resize(function() {
		resetValue();
		sizeScrollbar();
		reflowContent();
	});
	//init scrollbar size
	setTimeout( sizeScrollbar, 10 );//safari wants a timeout
	
	if(autoScroll==1){ setTimeout(animateScroll,125); }
	
});
