/**
 *  Handles product image swaping for colors on the catalog pages
 */
 
$(function() {
  // hide catalog menu inactive items
  $('.block-catalog_menu li li>ul:not(.active)').hide();
  $('.block-catalog_menu .end').hide();
  // color image swaping trigger
  $('li.color-swatch').click(function() { 
    imageSwap(this);
  });
});


function imageSwap(element) {
  id = $(element).attr('id');
  colorImage = $(element).attr('src');

  // wait till rollover image is loaded to perform animation
  $(new Image()).attr('src', colorImage).load(function(){
    $('#' + id + '-bkg').css('background','url('+colorImage+') no-repeat top center');
    $('#' + id + '-img').fadeOut(500, function() {
      $('#' + id + '-img').attr('src', colorImage);
      $('#' + id + '-img').fadeIn(250);
    });
  });

  return false;
}
