$(document).ready(function() {
	$( '.thumb-scroll-button' ).each(function(index) {
		var id = $( this ).attr( 'rel' );
		var w = $( '#tsimages' + id ).width();
		if(w > 600) {
			var coord = $( this ).position();
			var pos = $( '#tsbar' + id ).offset().left;
			var dist = w - 608;
			$(this).mousedown(function(event){
				event.preventDefault();
				$( '#top' ).disableSelection();
				$( '#top' ).css({'cursor': 'e-resize'});
				$( '#top' ).mousemove(function(event){
					var p = Math.round(Math.max(0, Math.min(550, event.pageX - pos - 25)));
					var d = Math.round((p/550)*dist) * -1;
					$( '#tsbutton' + id ).css({'top': coord.top + 'px', 'left': p + 'px' });
					$( '#tsimages' + id ).css({'left': d + 'px' });
				}).mouseup(function(){
					$( '#top' ).css({'cursor': 'auto'});
					$(this).unbind( 'mousemove' );
					$(this).unbind( 'mouseup' );
				});
			});
		} else {
			$( '#tsbar' + id ).hide();
		}
	});
});
