// spree/app/assets/javascripts/spree/frontend/product.js
var show_variant_images = function(variant_id) {
$('li.vtmb-' + variant_id).show();
var currentThumb = $('#' +
$("#main-image").data('selectedThumbId'));
// if currently selected thumb does not belong to current variant,
// hide it and select the first available thumb instead.
if(!currentThumb.hasClass('vtmb-' + variant_id) &&
!currentThumb.hasClass('tmb-all')) {
var thumb = $($('ul.thumbnails li:visible').eq(0));
var newImg = thumb.find('a').attr('href');
$('ul.thumbnails li').removeClass('selected');
thumb.addClass('selected');
$('#main-image img').attr('src', newImg);
$("#main-image").data('selectedThumb', newImg);
$("#main-image").data('selectedThumbId', thumb.attr('id'));