amount_products = 0;
products_autorotation_start = true;
from_interval = true;
$(document).ready(function(){
  amount_products = $('.product_rotation .rotator .img_container').size();
  
  
  // start autorotation for productimages
  products_autorotation = do_auto_rotation('right',4000);    
  $('.product_rotation').hover(function() {
    window.clearInterval(products_autorotation); 
  },
  function() {
    products_autorotation = do_auto_rotation('right',4000);
  });
  
});

function rotate(direction) {  
    $('.product_rotation .rotator .active').fadeOut('fast', function(){
      $('.product_rotation .rotator .img_container').hide();
      act_element = $('.product_rotation .rotator .active').attr('id');
      act_element = act_element.replace(/element/,'');

      if (direction == 'left'){
        if ((parseInt(act_element)-1) < 0){ next_element = parseInt(amount_products)-1; } else {next_element = parseInt(act_element)-1; }
      }
      if (direction == 'right'){
        if ((parseInt(act_element)+1) == amount_products){ next_element = 0; } else {next_element = parseInt(act_element)+1; }
      }

      $('.product_rotation .rotator .active').removeClass('active')
      $('.product_rotation .rotator #element'+next_element).fadeIn('fast').addClass('active');
    });
  
  return false;
}

// start autorotation
function do_auto_rotation(direction, milliseconds_interval) {
  return window.setInterval("rotate('right', true)", milliseconds_interval);
}
