/*判断是否移动端*/
function isMobileDevice() {
	return /Mobi|Android/i.test(navigator.userAgent);
}
/*判断Android|iOS|Desktop*/
function detectMobile() {
	var userAgent = navigator.userAgent || navigator.vendor || window.opera;
	// Check for mobile devices
	if (/android/i.test(userAgent)) {
		return 'Android';
	}
	if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
		return 'iOS';
	}
	return 'Desktop';
}
/*文章展开隐藏*/
function expandText(selector, maxLength) {
	var text = $(selector).text(); // 获取文本内容
	if (text.length > maxLength) {
		var shortText = text.substr(0, maxLength); // 截取前10个字符
		var longText = text.substr(maxLength); // 截取剩余的文字
		$(selector).html(shortText + '<span class="none">' + longText + '</span>' +
			'<a href="#" class="toggle-content">...展开</a>');
		$('.toggle-content').click(function(e) {
			e.preventDefault();
			$(this).prev('.none').toggle(); // 切换隐藏/显示状态
			if ($(this).text() === '...展开') {
				$(this).text('收起');
			} else {
				$(this).text('...展开');
			}
		});
	}
}
/*滚动到点击可视区*/
function handleScrollPosition(containerSelector, direction) {
	// 获取之前记录的滚动位置（如果有）
	var scrollPos = localStorage.getItem('scrollPosition_' + direction);
	// 如果有记录的滚动位置，则滚动到该位置
	if (scrollPos !== null) {
		if (direction === 'x') {
			$(containerSelector).scrollLeft(scrollPos);
		} else if (direction === 'y') {
			$(containerSelector).scrollTop(scrollPos);
		}
	}
	// 监听滚动事件，并保存滚动位置到本地存储
	$(containerSelector).on('scroll', function() {
		var currentScroll = $(this)[direction === 'x' ? 'scrollLeft' : 'scrollTop']();
		localStorage.setItem('scrollPosition_' + direction, currentScroll);
	});
}

/*-----------------------播放记录------------------------------*/
function saveToStorage(vodId, vodName, vodepisode, siteUrl) {
	var expirationTime = 24 * 60 * 60 * 1000; // 一天的毫秒数
	var currentTime = new Date().getTime();
	// 检查本地存储中是否已有记录
	var existingRecords = localStorage.getItem('vodRecords');
	existingRecords = existingRecords ? JSON.parse(existingRecords) : [];
	// 查找是否存在相同 vodId 的记录
	var foundIndex = existingRecords.findIndex(function(record) {
		return record.vodId === vodId;
	});
	// 如果找到相同 vodId 的记录，则更新其 vodepisode 和 timestamp 并移到最前面
	if (foundIndex !== -1) {
		existingRecords[foundIndex].vodepisode = vodepisode;
		existingRecords[foundIndex].siteUrl = siteUrl;
		existingRecords[foundIndex].timestamp = currentTime;
		var foundRecord = existingRecords.splice(foundIndex, 1)[0]; // 移除原有记录
		existingRecords.unshift(foundRecord); // 将更新后的记录放到数组最前面
	} else {
		// 否则，创建一个新记录并添加到数组中
		var newRecord = {
			vodId: vodId,
			vodName: vodName,
			vodepisode: vodepisode,
			siteUrl: siteUrl,
			timestamp: currentTime
		};
		existingRecords.unshift(newRecord); // 将新记录添加到数组最前面
		// 限制存储最多6条记录
		if (existingRecords.length > 5) {
			existingRecords = existingRecords.slice(0, 5); // 截取前6条记录
		}
	}
	// 过滤掉过期记录
	existingRecords = existingRecords.filter(function(record) {
		return (currentTime - record.timestamp) < expirationTime;
	});
	// 保存更新后的记录到本地存储
	localStorage.setItem('vodRecords', JSON.stringify(existingRecords));
}
// 获取存储记录函数
function getStoredRecords() {
	return JSON.parse(localStorage.getItem('vodRecords'));
}
// 清除存储记录函数
function clearStorage() {
	localStorage.removeItem('vodRecords');
}
// 准备将记录添加到 .watch-history ul，新的记录在前
function appendRecordsToUl() {
	var records = getStoredRecords();
	var ulElement = $('.watch-history ul');
	// 清空 ul 元素内容
	ulElement.empty();
	// 遍历记录并将其追加到 ul 元素中
	if (records && records.length > 0) {
		records.forEach(function(record) {
			var listItem = '<li><a href="' + record.siteUrl + '">' +
				'<span class="tit nowrap">' + record.vodName + '</span>' +
				'<span class="episode">' + record.vodepisode + '</span>' +
				'</a></li>';
			ulElement.append(listItem);
		});
	} else {
		ulElement.append(
			'<li style="text-align: center;color:var(--zy-gray);margin-top: 14px;"><i class="iconfont icon-kongkongruye" style="font-size: 70px"></i></li><li style="text-align: center;margin: 14px 0;color:var(--zy-gray);">无播放记录</li>'
			);
	}
}
/*-----------------------播放记录 end---------------------------*/
$(function() {
	/*懒加载*/
	$("img.lazyload").lazyload();
	/*菜单*/
	$(".menu").hover(function() {
		$(this).find(".menu-ul").css("display", "flex").css("flex-flow", "wrap").show();
	}, function() {
		$(this).find(".menu-ul").hide();
	});

	// 点击切换主题风格
	$('.changetheme').hover(function() {
		$('.zy-style').toggleClass('expand');
	});
	// 在页面加载时检查并应用保存的主题样式
	var savedTheme = localStorage.getItem('theme_style');
	if (savedTheme !== null) {
		$('body').addClass("style-for-" + savedTheme);
	}
	$(".zy-style a").click(function() {
		var styleName = $(this).attr("style-text");
		var expirationDays = 1; // Cookie 有效期为一天

		// 移除之前保存的主题样式
		var styleClasses = $('body').attr('class').split(' ');
		var classesToKeep = styleClasses.filter(className => !className.startsWith('style-for-'));
		$('body').removeClass().addClass(classesToKeep.join(' '));

		// 添加新的主题样式并保存到 localStorage
		$('body').addClass("style-for-" + styleName);
		localStorage.setItem('theme_style', styleName);
	});

	/*分享*/
	var clipboard = new ClipboardJS('.share');
	$(".share").click(function() {
		$.toast({
			heading: '复制成功，快去分享吧',
			position: 'mid-center',
			icon: 'success',
			loader: true,
			loaderBg: '#439b6b',
			stack: 1,
			text: $(this).attr('data-clipboard-text'),
		})
	});

	var swiper = new Swiper("#mjio-swiper", {
		slidesPerView: 3,
		spaceBetween: 10,
		freeMode: true,
		autoplay: true,
		loop: true,
		breakpoints: {
			767: {
				slidesPerView: 6,
				spaceBetween: 15,
			}
		}
	});
	$(".mj-tab .tabs a").on('click', function(e) {
		e.preventDefault();
		var index = $(this).index();
		var container = $(this).closest('.mj-tab');
		container.find(".a-con").removeClass('active').hide();
		container.find(".a-con").eq(index).addClass('active').css('display', 'grid');
		$(this).addClass('active').siblings().removeClass('active');
	});

	$(function() {
		expandText('#ysinfotext', 100);
		/*线路*/
		$(".switch").hover(function() {
			$(".mxianlu").css("display", "grid").show();
		}, function() {
			$(".mxianlu").hide();
		});
		$(".tips-close-btn").click(function() {
			$(this).hide();
			$(".tips-box").hide();
		});
	});
	var swiper1 = new Swiper(".txlswiper", {
		slidesPerView: 3,
		spaceBetween: 10,
		freeMode: true,
		autoplay: true,
		breakpoints: {
			767: {
				slidesPerView: 6,
				spaceBetween: 15,
			}
		}
	});

	var swiper2 = new Swiper(".ggswiper", {
		direction: 'vertical',
		height: 30,
		loop: true,
		autoplay: {
			delay: 3000,
			disableOnInteraction: false,
		}
	})


	if ($("#player-left").length) {
		let PlayerLeft = $("#player-left");
		let PlayerSide = $("#player-sidebar");
		let Position = $(".jisu a.active").position();
		let offsetTop = 70;
		let offsetLeft = 10;
		PlayerSide.scrollTop(Position.top - offsetTop);
		PlayerSide.scrollLeft(Position.left - offsetLeft);
	}


	$(function() {
		// 获取所有的 .screen-tog 元素
		$('.screen-tog').each(function() {
			var parentDD = $(this).siblings('dd');
			var scrollHeight = parentDD.prop('scrollHeight');
			var clientHeight = parentDD.prop('clientHeight');
			if (scrollHeight > clientHeight) {
				$(this).show();
			} else {
				$(this).hide();
			}
		});
		$('.screen-tog').on('click', function(e) {
			e.preventDefault();
			var parentDD = $(this).siblings('dd');
			parentDD.toggleClass('clamp3');
			if (parentDD.hasClass('clamp3')) {
				$(this).text('收起');
			} else {
				$(this).text('展开');
			}
		});


		if (isMobileDevice()) {
			var categoryId = '{$obj.type_id}';
			// 点击链接时保存每行的水平滚动位置和当前分类 ID 到本地存储
			$('.screen-{$obj.type_id} dl').on('click', 'a', function(e) {
				var $scrollableItems = $('dd');
				$scrollableItems.each(function(index) {
					var scrollLeft = $(this).scrollLeft();
					localStorage.setItem('scrollPosition_' + index, scrollLeft);
					localStorage.setItem('categoryId', categoryId); // 存储当前分类的 ID
				});
			});
			// 页面加载时根据本地存储的水平滚动位置和分类 ID 滚动到相应位置
			var storedCategoryId = localStorage.getItem('categoryId');
			if (storedCategoryId !== null && storedCategoryId === categoryId) {
				var $scrollableItems = $('dd');
				$scrollableItems.each(function(index) {
					var savedScroll = localStorage.getItem('scrollPosition_' + index);
					if (savedScroll !== null) {
						$(this).scrollLeft(savedScroll);
					}
				});
			}
			// 点击分类链接时清除所有分类的存储和左侧滚动位置
			$('.category-nav a').click(function() {
				for (var i = 0; i < localStorage.length; i++) {
					var key = localStorage.key(i);
					if (key.includes('scrollPosition_') || key === 'categoryId') {
						localStorage.removeItem(key);
					}
				}
			});
		}
	});


	$(".nav a").each(function() {
		if (this.href == document.location.toString().split("#")[0]) {
			$(this).addClass("active");
			return false;
		}
	});
	/*播放记录*/
	appendRecordsToUl();
});