/**
* @module       Swiper Slider
* @description  Enables Swiper Plugin
*/
;
(function ($, undefined) {
	var o = $(".swiper-slider");
	if (o.length) {
		function getSwiperHeight(object, attr) {
			var val = object.attr("data-" + attr),
			dim;
			if (!val) {
				return undefined;
			}
			dim = val.match(/(px)|(%)|(vh)$/i);
			if (dim.length) {
				switch (dim[0]) {
					case "px":
					return parseFloat(val);
					case "vh":
					return $(window).height() * (parseFloat(val) / 100);
					case "%":
					return object.width() * (parseFloat(val) / 100);
				}
			} else {
				return undefined;
			}
		}
		function toggleSwiperInnerVideos(swiper) {
			var prevSlide = $(swiper.slides[swiper.previousIndex]),
			nextSlide = $(swiper.slides[swiper.activeIndex]),
			videos;
			prevSlide.find("video").each(function () {
				this.pause();
			});
			videos = nextSlide.find("video");
			if (videos.length) {
				videos.get(0).play();
			}
		}
		function toggleSwiperCaptionAnimation(swiper) {
			var prevSlide = $(swiper.container),
			nextSlide = $(swiper.slides[swiper.activeIndex]);
			prevSlide
			.find("[data-caption-animate]")
			.each(function () {
				var $this = $(this);
				$this
				.removeClass("animated")
				.removeClass($this.attr("data-caption-animate"))
				.addClass("not-animated");
			});
			nextSlide
			.find("[data-caption-animate]")
			.each(function () {
				var $this = $(this),
				delay = $this.attr("data-caption-delay");
				setTimeout(function () {
					$this
					.removeClass("not-animated")
					.addClass($this.attr("data-caption-animate"))
					.addClass("animated");
				}, delay ? parseInt(delay) : 0);
			});
		}
		$(document).ready(function () {
			o.each(function () {
				var s = $(this);
				var pag = s.find(".swiper-pagination"),
				next = s.find(".swiper-button-next"),
				prev = s.find(".swiper-button-prev"),
				bar = s.find(".swiper-scrollbar"),
				h = getSwiperHeight(o, "height"), mh = getSwiperHeight(o, "min-height");
				s.find(".swiper-slide")
				.each(function () {
					var $this = $(this),
					url;
					if (url = $this.attr("data-slide-bg")) {
						$this.css({
							"background-image": "url(" + url + ")",
							"background-size": "cover"
						})
					}
				})
				.end()
				.find("[data-caption-animate]")
				.addClass("not-animated")
				.end()
				.swiper({
					autoplay: s.attr('data-autoplay') ? s.attr('data-autoplay') === "false" ? undefined : s.attr('data-autoplay') : 5000,
					effect: s.attr('data-slide-effect') ? s.attr('data-slide-effect') : "slide",
					speed: s.attr('data-slide-speed') ? s.attr('data-slide-speed') : 600,
					keyboardControl: s.attr('data-keyboard') === "true",
					mousewheelControl: s.attr('data-mousewheel') === "true",
					mousewheelReleaseOnEdges: s.attr('data-mousewheel-release') === "true",
					nextButton: next.length ? next.get(0) : null,
					prevButton: prev.length ? prev.get(0) : null,
					pagination: pag.length ? pag.get(0) : null,
					paginationClickable: pag.length ? pag.attr("data-clickable") !== "false" : false,
					paginationBulletRender: pag.length ? pag.attr("data-index-bullet") === "true" ? function (index, className) {
						return '<span class="' + className + '">' + (index + 1) + '</span>';
					} : null : null,
					scrollbar: (bar.length) ? bar.get(0) : null,
					scrollbarDraggable: bar.length ? bar.attr("data-draggable") !== "false" : true,
					scrollbarSnapOnRelease: true,
					scrollbarHide: bar.length ? bar.attr("data-draggable") === "false" : false,
					loop: s.attr('data-loop') !== "false",
					onTransitionStart: function (swiper) {
						toggleSwiperInnerVideos(swiper);
					},
					onTransitionEnd: function (swiper) {
						toggleSwiperCaptionAnimation(swiper);
					},
					onInit: function (swiper) {
						toggleSwiperInnerVideos(swiper);
						toggleSwiperCaptionAnimation(swiper);
					}
				});
				$(window)
				.on("resize", function () {
					var mh = getSwiperHeight(s, "min-height"),
					h = getSwiperHeight(s, "height");
					if (h) {
						s.css("height", mh ? mh > h ? mh : h : h);
					}
				})
				.trigger("resize");
			});
		});
	}
})(jQuery);