
var bindThumbBrowseButtons = function(thumbsPrev, thumbsNext) {
    
  function thumbsPrevClick() {
    /* Rotate product image thumbs leftward */
    var numThumbsVisible = 4;
    var numThumbsTotal = $('div.thumb_holder').size();
    
    var lastVisible = $('div.thumb_holder:visible:last');
    var lastVisibleSequenceId = lastVisible.attr('id').substring(13);  

    var thumbSequenceIdToShow = parseInt(lastVisibleSequenceId) - parseInt(numThumbsVisible);
    if (thumbSequenceIdToShow < 0) { /* Go "around" if we reached the beginning of thumbs */
      thumbSequenceIdToShow += numThumbsTotal;
    }
    
    lastVisible.hide();
    var thumbToShow = $('#thumb_holder_' + thumbSequenceIdToShow);
    var firstVisibleThumb = $('div.thumb_holder:visible:first');
    thumbToShow.insertBefore(firstVisibleThumb);
    $('#thumb_holder_' + thumbSequenceIdToShow).show();
  }
  
  function thumbsNextClick() {
    /* Rotate product image thumbs rightward */
    var numThumbsVisible = 4;
    var numThumbsTotal = $('div.thumb_holder').size();
    var firstVisible = $('div.thumb_holder:visible:first');
    var firstVisibleSequenceId = firstVisible.attr('id').substring(13);  
      
    var thumbSequenceIdToShow = parseInt(firstVisibleSequenceId) + parseInt(numThumbsVisible);
    if (thumbSequenceIdToShow >= numThumbsTotal) { /* Go "around" if we reached the end of thumbs */
      thumbSequenceIdToShow -= numThumbsTotal;      
    }
    
    firstVisible.hide();
    var thumbToShow = $('#thumb_holder_' + thumbSequenceIdToShow);
    var lastVisibleThumb = $('div.thumb_holder:visible:last');
    thumbToShow.insertAfter(lastVisibleThumb);
    $('#thumb_holder_' + thumbSequenceIdToShow).show();
  }
    
  thumbsPrev.live('click', thumbsPrevClick);
  thumbsPrev.live('mouseenter', function() {
    $(this).css('cursor','pointer');
  });
  
  thumbsNext.live('click', thumbsNextClick);
  thumbsNext.live('mouseenter', function() {
    $(this).css('cursor','pointer');
  });
};

fyndiq.zoomMainImage = function() {
  $('#product_main_image').parent('a').click();
}

fyndiq.switchMainImage = function(img_url, original_url) {
  $('#product_main_image').attr('src',img_url).parent('a').attr('href',original_url);
};


$(document).ready(function(){

  var thumbsPrev = $('#thumbs_prev_button');
  var thumbsNext = $('#thumbs_next_button');
  var imgStart = 1;
  var imgStop = 4;
  
  bindThumbBrowseButtons(thumbsPrev, thumbsNext);
  
  
});

