$(document).ready(function() {  
  var cycleOptions = {
    speed: 500,
    timeout: 5000,
    pause: 1,
    fx: 'scrollVert',
    delay: 100
  };

  $('div.product_category div.details').hide();

  /* Set up the left cycle */
  $('#showcase div.left .cycle').cycle(
    $.extend({}, cycleOptions, {
      prev: 'div.left div.buttons a.up',
      next: 'div.left div.buttons a.down'
    })
  );

  /* Set up the right cycle */
  $('#showcase div.right .cycle').cycle(
    $.extend({}, cycleOptions, {
      prev: 'div.right div.buttons a.down',
      next: 'div.right div.buttons a.up',
      rev: 1
    })
  );
  
  /* Open the Product Category page when the overlay is clicked */
  $('div.overlay').click(function(event){
    var parent = $(event.target).closest('div.side');
    if (parent.size() > 0){
      window.location = parent.find('a').attr('href');      
    }
  });
  
  /* Pause all cycles on hover */
  $('div.overlay, div.products').hover(
    function(){
      $('.cycle').cycle('pause');
    },
    function(){
      $('.cycle').cycle('resume');
    }
  );
  
  $('div.buttons').hover(
    function(event){
      event.stopPropagation();
      $(this).parents('div').mouseleave();
    },
    function(event){
      $(this).parents('div').mouseenter();
    }
  );

  $('div.overlay div.left, div.overlay div.right').hover(
    function(){
      $(this).parents('div.overlay').addClass("hover_" + $(this).attr('class'));
      $(this).find('div.product_category div.details').show();
    },
    function(){
      $(this).parents('div.overlay').removeClass("hover_" + $(this).attr('class'));
      $(this).find('div.product_category div.details').hide();
    }
  );
});
