// JavaScript Document

//moving products
var lFollowX = 0,
	lFollowY = 0,
	x = 0,
	y = 0,
	friction = 1 / 30;

function moveBackground() {
  x += (lFollowX - x) * friction;
  y += (lFollowY - y) * friction;

  translate = 'translate(' + x + 'px, ' + y + 'px) scale(1)';


  $('.p2').css({
	'-webit-transform': translate,
	'-moz-transform': translate,
	'transform': translate
  });

  window.requestAnimationFrame(moveBackground);
}

$(window).on('mousemove click', function(e) {

  var lMouseX = Math.max(-100, Math.min(100, $(window).width() / 2 - e.clientX));
  var lMouseY = Math.max(-100, Math.min(100, $(window).height() / 2 - e.clientY));
  lFollowX = (30 * lMouseX) / 300; // 100 : 12 = lMouxeX : lFollow
  lFollowY = (10 * lMouseY) / 300;

});

moveBackground();
//moving products




//read mode
$("#more_txt").click(function() {			
	$(".more_txt").toggleClass("open");
});

var interruptor=0;

$("#more_txt").click(function () {
  if(interruptor==0) {
	$(this).text("Read less");
	interruptor=1;
  }
  else {
	$(this).text("Read more");
	interruptor=0;
  }
});
//read mode

//milestone counter
(function($) {
	$.fn.countTo = function(options) {
		// merge the default plugin settings with the custom options
		options = $.extend({}, $.fn.countTo.defaults, options || {});
		// how many times to update the value, and how much to increment the value on each update
		var loops = Math.ceil(options.speed / options.refreshInterval),
		increment = (options.to - options.from) / loops;

		return $(this).each(function() {
			var _this = this,
			loopCount = 0,
			value = options.from,
			interval = setInterval(updateTimer, options.refreshInterval);

			function updateTimer() {
				value += increment;
				loopCount++;
				$(_this).html(value.toFixed(options.decimals));

				if (typeof(options.onUpdate) == 'function') {
					options.onUpdate.call(_this, value);
				}

				if (loopCount >= loops) {
					clearInterval(interval);
					value = options.to;

					if (typeof(options.onComplete) == 'function') {
						options.onComplete.call(_this, value);
					}
				}
			}
		});
	};

	$.fn.countTo.defaults = {
		from: 0,				// the number the element should start at
		to: 100,				// the number the element should end at
		speed: 1000,			// how long it should take to count between the target numbers
		refreshInterval: 100,	// how often the element should be updated
		decimals: 0,			// the number of decimal places to show
		onUpdate: null,			// callback method for every time the element is updated,
		onComplete: null,		// callback method for when the element finishes updating
	};
})(jQuery);


function isViewed(selector) {

	var viewport = $(window),
	item = $(selector);

	var viewTop = viewport.scrollTop(),
		viewBtm = viewport.scrollTop() + viewport.height(),
		itemTop = item.offset().top,
		itemBtm = item.offset().top + item.height();

	return ((itemTop < viewBtm) && (itemTop > viewTop));
	};

	var counter = setInterval(function() {countdown()}, 500);

var countdown = function() {
	var random1 = Math.floor(Math.random()*(343-14+1)) + 14;
	var random2 = Math.floor(Math.random()*(5891-5627+1)) + 5627;
	var random3 = Math.floor(Math.random()*(686-28+1)) + 28;
	var random4 = Math.floor(Math.random()*(5627-4872+1)) + 4872;
	if(isViewed('.milestone')) {
		clearInterval(counter);
		$('.timer1').countTo({
			from: 0,
			to: 500,
			speed: 1000,
			refreshInterval: 20,
		});
		$('.timer2').countTo({
			from: 0,
			to: 6,
			speed: 1000,
			refreshInterval: 20,
		});
		$('.timer3').countTo({
			from: 0,
			to: 20,
			speed: 1000,
			refreshInterval: 20,
		});
		$('.timer4').countTo({
			from: 0,
			to: 10,
			speed: 1000,
			refreshInterval: 20,
		});
	};
};
//milestone counter

//smooth scrool
var $root = $('html, body');
$('.menu > li > a').click(function() {
    $root.animate({
        scrollTop: $( $.attr(this, 'href') ).offset().top-20
    }, 1000);
    return false;
});
//smooth scrool

// ===== Scroll to Top ==== 
//go to top
$(window).scroll(function() {
    if ($(this).scrollTop() > 250) {
        $('#return-to-top').fadeIn();
    } else {
        $('#return-to-top').fadeOut();
    }
});
$("#return-to-top").click(function () {
   $("html, body").animate({scrollTop: 0}, 1000);
});

$(".mobile_menu").click(function () {
   $(".mobile_mnu_bx").slideToggle(800);
});
$(".mobile_mnu_bx > ul > li > a").click(function () {
   $(".mobile_mnu_bx").slideUp(800);
});


$(".spkr_bx").hover(function() {
	$(this).parent().find('.spkr_bx > .img-fluid').toggleClass('hover_img');
    //$(".spkr_bx > .img-fluid").toggleClass("hover_img");
});

$('[data-fancybox="images"]').fancybox({
  buttons : [ 
    'slideShow',
    'share',
    'zoom',
    'fullScreen',
    'close'
  ],
  thumbs : {
    autoStart : true
  }
});

