(function () {
	
	
	var isbn = '9787100105811', minPageNum = 1, maxPageNum = 2112, numMultiPages = 10000; // numMultiPages: 长按点击一次最多可能添加的页数
	var selImg = 'div#book_isbn_9787100105811 img', selRoot = 'div#book_isbn_9787100105811';
	

	var userAgent, mobileUserAgent;
	if (navigator.userAgent.match(/(Android)|(Adr)/i)) {
		userAgent = 'Android';
		if (document.querySelector('div.bd_body')) {
			mobileUserAgent = 'BlueDict';
		}
	} else if (navigator.userAgent.match(/goldendict/i)) {
		userAgent = 'GoldenDict';
	} else {
		userAgent = 'Eudic';
	}
	if (userAgent == 'GoldenDict') {
		var root = document.querySelector(selRoot);
		var divSeparator = root.parentNode.parentNode;
		if (divSeparator) {
			divSeparator.previousElementSibling.style.height = "10px"; // 避免顶部在GoldenDict中折行的问题
		}
	}

	var numPagesToBeAdded; // 长按点击一次实际应添加的页数。当长按点击最前或最后页面时，numPagesToBeAdded取决于当前点击页码和minPageNum/maxPageNum, numMultiPages;	当长按点击页面不是最前或最后页面时，numPagesToBeAdded取决于当前点击页码和其前/后页的页码以及numMultiPages
	var siblingImg; // 在页面上按下鼠标时，执行GetSiblingImg()函数返回其前后相邻页面siblingImg供跳转，若无siblingImg或虽有siblingImg但页面不连续需要追加页面则siblingImg为null
	var arrImgs = document.querySelectorAll(selImg);
	for (var i=0; i<arrImgs.length; i++) {
		var imgSrc = arrImgs[i].getAttribute('src');
		var imgSuffix = imgSrc.substring(imgSrc.length - 4);
		// 原版mdx中无id和max-width属性，需添加。有的词头(如let)内容所在页面超过一页，MDX中该词头出现多次，每次仅显示一个页面。这种情况需避免重复执行JS多次导致重复添加
		if (!arrImgs[i].getAttribute('id')) {			
			var pageNum = imgSrc.substring(imgSrc.length - 8, imgSrc.length - 4);			
			arrImgs[i].setAttribute('id', 'page_' + isbn + '_' + pageNum);
		}		
		// 有的词头(如let)内容所在页面超过一页，MDX中该词头出现多次，每次仅显示一个页面。这种情况需避免重复执行JS多次导致重复绑定事件响应函数
		if (!arrImgs[i].eventListenerAdded) {
			arrImgs[i].addEventListener('mousedown', RespondToMouseDown);
			arrImgs[i].addEventListener('click', RespondToClick);
			arrImgs[i].eventListenerAdded = true;
		}
	}
	
	// 在页面上按下鼠标时，执行GetSiblingImg()函数返回其前后相邻页面siblingImg供跳转，若无siblingImg或虽有siblingImg但页面不连续需要追加页面则siblingImg为null
	// 需要追加页面的充分必要条件是siblingImg == null && numPagesToBeAdded > 0
	function GetSiblingImg (e) {
		var arrImgs = document.querySelectorAll(selImg);
		for (var i=0; i<arrImgs.length; i++) {
			if (e.target == arrImgs[i]) {
				var imgSrc = e.target.getAttribute('src');
				var pageNumClicked = parseInt(imgSrc.substring(imgSrc.length - 8, imgSrc.length - 4));					
				var left = GetClickOffsetX(e) < 0.5 * GetViewableWidth();
				if (left && i == 0) {
					numPagesToBeAdded = Math.min(numMultiPages, pageNumClicked - minPageNum);
					return null;
				} else if (!left && i == arrImgs.length - 1) {
					numPagesToBeAdded = Math.min(numMultiPages, maxPageNum - pageNumClicked);
					return null;
				}				
				if (left) {
					var siblingIdx = i - 1;
				} else {
					var siblingIdx = i + 1;
				}
				imgSrc = arrImgs[siblingIdx].getAttribute('src');
				var pageNumSibling = parseInt(imgSrc.substring(imgSrc.length - 8, imgSrc.length - 4));
				numPagesToBeAdded = Math.min(numMultiPages, Math.round(Math.abs(pageNumSibling - pageNumClicked)) - 1);
				if (numPagesToBeAdded > 0) {
					return null;
				} else { // 如果相邻两页页码相同也会执行此语句，此时numPagesToBeAdded==-1无意义但不会被后续程序使用
					return arrImgs[siblingIdx];					
				}
			}
		}
	}	
	
	function RespondToMouseDown (e) {
		if (e.button != 0) {
			return;
		}
		flagMultiImgs = false;
		siblingImg = GetSiblingImg(e); // 为避免之后执行RespondToClick()函数时GetSiblingImg()函数被再次调用，引入全局变量siblingImg保存上次GetSiblingImg()函数被调用后的结果
		if (!siblingImg && numPagesToBeAdded > 0) {			
			timerAddMultipleImgs = setTimeout(AddMultipleImgs(e.target, GetClickOffsetX(e) < 0.5 * GetViewableWidth()), 1000);	
		}
	}

	var timerAddMultipleImgs, flagMultiImgs;
	
	function AddMultipleImgs (imageClicked, left) {
		return function() {
			flagMultiImgs = true;
			AddImgs (imageClicked, left, numPagesToBeAdded);
		}
	}
	
	function AddImgs (imageClicked, left, numPagesToAdd) { // 长按点击一次添加numPagesToAdd页数
		var imgSrc = imageClicked.getAttribute('src');
		var pageNumToAdd = parseInt(imgSrc.substring(imgSrc.length - 8, imgSrc.length - 4));
		var imgClicked = imageClicked;
		var numPagesAdded = 0;
		while (true) {
			if (left) {
				pageNumToAdd--;
			} else {
				pageNumToAdd++;		
			}
			if (++numPagesAdded > numPagesToAdd) {
				break;
			}				
			var newImg = GetAnImg(pageNumToAdd, imgClicked);
			if (left) {
				imgClicked.parentNode.insertBefore(newImg, imgClicked);
			} else {
				imgClicked.parentNode.appendChild(newImg);	
			}				
			imgClicked = newImg;				
		}		
	}
	
	function RespondToClick (e) {	
		if (flagMultiImgs || e.button != 0) {
			return;
		}
		if (!siblingImg) {
			if (numPagesToBeAdded > 0) {
				clearTimeout(timerAddMultipleImgs);
				var left = GetClickOffsetX(e) < 0.5 * GetViewableWidth();			
				AddImgs(e.target, left, 1);
			}
		} else {
			if (getComputedStyle(siblingImg).display == 'none') {
				siblingImg.style.display = 'block';
			}
			if (userAgent == 'GoldenDict') {
				siblingImg.scrollIntoView();
			} else {
				window.location.assign('#' + siblingImg.getAttribute('id'));
			}		
		}
	}

	function GetAnImg (pageNum, imgClicked) {
		var src = imgClicked.getAttribute('src');
		var paddedPageNum = '000' + pageNum;
		paddedPageNum = paddedPageNum.substring(paddedPageNum.length - 4);			
		var img = document.createElement('img');
		img.setAttribute('src', src.substring(0, src.length - 8) + paddedPageNum + imgSuffix);
		img.setAttribute('id', 'page_' + isbn + '_' + paddedPageNum);		
		img.addEventListener('mousedown', RespondToMouseDown);
		img.addEventListener('click', RespondToClick);
		return img;
	}
	
	function GetViewableWidth () {
			if (userAgent == 'Android') {
				return window.screen.width;
			} else if (userAgent == 'GoldenDict') {
				return document.body.clientWidth;
			} else if (userAgent == 'Eudic') {
				return document.body.clientWidth;
			} else {
				return window.screen.width;
			}			
	}

	function GetClickOffsetX (e) {
		if (userAgent == 'Android') {
			return e.screenX;
		} else if (userAgent == 'GoldenDict') {
			return e.clientX;
		} else if (userAgent == 'Eudic') {
			return e.clientX;
		} else {
			return e.screenX;
		}			
	}	
})();