function validateForm(req) {
   for (var i = 0; i < req.length; i++) {
      if ($(req[i]).val() == '') {
         return false;
      }
   }
   return true;
}

$('document').ready(function(){
   /**
    * Send-a-friend button.
    */
   $('#sendaFriend .sendButton').click(function() {
      var params = $('#sendaFriend').serializeArray();
      if(validateForm($('#sendaFriend .required'))) {
         $('#sendaFriendForm').html($('#TB_ajaxContent').html());
         $('#TB_ajaxContent').load(DOCUMENT_ROOT+'/pages/sendafriend.php', params);
      } else
         alert('Bitte füllen Sie alle Formularfelder aus.');
   });

   /**
    * More-button for very long texts.
    */
   $('.expandable').each(function() {
      $(this).find('.fullText').hide();
      $(this).find('.shortText .more .alternativeLink').click($.proxy(function() {
         $(this).find('.shortText').hide();
         $(this).find('.fullText').show();
         return false;
      }, this));
   });

   /**
    * Slider
    */
   (function (viewportElement, controlsElement) {
      var sliderElement, // const
         itemElements, // const
         positionIndex,
         applyPosition; // const

      // Does the slider even exist?
      if (viewportElement.length > 0 && controlsElement.length > 0) {
         sliderElement = viewportElement.find('.slider');
         itemElements = sliderElement.find('.item');

         // Do we need controls?
         if (itemElements.length <= 2) {
            controlsElement.hide();
         }

         // Make sure the items fit in the slider.
         sliderElement.width(itemElements.outerWidth(true) * itemElements.length);

         // Initialize the slider at the very left.
         viewportElement.scrollLeft(0);
         positionIndex = 0;
         controlsElement.find('.info .currentIndex').text(positionIndex + 1);
         controlsElement.find('.info .itemCount').text(itemElements.length);

         /**
          * Moves the slider to the current position in a certain time.
          *
          * @param duration Duration of the animation (in ms). 0 means no
          *    animation. Default: 500 ms.
          */
         applyPosition = function (duration) {
            var item, // const
               scrollPosition; // const

            // Provide default argument values.
            duration = duration || 750;

            // Set new position index.
            controlsElement.find('.info .currentIndex').text(positionIndex + 1);

            // Determine target item.
            item = itemElements.eq(positionIndex);

            // Determine target scroll position.
            scrollPosition = item.offset().left - sliderElement.offset().left;

            // Do we need to animate?
            if (duration > 0) {
               viewportElement.stop().animate({
                  scrollLeft: scrollPosition
               }, duration);
            } else {
               viewportElement.scrollLeft(scrollPosition);
            }
         };

         // Bind controls.
         controlsElement.find('.prev').click(function () {
            positionIndex = Math.max(positionIndex - 1, 0);
            applyPosition();
            return false;
         });
         controlsElement.find('.next').click(function () {
            positionIndex = Math.min(positionIndex + 1, itemElements.length - 2);
            applyPosition();
            return false;
         });
      }
   }($('.contentSlider .viewport'), $('.contentSlider .controls')));
});
