function incrementRight() {
	new_value = parseInt($('#thumbnails').css("top"));
	if (new_value < 0)
		new_value += 10;
	new_value = new_value + "px";
	$('#thumbnails').css('top' , new_value);

}
function incrementUp() {

	new_value = parseInt($('#thumbnails').css("top"));
	if (new_value > limit)
		new_value -= 10;
	new_value = new_value + "px";
	
	$('#thumbnails:first').css('top' , new_value);
}
function scrollLeft() {
	move_interval = setInterval(incrementRight, 10);

}
function scrollRight() {
	move_interval = setInterval(incrementUp, 10);
}
function stopScroll() {
	if (move_interval !== undefined) {
		clearInterval(move_interval);
	}
}

function addScrollBehaviour() {
	move_interval = false; //just create the var so stopScroll() does not complain
	
	/*
	$('#upArrow').mousedown( function() {
		scrollLeft();
	}).mouseup( function() {
		stopScroll();
	}).mouseout( function() {
		stopScroll();
	});
	*/
	$('#upArrow').hover( 
		function() {
		scrollLeft();
		} ,
		function() {
		stopScroll();
		}
	);
	
	/*
	$('#downArrow').mousedown( function() {
		scrollRight();
	}).mouseup( function() {
		stopScroll();
	}).mouseout( function() {
		stopScroll();
	});
	*/
	$('#downArrow').hover( 
		function() {
		scrollRight();
		} ,
		function() {
		stopScroll();
		}
	);
}
function addThumbnailBehaviour() {
	$("#thumbnails > a").click(function(e) {
		e.preventDefault();
		
		if (next_interval !== undefined) {
			clearInterval(next_interval);
		}
	
		//target_file = $(this).attr('href');
		var i = $("#thumbnails > a").index(this);
		//alert(i);
		
		change_img_to = $(this).attr('href');
		
		var main_img = $('#mainPhoto > img:first');
		main_img.fadeOut("fast", function() {
			
			$('#mainPhoto > img:first').attr('src', change_img_to);
			$('#mainPhoto > img:first').load(function () {
				$('#mainPhoto > img:first').fadeIn("fast");
			});
			
		});
		
		//current_img = i;
		
		//setPrevNextHref();
		//alert(target_file);
		
		$('#thumbnails > a').removeClass('selected');
		$(this).addClass("selected");
	});
}


function addThumbstripBehaviors() {
	
	//first the scroll behavior
	addScrollBehaviour();
	
	//next the image thumbnail behavior
	addThumbnailBehaviour();
}
function preloadImages() {
	
	preloaded_images = new Array();
	//put this on a time delay
	jQuery.each(image_data, function(i, item) {
		//alert(i);
		preloaded_images[i] = new Image();
		preloaded_images[i].src = item.large_src;
	});
	
}

function slowImageReveal() {
	//$("#mainPhoto img").hide();
	$("#mainPhoto img").load(function() {
		$(this).fadeIn("slow");
	});
	thumbs_loaded = 0;

	$("#thumbnails").hide();
	//$("#thumbnails > a > img").hide();
	$("#thumbnails").show();
	$("#thumbnails > a > img").load(function() {
		$(this).fadeIn("normal");
		thumbs_loaded++;
		if (thumbs_loaded == num_images) {
			
			//stripPosition(current_img); //you want the images loaded, then this will calculate combined width, and adjust
		}
	});
	addThumbstripBehaviors();
}
function startSlideshow() {
	current_img = 0;
	next_interval = setInterval(slideShowNext, 3000);	
}
function slideShowNext()  {
	current_img++;
	new_value = ((-current_img * 106) + 500);
	if ( new_value < parseInt($('#thumbnails').css("top"))) {
		$('#thumbnails:first').css('top' , new_value);
	}
	$('#thumbnails a:eq(' + current_img + ')').trigger('click');
	if (current_img < (num_images - 1)) {	
		//alert(current_img + 'out of' + num_images);
		next_interval = setInterval(slideShowNext, 4000);	
	} else {
		clearInterval(next_interval);
	}
}
$(document).ready(function() {
	
	//current_img = Number($('#next-prev > p > span:eq(0)').text()) - 1; //replace with #current_image
	num_images = $('#thumbnails > a').length;
	limit = (-num_images * 106) + 600;
	slowImageReveal();
	 //adjust position based on chosen pic, so its centered-like
	startSlideshow();
	
	
	
});
