$(document).ready(function() {
 if($('.list_360').length > 1){
    var pic_X = $('.list_360').offset().left;
    var pic_W = $('.list_360').width() / 2;
    var center_X = pic_X + pic_W;
    var movestop = pic_W / 10;
    var isMouseDown = false;

    var previousMouseX;
    var displacementCounter = 0;

    // Ustawienie początkowego elementu jako aktywnego
    var middleIndex = Math.floor($('.list_360 li').length / 2);
    $('.list_360 li').eq(middleIndex).addClass('active');

    // Obsługa zdarzenia kliknięcia lewego przycisku myszy
    $('.list_360').on('mousedown touchstart', function(event) {
        if (event.which === 1 || event.type === 'touchstart') {
            isMouseDown = true;
        }
    });

    // Obsługa zdarzenia puszczenia lewego przycisku myszy
    $(document).on('mouseup touchend', function(event) {
        if (event.which === 1 || event.type === 'touchend') {
            isMouseDown = false;
        }
    });

    // Obsługa poruszania myszą lub dotyku
    $('.list_360').on('mousemove touchmove', function(event) {
        if (isMouseDown) {
            var mouse_X;
            if (event.type === 'touchmove') {
                mouse_X = event.originalEvent.touches[0].pageX;
            } else {
                mouse_X = event.pageX;
            }

            if (typeof previousMouseX !== 'undefined') {
                var displacement = mouse_X - previousMouseX;

                if (displacement !== 0) {
                    if ((displacement > 0 && displacementCounter < 0) || (displacement < 0 && displacementCounter > 0)) {
                        displacementCounter = 0;
                    }

                    displacementCounter += displacement;

                    if (Math.abs(displacementCounter) >= 2) {
                        var myreturn = displacementCounter > 0 ? 1 : 0;
                        displacementCounter = 0;

                        previousMouseX = mouse_X;
                        console.log("Przesunięcie: " + myreturn);
                        moveImg(myreturn);
                    }
                }
            }

            previousMouseX = mouse_X;
        }
    });

    function moveImg(dir) {
        var activeElement = $('.list_360 li.active');

        if (dir) {
            var nextElement = activeElement.next();

            if (nextElement.length > 0) {
                activeElement.removeClass('active');
                nextElement.addClass('active');
            }
        } else {
            var prevElement = activeElement.prev();

            if (prevElement.length > 0) {
                activeElement.removeClass('active');
                prevElement.addClass('active');
            }
        }
    }
	}
});







$(document).ready(function() {
  if ($('#product_id').length > 0 ) {
        var product_id = $('#product_id').val();
  }
  
  $('#product_media_list').slick({
  infinite: true,
  lazyLoad: 'ondemand',
  fade: true,
  cssEase: 'linear',    
  speed: 800,
  arrows: false,
  draggable: false,
  swipe: false,
  slidesToShow: 1,
  centerMode: false, 
  variableWidth: false,    
  });
  if($('#product_thumb_list > div').length > 1){
    $('#product_thumb_list').slick({
    infinite: true,
	initialSlide: 0,
    centerMode: false,
	centerPadding: '5px',
    variableWidth: true,
	focusOnSelect: true, 
	draggable: true,
  	swipe: true,  
    lazyLoad: 'ondemand',    
    speed: 800,
    slidesToShow: 3,
    asNavFor: '#product_media_list'
    });
  }

  $('.fancybox').fancybox({cyclic: true});
  $('#tab-description img').each(function(){
  myparent = $(this).parent();
 
  if(myparent.get( 0 ).tagName != 'TD'){
  var parentTag = $( this ).parent().get( 0 ).tagName;
  if(parentTag != 'A'){
    mylink = $(this).attr('src');
    $(this).wrap($('<a class="fancybox" data-fancybox="gallery" rel="fancybox" href="'+mylink+'">',{
     href: 'mylink'
    }));
    
    }
    }else{
     if(parentTag != 'A'){
      $(this).attr('data-toggle', "tooltip");
      $(this).attr('data-placement', "bottom");
      $(this).addClass('info_icon');
      $(this).css('width','25px!important');
      $(this).css('height','25px!important');
      
    }
    }
});

$('#product_media_list .box_img a').on('click', function(event) {
      event.preventDefault();
	  
	if ($('#product_media_gallery_popup').length > 0 ) {
	   $('#product_media_gallery_popup').modal({show:true});
	}else{
		openMediaGallery();
	}
      
});

function openMediaGallery(){
	$.ajax({
		url: 'index.php?route=product/product/productMediaGallery&product_id=' + product_id,
		type: 'post',
		dataType: 'json',
		beforeSend: function() {
			$('#button-review').button('loading');
		},
		complete: function() {
			$('#button-review').button('reset');
		},
		success: function(json) {
	    if(json['success']) {
		    $( "#content" ).append(json['success']);
	        $('#product_media_gallery_popup').modal({show:true});
		    $('#product_question').on('hidden.bs.modal', function () {
	          $('#product_question').remove();
	        });
	    }
		}
	});

}    

  $('#productNotifyAvailability').on('click', function() {
    
    var product_id = $('#product_id').val();
    

  
  	$.ajax({
  		url: 'index.php?route=product/product/productNotifyAvailability&product_id=' + product_id,
  		type: 'post',
  		dataType: 'json',
  		beforeSend: function() {
  			$('#productNotifyAvailability').button('loading');
  		},
  		complete: function() {
  			$('#productNotifyAvailability').button('reset');
  		},
  		success: function(json) {
        if (json['success']) {
        $( "#content" ).append(json['success']);
        
  			$('#product_notify_availability').modal({show:true});
        $('#product_notify_availability').on('hidden.bs.modal', function () {
          $('#product_notify_availability').remove();
        });
        }
  		}
  	});
  });
  
$('#content').on('click', '#button-productNotifyAvailability', function(){
    
    if($('#agree').is(":checked")){
       agree = 1;
    }else{
      agree = 0;
    }
   
 
    if ($('#product_id').length > 0 ) {
        var product_id = $('#product_id').val();
    }
  
    if($(this).attr('question_type')){
       var question_type = $(this).attr('question_type');
    }else{
       var question_type = 'product_normal'
    }
    
    if ($("#g-recaptcha-response").length) {
      captcha = $('#g-recaptcha-response').val();
    }else{
      captcha = '';
    }
    	$.ajax({
    		type: 'POST',
    		url: 'index.php?route=product/product/productNotifyAvailabilityAdd&product_id=' + product_id,
    		dataType: 'json',
    		data: 'g-recaptcha-response='+captcha+'&question_agree='+agree+'&name=' + encodeURIComponent($('input[name=\'name\']').val()) + '&email=' + encodeURIComponent($('input[name=\'email\']').val()),
    		beforeSend: function() {
                $('.loading_box').html('<div role="status" class="spinner-border text-primary"></div>');
    			$('.customForm .text-danger').remove();
				$('.info_box').html('');
    		},
    		complete: function() {
    		
    		},
    		success: function(data) {
            	$('.loading_box').html('');
    			if (data.error) {
                
                    for (i in data['error']) {
						var element = $('#' + i);
						var error_text = '<div class="text-danger">' + data['error'][i] + '</div>';
						
						if ($(element).parent().hasClass('form-group')) {
							$(element).parent().append(error_text);
						} else {
							$(element).after(error_text);
						}
					}    				
    			}else if (data.info) {
				       $('.info_box').html(data['info']['notify_availability_exist']);
				}else{
                     $('#product_notify_availability').modal("hide");
                     notify.sended_product_question('fa fa-bell', data['success']);  		
      			}
    		}
    });
  });  
  
  $('#productQuestion, .productQuestion').on('click', function() {
    
    if(!product_id){
       var product_id = $(this).attr('product_id');
    }
    
    if($(this).attr('question_type')){
       var question_type = $(this).attr('question_type');
    }else{
       var question_type = 'product_normal'
    }
    
    if($(this).attr('data_serialized')){
       var data_serialized = $(this).attr('data_serialized');
    }else{
       var data_serialized = '';
    }
  
  	$.ajax({
  		url: 'index.php?route=product/product/productQuestion&product_id=' + product_id + '&question_type=' + question_type,
  		type: 'post',
  		dataType: 'json',
        data: 'data_serialized='+data_serialized,
  		beforeSend: function() {
  			$('#button-review').button('loading');
  		},
  		complete: function() {
  			$('#button-review').button('reset');
  		},
  		success: function(json) {
        if (json['success']) {
        $( "#content" ).append(json['success']);
        
  			$('#product_question').modal({show:true});
        $('#product_question').on('hidden.bs.modal', function () {
          $('#product_question').remove();
        });
        }
  		}
  	});
  });
  
  
  $('#content').on('click', '#button-question-product', function(){
    
    if($('#agree').is(":checked")){
       agree = 1;
    }else{
      agree = 0;
    }
   
 
    if ($('#product_id').length > 0 ) {
        var product_id = $('#product_id').val();
    }else{
        var product_id = $(this).attr('product_id');
    }
  
    if($(this).attr('question_type')){
       var question_type = $(this).attr('question_type');
    }else{
       var question_type = 'product_normal'
    }
    
    if($(this).attr('data_serialized')){
       var data_serialized = $(this).attr('data_serialized');
    }else{
       var data_serialized = '';
    }
    
    if ($("#g-recaptcha-response").length) {
      captcha = $('#g-recaptcha-response').val();
    }else{
      captcha = '';
    }
    	$.ajax({
    		type: 'POST',
    		url: 'index.php?route=mail/product/productQuestion&product_id=' + product_id+ '&question_type=' + question_type,
    		dataType: 'json',
    		data: 'data_serialized='+data_serialized+'g-recaptcha-response='+captcha+'&question_agree='+agree+'&name=' + encodeURIComponent($('input[name=\'questionprice_name\']').val()) + '&text=' + encodeURIComponent($('textarea[name=\'questionprice_text\']').val()) + '&email=' + encodeURIComponent($('input[name=\'questionprice_email\']').val()) + '&company=' + encodeURIComponent($('input[name=\'questionprice_company\']').val()) + '&nip=' + encodeURIComponent($('input[name=\'questionprice_nip\']').val()) + '&telephone=' + encodeURIComponent($('input[name=\'questionprice_telephone\']').val()),
    		beforeSend: function() {
                $('.loading_box').html('<div role="status" class="spinner-border text-primary"></div>');
    			$('.customForm .text-danger').remove();
    		},
    		complete: function() {
    		
    		},
    		success: function(data) {
            	$('.loading_box').html('');
    			if (data.error) {
                
                    for (i in data['error']) {
						var element = $('#' + i);
						var error_text = '<div class="text-danger">' + data['error'][i] + '</div>';
						
						if ($(element).parent().hasClass('form-group')) {
							$(element).parent().append(error_text);
						} else {
							$(element).after(error_text);
						}
					}    				
    			}else{
                     $('#product_question').modal("hide");
                     notify.sended_product_question('fa fa-envelope', data['success']);  		
      			}
    		}
    });
  });
  
  
  $('#button-add_new_review').on('click', function() {
  	$.ajax({
  		url: 'index.php?route=product/product/productReview&product_id=' + product_id,
  		type: 'post',
  		dataType: 'json',
  		beforeSend: function() {
  			$('#button-review').button('loading');
  		},
  		complete: function() {
  			$('#button-review').button('reset');
  		},
  		success: function(json) {
        if (json['success']) {
        $( "#content" ).append(json['success']);
        
  		$('#product_review').modal({show:true});
        $('#product_review').on('hidden.bs.modal', function () {
          $('#product_review').remove();
        });
        }
  		}
  	});
  });
  
  $('body').on('click', '#button-review', function(){

    if ($("#g-recaptcha-response").length) {
      captcha = $('#g-recaptcha-response').val();
    }else{
      captcha = '';
    }
    $.ajax({
  		type: 'POST',
  		url: 'index.php?route=product/product/write&product_id=' + product_id,
  		dataType: 'json',
  		data: 'g-recaptcha-response='+captcha+'&name=' + encodeURIComponent($('input[name=\'name\']').val()) + '&text=' + encodeURIComponent($('textarea[name=\'text\']').val()) + '&rating=' + encodeURIComponent($('input[name=\'rating\']').val()),
  		beforeSend: function() {
              $('.loading_box').html('<div role="status" class="spinner-border text-primary"></div>');
  			$('.customForm .text-danger').remove();
  		},
  		complete: function() {
  		
  		},
  		success: function(data) {
          	$('.loading_box').html('');
  			if (data.error) {
              
                  for (i in data['error']) {
					var element = $('#' + i);
					var error_text = '<div class="text-danger">' + data['error'][i] + '</div>';
					
					if ($(element).parent().hasClass('form-group')) {
						$(element).parent().append(error_text);
					} else {
						$(element).after(error_text);
					}
				}    				
  			}else{
                   $('#product_review').modal("hide");
                   notify.default('fa fa-pencil', data['success']['message'], data['success']['title']);  		
    			}
  		}
    });
  });
  
});
