var cpos = 0;
var ctop;
var cratio = parseInt($('.productgrid').css('height')) / parseInt($('#scrubber-cursor').css('height'));
var itemheight;

function scrollGrid(rtop) {
	rtop = parseInt(rtop);
	cpos = (rtop - ctop) * cratio;
	$('.productgrid').scrollTop(cpos);
}

$(function() {
	mtop = ($('#rightScrubber').height() - $('#scrubber').height()) / 2;
	$('#scrubber').css('marginTop',mtop).css('marginLeft',0);
	ctop = parseInt($('#scrubber-cursor').position().top);
	itemheight = parseInt($('.scrubber-item').height()) +
				 parseInt($('.scrubber-item').css('marginTop')) + 
				 parseInt($('.scrubber-item').css('marginBottom'));
	
	$('#scrubber-cursor').draggable({
							axis: 'y',
							containment: '#scrubber',
							drag: function(event,ui) { scrollGrid(ui.position.top); },
							end: function(event,ui) { scrollGrid(ui.position.top); }
						});
	$('.scrubber-item:not(.scrubber-last)').click(function() {
							$('#scrubber-cursor').css('top',$(this).position().top);
							scrollGrid($(this).position().top);
						});
	$('.scrubber-last').click(function() {
							$('#scrubber-cursor').css('top',$(this).position().top - itemheight);
							scrollGrid($(this).position().top - ($(this).height()));
						});
	$('.productgrid').mousewheel(function(event,delta) {
		qsl.hide();
		if(delta > 0) {
			// go up
			newTop = parseInt($('#scrubber-cursor').position().top) - 5;
			if(newTop >= ctop) {
				$('#scrubber-cursor').css('top',newTop);
			}
			scrollGrid(newTop);
		} else if(delta < 0) {
			// go down
			newTop = parseInt($('#scrubber-cursor').position().top) + 5;
			if(newTop <= (parseInt($('.scrubber-last').position().top) - itemheight)) {
				$('#scrubber-cursor').css('top',newTop);
			}
			scrollGrid(newTop);
		}
		return false;
	});
});