/**
 * Bootstrap Carousel Swipe v1.1
 *
 * jQuery plugin to enable swipe gestures on Bootstrap 3 carousels.
 * Examples and documentation: https://github.com/maaaaark/bcSwipe
 *
 * Licensed under the MIT license.
 */
(function($) {
    var addShown = false;
    $.fn.bcSwipe = function(settings) {
    var config = { threshold: 50 };
    if (settings) {
      $.extend(config, settings);
    }

    //console.log(this);

    this.each(function() {
      var stillMoving = false;
      var start;

        //alert('1');

      if ('ontouchstart' in document.documentElement) {
        //alert('test');
          this.addEventListener('touchstart', onTouchStart, false);
      }

      function onTouchStart(e) {
        if (e.touches.length == 1) {
          //alert('1');
          start = e.touches[0].pageX;
          stillMoving = true;
          this.addEventListener('touchmove', onTouchMove, false);
        }
      }

      function onTouchMove(e) {
        //alert('1');
        if (stillMoving) {
          var x = e.touches[0].pageX;
          var difference = start - x;
          if (Math.abs(difference) >= config.threshold) {
            cancelTouch();

            var currentItem = $('.imageSliderG3').find('.item.active').attr('id');
            var lastItem = $('#lastItem').val();
            var firstItem = $('#firstItem').val();
            var totalItems = $('#maxItemsTotal').val();
            var number = $('#number').val();

            if (difference > 0) {
              
              if(currentItem == lastItem && currentItem != totalItems){
                pager++;
                $('#g3Pagination-pager').val(pager);
                setG3Pagination(pager);

                $('#lastItem').val(parseInt(lastItem)+parseInt(number));
                $('#firstItem').val(parseInt(firstItem)+parseInt(number));
                $('#lastChunk').val('left');

                var action = detailForm.attr("action"),
                  method = detailForm.attr("method");
                data = detailForm.serialize();
                ajaxG3Paginated(action, method, data);
              }

              /*if(currentItem !== 0 && (currentItem)%5 == 0 && fotoadd_isFilled && addShown == false) {

                    addShown = true;

                    //var prevItem = parseInt(currentItem)-1;
                    //TODO hide img information
                    //jQuery('.md-single-wrap, .info').hide();

                    jQuery('#'+currentItem).css('display','none');
                    //jQuery('#'+currentItem).removeClass('active');
                    //jQuery('#'+prevItem).addClass('active');
                    jQuery('#fotoadd').css('display','block');
                }else{*/
                    //jQuery('#fotoadd').css('display','none');
                    //jQuery('#'+currentItem).css('display','');
                    //reset marker
                    //addShown = false;
                    $(this).carousel('next');
                /*}*/

            }
            else {
              if(currentItem == firstItem && currentItem != 1){
                pager = $('#g3Pagination-pager').val();
                pager--;
                $('#g3Pagination-pager').val(pager);
                setG3Pagination(pager);

                $('#lastItem').val(lastItem-(lastItem-firstItem+1));

                $('#firstItem').val(parseInt(firstItem)-parseInt(number));
                $('#lastChunk').val('right');

                var action = detailForm.attr("action"),
                    method = detailForm.attr("method");
                data = detailForm.serialize();
                ajaxG3Paginated(action, method, data);
              }

                /*if(currentItem !== 0 && (currentItem)%5 == 0 && fotoadd_isFilled && addShown == false) {

                    addShown = true;

                    //var prevItem = parseInt(currentItem)+1;

                    //TODO hide img information
                    //jQuery('.md-single-wrap, .info').hide();

                    jQuery('#'+currentItem).css('display','none');
                    //jQuery('#'+currentItem).removeClass('active');
                    //alert(prevItem);
                    //jQuery('#'+prevItem).addClass('active');
                    jQuery('#fotoadd').css('display','block');

                }else{*/
                    /*$(this).carousel('prev');
                    jQuery('#fotoadd').css('display','none');
                    jQuery('#'+currentItem).css('display','');
                    //reset marker
                    addShown = false;*/
                    $(this).carousel('prev');
                /*}*/

            }
          }
        }
      }

      function cancelTouch() {
        
        //alert('1');
        
        this.removeEventListener('touchmove', onTouchMove);
        start = null;
        stillMoving = false;
      }
    });

    return this;
  };
})(jQuery);
