jQuery(document).ready(function(){

   function openImage(fBox, fOnLoad) {
      var tLink = fBox.find('.single-image');
      if(tLink.find('img').length < 1) { //alert(tLink.get(0).href);
         var tImage = jQuery('<img src="' + tLink.get(0).href + '" />')
                 .load(function(){ jQuery(this).removeClass('image-loading');if(fOnLoad) fOnLoad(); });

         tLink.append(tImage);
      } else {
         if(fOnLoad) fOnLoad();
      }
   }


   var tGalleryNextPrevClick = function(fCompleteHandler) {
      var tGallery = jQuery(this).parents('.gallery:first');
      var tGWrapper = tGallery.find('.gallery-wrapper');
      var tNavigation = tGallery.find('.gallery-navigation');
      var tNavigationHeight = tNavigation.height();
      tGWrapper.css('height', tGWrapper.height());

      var tDirection = jQuery(this).hasClass('next');

      var tCurrent = tGWrapper.find('.gallery-photo-item:visible');
      var tNext = ( tDirection ? tCurrent.next('.gallery-photo-item') : tCurrent.prev('.gallery-photo-item'));
      if(tNext.length < 1)
         tNext = tGWrapper.find('.gallery-photo-item:' + (tDirection ? 'first' : 'last'));

      var tCompleter = jQuery.isFunction(fCompleteHandler) ? fCompleteHandler : jQuery.noop;

      openImage(tNext, function() {
         var tHeight = tNext.height();
         if(tHeight > 0)
            tGWrapper.height(tNext.height() + (tNavigation.is(':visible') ? tNavigationHeight : 0));

         slideHorizontalAnimation(tCurrent, tNext, tDirection, function(){
            tCurrent.css('display', 'none');
            tNext.css('width', '100%');
            if(tHeight == 0)
               tGWrapper.height(tNext.height() + (tNavigation.is(':visible') ? tNavigationHeight : 0));

            tCompleter(tNext.get(0));
         });
      });

      return false;
   };


   jQuery('.gallery').each(function(){
      var tGallery = jQuery(this);
      var tViewport = tGallery.find('.gallery-viewport');
      var tNavigation = tGallery.find('.gallery-navigation');

      var tSlideShowTimeout = true;

      jQuery('.gallery-wrapper', this).each(function(){
         var tPhotoItem = jQuery('.gallery-photo-item:first', this).css('display', 'block');

         openImage(tPhotoItem, function(){
            if(tGallery.hasClass('gallery-start-slideshow')) {
               tNavigation.hide();
               var tStartSlideShow = function() {
                  if(!tSlideShowTimeout)
                     return;

                  clearTimeout(tSlideShowTimeout);
                  tGalleryNextPrevClick.call(tNavigation.find('.next').get(0), function() {
                     tSlideShowTimeout = setTimeout(tStartSlideShow, 5000);
                  });
               };
               tSlideShowTimeout = setTimeout(tStartSlideShow, 5000);
            }
         });
      });

      var tImages = tGallery.find('.gallery-photo-item .single-image').bind('click', function(){
         try { clearTimeout(tSlideShowTimeout); } catch(e) { /*do nothing*/ }
         tSlideShowTimeout = false;
         tNavigation.fadeIn(500);
         return false;
      });

      if(tViewport.find('.gallery-photo-item').length < 2)
         tNavigation.css('display', 'none');
      else {
         var tPointsWrapper = jQuery('<ul />');
         tGallery.find('.gallery-photo-item').each(function() {
            var tL = jQuery('<li />');
            tPointsWrapper.append(tL);
            if(jQuery(this).is(':visible'))
               tL.addClass('active');
            this.jsGalleryPoint = tL;
         });
         tPointsWrapper.find('li:first').addClass('active');
         var tDiv = jQuery('<div class="points-wrapper" />').append(tPointsWrapper);
         tNavigation.append(tDiv);

         var tGNC = function(fE) {
            if(fE) fE.preventDefault();
            tGalleryNextPrevClick.call(this, function(fNextItem) {
               tPointsWrapper.find('.active').removeClass('active');
               if(fNextItem.jsGalleryPoint) {
                  fNextItem.jsGalleryPoint.addClass('active');
               }
            });
         };

         var tPrevButton = tNavigation.find('.prev').click(tGNC);
         var tNextButton = tNavigation.find('.next').click(tGNC);

         /* scroll images by touchscreen */
         tGallery.find('.gallery-photo-item .single-image').touchwipe({
            wipeLeft: function() {
               tNextButton.click();
            },
            wipeRight: function() {
               tPrevButton.click();
            },
            preventDefaultEvents: false
         });
      }
   });


   function slideHorizontalAnimation(fCurrentPage, fNewPage, fForward, fCompleteHandler, fDuration) {
      var tCurrentWidth = fCurrentPage.width();
      var tPageOld = fCurrentPage;
      var tCurrentPage = fNewPage;
      var tPageOldOffset = tPageOld.offset();
      if(tPageOldOffset == null)
         tPageOldOffset = { y: 0 };
      tPageOld.css('position', 'absolute').css('width', tCurrentWidth);
      tCurrentPage.css('position', 'absolute').css('top', tPageOldOffset.y).css('left', (fForward ? tCurrentWidth : -tCurrentWidth)).css('width', tCurrentWidth).css('display', 'block');
      //tPageOld.add(tCurrentPage).animate({ left: (fForward ? '-' : '+') + '=' + tCurrentWidth + 'px' },
      tPageOld.animate({ left: (fForward ? -tCurrentWidth : tCurrentWidth)},
      { duration: fDuration, complete: jQuery.isFunction(fCompleteHandler) ? fCompleteHandler : jQuery.noop });
      tCurrentPage.animate({ left: 0 },
      { duration: fDuration, complete: jQuery.isFunction(fCompleteHandler) ? fCompleteHandler : jQuery.noop });
   }
});

