	$(function() {
		
		var image_number = 1;		
		var image_count = $('DIV.recently_viewed_container .recently_viewed_scroller IMG').length;
		var image_jump = 95;
		var images_to_display = 9;
		var animate_in_progress = false;
		
		refreshSteps();
		
		$('#recently_viewed_next').mouseover(function(){
			$(this).find('img').attr("src", $(this).children().attr("src").replace(/_off\.gif$/, '_on.gif'));
		});
		
		$('#recently_viewed_next').mouseout(function(){
			$(this).find('img').attr("src", $(this).children().attr("src").replace(/_on\.gif$/, '_off.gif'));
		});
		
		$('#recently_viewed_prev').mouseover(function(){
			$(this).find('img').attr("src", $(this).children().attr("src").replace(/_off\.gif$/, '_on.gif'));
		});
		
		$('#recently_viewed_prev').mouseout(function(){
			$(this).find('img').attr("src", $(this).children().attr("src").replace(/_on\.gif$/, '_off.gif'));
		});
		
		$('#recently_viewed_next').click(function() {
			doStep(true);
			return false;
		});
		
		$('#recently_viewed_prev').click(function() {
			doStep(false);
			return false;
		});
		
		function getCurrentPosition() {
			return Math.abs(parseInt($('div.recently_viewed_content').css("margin-left")));
		}
		
		function isStepAvailable(next) {
			if ( next == true ) {
				return (image_count>images_to_display && (image_count-images_to_display)>=image_number) ? true : false;
			}
			else {
				return (image_number>1) ? true : false;
			}
		}
		
		function refreshSteps() {
			if ( isStepAvailable(true) ) {
				$('#recently_viewed_next').fadeIn();
			}
			else {
				$('#recently_viewed_next').fadeOut();
			}
			
			if ( isStepAvailable(false) ) {
				$('#recently_viewed_prev').fadeIn();
			}
			else {
				$('#recently_viewed_prev').fadeOut();
			}
		}
		
		function getStep(next) {
			var currentPosition = getCurrentPosition();
			
			if ( isStepAvailable(next) ) {
				if ( next == true ) {
					currentPosition += image_jump;
				}
				else {
					currentPosition -= image_jump;
				}
			}
			
			return (currentPosition>0) ? currentPosition : 0;
		}
		
		function doStep(next) {
			if ( !animate_in_progress ) {
				animate_in_progress = true;
								
				$('div.recently_viewed_content').animate(
					{
						marginLeft: '-' + getStep(next) + 'px'
					}, 
					{
						queqe: false, 
						duration: 750,
						complete: function(){
							animate_in_progress = false;
							
							if ( next ) {
								image_number++;
							}
							else {
								image_number--;
							}
							
							refreshSteps();
						}
					}
				);
			}
		}
	});
	
