require('swiper/swiper-3.4.2.jquery.min.js');
class pageIndex {
	constructor() {
		this.$newsTypeNav = $('.news-type__item')
		this.$newsWrapList = $('.news-list-wrap')
		this.init()
	}

	init() {
		this.setNewsTypeNav()
		this.setLookMore()
		this.lazyload()
		this.fixedNews()
	}
	//滑动过程中固定广告或者新闻导航
	fixedNews() {
		var self = this
		var height = $(".news-type-nav").offset().top

		$(window).scroll(function () {
			var $da = $('.da-like-block')
			if ($da.length) return
			if ($(window).scrollTop() < height) {
				$(".news-type-nav").css({ position: 'relative', 'top': 0 });
			} else {
				$(".news-type-nav").css({ position: 'fixed', 'top': 0, left: 0 });
			}
		})
	}
	setNewsTypeNav() {
		var self = this
		this.$newsTypeNav.click(function () {
			var _this = $(this)
			if (_this.hasClass('active')) return;

			var index = _this.index()
			_this.addClass('active')
				.siblings().removeClass('active')

			self.$newsWrapList.removeClass('active')
				.eq(index).addClass('active')

			var $ele = self.$newsWrapList.eq(index)

			$ele.find('img[data-src]').each((index, item) => {
				$(item).attr('src', $(item).attr('data-src')).removeAttr('data-src')
			})
		})
	}
	setLookMore() {
		var lookMore = $('.common-look-more');
		lookMore.click(function () {
			var _this = $(this),
				href = encodeURI(_this.data('href'))

			location.href = href;
		})
	}
	lazyload() {
		var finish = false
		$(window).scroll(function () {
			if (finish) return
			var $imgs = $('img[data-lazy]'),
				$win = $(this),
				offsetBottom = $win.scrollTop() + $win.height()//底部的offsetY
			if ($imgs.length == 0) {
				finish = true
				return
			}
			$imgs.each((index, item) => {
				var $item = $(item)
				if (offsetBottom >= $item.offset().top - 100)
					$item.attr('src', $item.attr('data-lazy')).removeAttr('data-lazy')
			})
		})

		$(window).scroll()

		return this
	}
}

$(() => {
	new pageIndex()

	var swiper = new Swiper('.dw-common-nav', {
		slidesPerView: 'auto',
		freeMode: true
	});

	var swiper = new Swiper('.news-type-nav', {
		slidesPerView: 'auto',
		freeMode: true
	});
})
