
/* =============================================================================
 * media wrap - event detail
============================================================================= */
$(window).load(function() {
   if ($(".mediaWrap .slideshow .slider").children().length == 1) {
      $(".mediaWrap .slideshow .leftButton ").hide();
      $(".mediaWrap .slideshow .rightButton ").hide();
   }

   $(".mediaWrap .slideshow .leftButton, .mediaWrap .slideshow .rightButton").hover(function() {
      $(this).stop().animate({opacity: 0.8}, 200);
   }, function() {
      $(this).stop().animate({opacity: 0}, 500);
   }).animate({opacity: 0.8}, 2000, function() {
      $(this).animate({opacity: 0}, 2000);
   });
   $(".mediaWrap .slideshow .slider > *:last").css('margin-right', '0px');

   // slideshow
   $(".mediaWrap .slideshow").each(function() {
      var options = {
         acceleration:    0.5, // how fast the slider gains speed (more means faster)
         friction:        0.5, // how fast the slider looses speed (more means faster)
         pixelsPerSecond: 500  // maximum movement speed
      };

      // movement
      var animationInterval = null;
      var currentVelocity = 0;
      var sliderWrap = $(this).find('.sliderWrap');
      options.maximumVelocity = options.pixelsPerSecond / 60;

      // prepare
      var sliderWidth = 0;
      $(this).find('.slider').children().each(function() {
         sliderWidth += $(this).outerWidth(true);
      })
      $(this).find('.slider').width(sliderWidth);

      // remove buttons if there is nothing to scroll
      if (sliderWidth <= sliderWrap) {
         $(this).find('.leftButton, .rightButton').remove();
      }

      // animation routine
      var move = function(direction) {
         if (animationInterval !== null) {
            window.clearInterval(animationInterval);
         }

         animationInterval = window.setInterval(function() {
            if (direction < 0) { // left
               currentVelocity -= options.acceleration;
               if (currentVelocity < -options.maximumVelocity) {
                  currentVelocity = -options.maximumVelocity;
               }
            } else if (direction > 0) { // right
               currentVelocity += options.acceleration;
               if (currentVelocity > options.maximumVelocity) {
                  currentVelocity = options.maximumVelocity;
               }
            } else if (currentVelocity < 0) { // stop
               currentVelocity += options.friction;
               if (currentVelocity > 0) {
                  currentVelocity = 0;
               }
            } else if (currentVelocity > 0) { // stop
               currentVelocity -= options.friction;
               if (currentVelocity < 0) {
                  currentVelocity = 0;
               }
            } else {
               window.clearInterval(animationInterval);
            }

            // everything calculcated? okay, lets get ready to rumble.
            sliderWrap[0].scrollLeft += parseInt(currentVelocity);
         }, 16);
      };

      // bindings
      $(this).find('.leftButton').mousedown(function() {
         move(-1);
         return false;
      });
      $(this).find('.rightButton').mousedown(function() {
         move(1);
         return false;
      });
      $(this).find('.leftButton, .rightButton').mouseup(function() {
         move(0);
         return false;
      }).mouseleave(function() {
         $(this).mouseup();
         return false;
      });

   });
});

/* =============================================================================
 * shadowbox
============================================================================= */
Shadowbox.init({slideshowDelay: 5, continuous: true});

