jQuery(function ($) {

   //Fix IE
   if ($.browser.msie && $.browser.version < 7) {
      $('img[src$=".png"], .content .column, .arrow').ifixpng();
   }

   //Wrap fuer Ueber uns
   (function () {
      var aboutItems = $('.aboutEntry'),
         collapsedHeight = aboutItems.height(),
         slideDuration = 500,
         expand,
         collapse,
         collapseAll,
         isCollapsed;

      expand = function (item) {
         var targetHeight;

         item = $(item);
         item.find('.title .arrow').removeClass('collapsed');

         if (isCollapsed(item)) {
            targetHeight = item.find('.wrap').height();

            item.stop().animate({
               height: targetHeight+'px'
            }, slideDuration);
         }
      };

      collapse = function (item) {
         var item = $(item);

         item.find('.title .arrow').addClass('collapsed');

         if (!isCollapsed(item)) {
            item.stop().animate({
               height: collapsedHeight+'px'
            }, slideDuration);
         }
      };

      collapseAll = function () {
         aboutItems.stop().each(function () {
            collapse(this);
         });
      };

      isCollapsed = function (item) {
         return $(item).height() == collapsedHeight;
      };

      aboutItems.each(function () {
         var item = $(this);

         $('.title', this).click(function (event) {
            event.preventDefault();

            if (isCollapsed(item)) {
               collapseAll();
               expand(item);
            } else {
               collapse(item);
            }
         });
      });
   }());

   //Wrap fuer Weiter Termine
   (function () {
      var aboutItems = $('.furtherEvents'),
         collapsedHeight = aboutItems.height(),
         slideDuration = 500,
         expand,
         collapse,
         collapseAll,
         isCollapsed;

      expand = function (item) {
         var targetHeight;

         item = $(item);
         item.find('.openWrap .arrow').addClass('collapsed');

         if (isCollapsed(item)) {
            targetHeight = item.find('.wrap').height();

            item.stop().animate({
               height: targetHeight+'px'
            }, slideDuration);
         }
      };

      collapse = function (item) {
         var item = $(item);

         item.find('.openWrap .arrow').removeClass('collapsed');

         if (!isCollapsed(item)) {
            item.stop().animate({
               height: collapsedHeight+'px'
            }, slideDuration);
         }
      };

      collapseAll = function () {
         aboutItems.stop().each(function () {
            collapse(this);
         });
      };

      isCollapsed = function (item) {
         return $(item).height() == collapsedHeight;
      };

      aboutItems.each(function () {
         var item = $(this);

         $('.openWrap', this).click(function (event) {
            event.preventDefault();

            if (isCollapsed(item)) {
               collapseAll();
               expand(item);
            } else {
               collapse(item);
            }
         }).click();
      });
   }());
});

$(window).load(function() {
   /*
    * media wrap
    */
   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();
      }

      // add player icons
      var slider = $(this).find('.slider');
      $(this).find('.slider .pictureWrap .video-id').each(function() {
         $('<img src="'+DR+'/media/png/icon_play.png" alt="" />').css({
            position: 'absolute',
            top:      '6px',
            left:     parseInt($(this).parent().offset().left - slider.offset().left + 20)+'px'
         }).appendTo($(this).parent());
      });

      // 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;
      });

   });
});

$('document').ready(function(){
 $('.bookingTable .submitPresent').click(function(){
       var values = $('.bookingPresent').serializeElementToQueryString();
       alert('values '+values);
       document.location.href = DOCUMENT_ROOT + '/de_DE/booking/?' + values; 
       
       return false;
   });
   
  $('.bookingTable .submitSelf').click(function(){
       var values = $('.bookingSelf').serializeElementToQueryString();
       alert('values '+values);
       document.location.href = DOCUMENT_ROOT + '/de_DE/booking/?' + values; 
       
       return false;
   });
});
