/*
<div class='container'>
    <div class='wrapper'>
        <div class='slide'>Slide 1</div>
        <div class='slide'>Slide 2</div>
        <div class='slide'>Slide 3</div>
    </div>
</div>

pagination = fraction | dynamic  分页样式
navigation 上下页按钮
next = .class 下一页对象
prev = .class 上一页对象
scrollbar 滚动条
effect = fade | cube | coverflow | flip   切换方式
vertical 垂直滚动
loop  循环播放
mousewheel 鼠标滚动
zoom 双击图片缩放
view = 1 - 9 - auto
cols = 1 - 9 - auto
group = 1 - 9 - auto  分组
control 控制绑定 .swiper-slide-active
thumbs 缩略图 .swiper-slide-thumb-active
autoHeight 自动高度
center 居中
free 自由滚动模式
space 空隙
index= 1+
init = fn

start = fn
end = fn
delay = 4 自动播放
parallax 视差效果
breakpoints  320:3^2,720:3^1 自适应
noSwipingSelector = '.class' 禁止部分拖拽滚动
*/
var SwiperData = {};
$.extend({
	// 读取 swiper 对象
	swiper: function(obj) {
		return SwiperData[$(obj).attr('swiper')];
	},
	swiperModule: function(a, swi) {
		var sli = swi.slides;
		var l0 = a.offset().left;
		var t0 = a.offset().top;
		var w0 = a.width();
		var h0 = a.height();
		for (var i in sli) {
			if (typeof(sli[i]) == 'object') {
				$.swiperNotInSwi($(sli[i]), l0, t0, w0, h0);
				$(sli[i]).find('[module]').each(function() {
					$.swiperNotInSwi($(this), l0, t0, w0, h0);
				});
			}
		}
	},
	// 这个东西是为了兼容模块系统的定位功能
	// 没有特殊要求的话就忽略了吧
	swiperNotInSwi: function(a, l0, t0, w0, h0){
		var l1 = a.offset().left;
		var t1 = a.offset().top;
		var w1 = a.width();
		var h1 = a.height();
		// 不在可视区
		if ((l1+w1)<(l0+20) || l1>(l0+w0-20) || (t1+h1)<(t0+20) || t1>(t0+h0-20)) {
			a.addClass('not-in-swi');
		} else {
			a.removeClass('not-in-swi');
		}
	}
});

$.include(jextpath + 'web/swiper5/swiper.min.css');
$.include(jextpath + 'web/swiper5/swiper.min.js', function() {
	JEXTFN['WEBSWIPER'] = function(){
		jx('.container').each(function(i, e) {
			var a = $(this);
			var w = a.children('.wrapper');
			a.addClass('swiper-container');
			w.addClass('swiper-wrapper');
			w.children().addClass('swiper-slide');
			w.children().each(function(i){
				$(this).attr('index', i);
			});
			var pagination = a.attr('pagination'),
				navigation = a.attr('navigation'),
				scrollbar = a.attr('scrollbar'),
				index = parseInt(a.attr('index')||0),
				loop = a.attr('loop'),
				effect = a.attr('effect'),
				vertical = a.attr('vertical'),
				view = a.attr('view'),
				cols = a.attr('cols'),
				group = a.attr('group'),
				autoHeight = a.attr('autoHeight'),
				breakpoints = a.attr('breakpoints') || '{}',
				delay = a.attr('delay'),
				fade = a.attr('fade'),
				click = a.attr('click'),
				thumbs = a.attr('thumbs'),
				center = a.attr('center'),
				free = a.attr('free'),
				zoom = a.attr('zoom'),
				parallax = a.attr('parallax'),
				mousewheel = a.attr('mousewheel'),
				control = a.attr('control');
			if (breakpoints.indexOf('}') < 0) {
				var new_break = breakpoints.split(/,|;/);
				breakpoints = {};
				for (var k in new_break) {
					var v = new_break[k].split(':');
					breakpoints[v[0]] = v[1] ? v[1].split('^') : [];
				}
			} else {
				breakpoints = $.parseJSON(breakpoints);
			}
			if (($(thumbs).size() && !$.swiper(thumbs)) || ($(control).size() && !$.swiper(control))) {
				return;
			}
			if (pagination != 'none') {
				a.append('<div class="swiper-pagination"></div>');
			}
			if (typeof(navigation) != 'undefined') {
				a.append('<div class="swiper-button-prev"></div>');
				a.append('<div class="swiper-button-next"></div>');
			}
			if (typeof(scrollbar) != 'undefined' && typeof(loop) == 'undefined') {
				a.append('<div class="swiper-scrollbar"></div>');
			}
			b = Math.random().toString().replace(/\./g, '');
			a.attr({'swiper': b});

			var breakpoints_ = {};
			for (var iii in breakpoints) {
				if (iii <= 0) {
					continue;
				}
				breakpoints_[iii] = {};
				var si0 = parseInt(breakpoints[iii][0]);
				var si1 = parseInt(breakpoints[iii][1]);
				if (si0) {
					breakpoints_[iii].slidesPerView = si0;
				}
				breakpoints_[iii].spaceBetween = si1 || 0;
			}

			var data = {
				// 如果需要分页器
				pagination: {
					el:'[swiper="' + b + '"] .swiper-pagination',
					clickable:true,
					dynamicBullets:pagination=='dynamic' ? true : false,
				},
				// 如果需要前进后退按钮
				navigation:{
					nextEl:'[swiper="' + b + '"] .swiper-button-next'+(a.attr('next')?','+a.attr('next'):''),
					prevEl:'[swiper="' + b + '"] .swiper-button-prev'+(a.attr('prev')?','+a.attr('prev'):''),
				},
				// 如果需要滚动条
				scrollbar:{
					el:'[swiper="' + b + '"] .swiper-scrollbar',
				},
				breakpoints:breakpoints_,
				initialSlide:index,
				roundLengths:true,
				slideToClickedSlide: typeof(click) != 'undefined',
				spaceBetween:parseInt(a.attr('space')) || 3,
				noSwipingSelector:a.attr('noSwipingSelector')||'',
				on:{
					init:function(){
						$.swiperModule(a, this);
						a.attr({'active-index':this.activeIndex});
						$.eval(a.attr('init'), a, this);
					},
					slideChangeTransitionStart:function(){
						// 整屏滚动效果
						if(typeof(mousewheel)!='undefined'){
							var t=this;
							var c=$(t.slides[t.activeIndex]);
							var d=c.find('.container[mousewheel]');
							if(d.size()){
								t.mousewheel.disable()
								var e=$.swiper(d);
								if(!c.hasClass('exec-mousewheel')) c.addClass('exec-mousewheel').mousewheel(function(go,xy,x,y){
									if(d.attr('is-mouserunning')=='1') return;
									if(parseInt(d.attr('activeIndex'))==e.activeIndex){
										go=1;
									}else{
										go=0;
									}
									if(go==1&&y<0&&e.activeIndex==e.slides.length-1){
										t.slideTo(t.activeIndex+1);
										d.attr('is-mouserunning',1);
									}
									if(go==1&&y>0&&e.activeIndex==0){
										t.slideTo(t.activeIndex-1);
										d.attr('is-mouserunning',1);
									}
									setTimeout(function(){d.attr({'is-mouserunning':0})},300);
								});
							}else{
								t.mousewheel.enable();
							}
						}

						// 回调函数
						$.eval(a.attr('start'), a, this);
					},
					slideChangeTransitionEnd:function(){
						$.swiperModule(a, this);
						a.attr({'active-index':this.activeIndex});
						$.eval(a.attr('end'), a, this);
					},
				},
				watchSlidesVisibility:true,
			}
			// 分页方式
			if(pagination!='dynamic' && pagination && typeof(pagination)!='undefined') data.pagination.type=pagination;
			// 切换方式
			if(typeof(effect)!='undefined') data.effect=effect;
			// 双击或者手机双指缩放图片
			if(typeof(zoom)!='undefined') data.zoom=true;
			// 鼠标滚动
			if(typeof(mousewheel)!='undefined') data.mousewheel=true;
			// 垂直滚动
			if(typeof(vertical)!='undefined') data.direction='vertical';
			// 循环模式选项
			if(typeof(loop)!='undefined') data.loop=true;
			// 展示数量
			if(typeof(view)!='undefined') data.slidesPerView=view;
			// 展示数量
			if(typeof(cols) != 'undefined') {
				data.slidesPerColumn = cols;
				data.slidesPerColumnFill = a.attr('colsfill') || 'row';
			}
			// 展示数量
			if(typeof(group)!='undefined') data.slidesPerGroup=group;
			// 自动高度
			if(typeof(autoHeight)!='undefined') data.autoHeight=true;
			// 居中
			if(typeof(center)!='undefined') data.centeredSlides=true;
			// 自由模式
			if(typeof(free)!='undefined') data.freeMode=true;
			// 视差效果
			if(typeof(parallax)!='undefined') data.parallax=true;
			// 整屏滚动效果
			if(a.find('img[data-src]').size()){
				a.find('img[data-src]').addClass('swiper-lazy');
				data.lazy=true;
			}
			// 整屏滚动效果
			if(typeof(data.direction)!='undefined' && typeof(data.autoHeight)!='undefined' && typeof(data.mousewheel)!='undefined'){
				var wh=$(window).height();
				console.log(w.children().size())
				w.children().each(function(){
					if(typeof($(this).attr('autoHeight'))=='undefined') $(this).height(wh);
				});
			}
			// 自动播放
			if(delay){
				data.autoplay = {
					delay: delay * 1000,
				    disableOnInteraction: true,
				    stopOnLastSlide: false,
				}
			}
			if($(thumbs).size()){
				data.thumbs={swiper:$.swiper(thumbs)}
			}

			// 手机版去掉禁止滑动的样式
			if($.mobile()){
				a.find('.swiper-no-swiping').addClass('swiper-no-swiping-').removeClass('swiper-no-swiping');
			}


			// 执行
			SwiperData[b]=new Swiper(a,data);

			// 鼠标移入停止自动播放
			if($(control).size()){
				SwiperData[b].controller.control=$.swiper(control);
		        $.swiper(control).controller.control=SwiperData[b];
			    $(control).hover(function(){
			    	$.swiper(a).autoplay.stop();
			    }, function() {
			    	if(delay) $.swiper(a).autoplay.start();
			    });
			    a.hover(function(){
			    	$.swiper(control).autoplay.stop();
			    },function(){
			    	if ($(control).attr('delay')) $.swiper(control).autoplay.start();
			    });
		    }
		    a.hover(function(){
		    	$.swiper(a).autoplay.stop();
		    },function(){
		    	if (delay) $.swiper(a).autoplay.start();
		    });
		});
	};
	JEXTFN['WEBSWIPER']();
});