﻿var CK_HOSTID = "comic_hostid";
var ic = {
	norefresh:false,
	img:DOM.E("ComicImage"),
	imgUrlBase:"",
	fitToScreen:function(scale){
		if(!this.img){
			return;
		}
		var w, ck;
		var sw = screen.availWidth - 35;
		if(scale && this.img.offsetWidth > sw){
			w = sw + "px";
		}else{
			w = "auto";
		}
		DOM.E("ComicImageScale_Ori").style.fontWeight = w == "auto"? "bold" : "";
		DOM.E("ComicImageScale_Fit").style.fontWeight = w == "auto"? "" : "bold";
		if(typeof scale == "boolean"){
			HTML._C("ComicImageScale", scale? "FitToScreen" : "", 60, "comicspot.net", "/");
		}
		this.img.style.width = w;
	},
	onclick_turnPage:function(){
		this.blur();
	},
	//添加JS翻页链接
	init_norefresh:function(){
		if(!this.norefresh){
			return;
		}
		this.ept.p1st.href = "javascript:ic.turnPage(1)";
		this.ept.plast.href = "javascript:ic.turnPage(PAGES)";
		this.ept.pprev.href = "javascript:ic.turnPage('prev')";
		this.ept.pnext.href = "javascript:ic.turnPage('next')";
		for(var i in this.ept){
			if(i.substring(0, 1) == "p"){
				this.ept[i].onclick = this.onclick_turnPage;
			}
		}
		var el = DOM.E("List"), e, p;
		for(var i in el.childNodes){
			e = el.childNodes[i];
			if(e.nodeType != 1){
				continue;
			}
			e.id = "PageTo" + parseInt(e.innerHTML, 10);
			p = e.href.lastIndexOf("/");
			p = parseInt(e.href.substring(p > 0? p + 2 : 1), 10);
			if(!isNaN(p)){
				e.href = "javascript:ic.turnPage(" + p + ")";
			}
		}
	},
	init:function(){
		this.ept = {p1st:DOM.E("LinkPage1st"), pprev:DOM.E("LinkPagePrev"),
			pnext:DOM.E("LinkPageNext"), plast:DOM.E("LinkPageLast"),
			cprev:DOM.E("LinkChapterPrev"), cnext:DOM.E("LinkChapterNext")};
		this.init_norefresh();
		var en = this.ept.pnext == null? this.ept.cnext : this.ept.pnext;
		if(en){
			this.img.setAttribute("nextPage", en.href);
		}
		//拖动图片移动窗口滚动条
		HTML.dragScroll(this.img, null, 2);
		this.img.alt += "\n鼠标右击图片可缩放图片";
		this.img.isMap = true;
		//右键缩放图片
		this.img.oncontextmenu = function(){
			if(this.offsetWidth == 0){
				return;
			}
			var widthori = parseInt(this.getAttribute("widthori"));
			if(isNaN(widthori)){
				widthori = this.offsetWidth;
				this.setAttribute("widthori", widthori);
			}
			this.style.width = (widthori * (this.offsetWidth == widthori? 2 : 1)) + "px";
			return false;
		}
		//提示文件未找到，添加回复链接
		this.img.onerror = function(){
			ic.img.src = "http://i.acspot.com/images/loadError.gif";
			var i = "ErrorReporter";
			var d = DOM.E(i);
			if(d){
				d.style.display = "block";
			}else{
				d = DOM._E("div");
				d.id = i;
				d.innerHTML = "或许是临时性网络问题，如页面刷新后仍然无法看到图，<a onclick='reportError()'>按此回报图片问题</a>";
				this.parentNode.insertBefore(d, this.nextSibling);
			}
		}
		//图片已载入出错的情况
		if(window.imageLoadFailed == true){
			this.img.onerror();
		}
		//双击图片翻页
		this.img.ondblclick = function(){
			if(ic.norefresh){
				ic.turnPage("next");
			}else{
				var np = this.getAttribute("nextPage");
				if(np){
					location.href = np;
				}
			}
			return false;
		}
		this.imgUrlBase = this.img.src.substring(0, this.img.src.lastIndexOf("/") + 1);
		this.pageUrlBase = location.href.substring(0, location.href.lastIndexOf("/") + 1);
		//添加图片控制链接
		var eic = DOM._E("div");
		eic.id = "ComicImageController";
		eic.innerHTML = "<a id='ComicImageScale_Ori' style='font-weight:bold' href='javascript:ic.fitToScreen(false)'>原始大小</a> | " +
			"<a id='ComicImageScale_Fit' href='javascript:ic.fitToScreen(true)'>适合屏幕</a> | " +
			"<a href='javascript:ic.share(0)'>分享</a> | <a href='javascript:ic.share(1)'>收藏</a>" +
			"<div id='ComicImageLoading'><img src='http://i.acspot.com/images/loading_wt.gif'/><div class='text'>正在载入图片，请稍候</div></div>";
		DOM.E("ComicOuter").insertBefore(eic, this.img);
		this.imgLoading = DOM.E("ComicImageLoading");
		this.img.onload = function(){
			var cis = HTML.C("ComicImageScale");
			if(cis == "FitToScreen"){
				ic.fitToScreen(cis);
			}
			ic.loadNextPage();
			ic.imgLoading.style.display = "none";
		}
	},
	getExt:function(page){
		var ext = EXT, p = EXT.indexOf(" ");
		if(ext.length > 0 && p > 0){
			var exIds = ext.substring(spacePos).split(" "), jpg = false;
			for(var i in exIds){
				if(exIds[i] == page){
					jpg = true;
					break;
				}
			}
			ext = jpg? null : ext.substring(0, p);
		}
		return ext? ext : "jpg";
	},
	loadNextPage:function(){
		if(PAGE == PAGES){
			return;
		}
		var ext = this.getExt(PAGE + 1);
		var src = this.img.src.substring(0, this.img.src.lastIndexOf("/") + 2) +
			Util.getZeroDigit(PAGE + 1, 3) + "." + (ext? ext : "jpg");
		var img = new Image();
		img.src = src;
	},
	share:function(a){
		var t = DOM.E("Title").innerHTML.replace(/<[bB]>/, "").replace(/<\/[bB]>/, "");
		var u = this.pageUrlBase + "p" + Util.getZeroDigit(PAGE, 3) + ".html";
		if(a == 0){
			var v = t + "\n" + u;
			if(HTML.setClipboard(v)){
				alert("漫画标题与地址已复制到系统剪贴板，按Ctrl+V粘贴到QQ、MSN等处");
			}else{
				prompt("按Ctrl+C复制以下漫画标题与地址，按Ctrl+V粘贴到QQ、MSN等处", v);
			}
		}else if(a == 1){
			HTML.setBookmark(t, u);
		}
	},
	turnPage:function(p){
		if(p == "prev"){
			p = PAGE - 1;
		}else if(p == "next"){
			p = PAGE + 1;
		}
		if(p > PAGES){
			location.href = this.ept.cnext? this.ept.cnext.href : "/viewed-" + BASEPATH;
			return;
		}else if(p <= 0){
			if(this.ept.cprev){
				location.href = this.ept.cprev.href;
			}else{
				alert("已是漫画的第一页");
			}
			return;
		}
		this.img.src = this.imgUrlBase + "p" + Util.getZeroDigit(p, 3) + "." + this.getExt(p);
		var rf = "第" + PAGE + "页", rt = "第" + p + "页";
		DOM.D.title = DOM.D.title.replace(rf, rt);
		var e = DOM.E("Title");
		e.innerHTML = e.innerHTML.replace(rf, rt);
		e = DOM.E("ErrorReporter");
		if(e){
			e.style.display = "none";
		}
		this.imgLoading.style.display = "block";
		this.img.alt = this.img.alt.replace(rf, rt);
		//移动纵向滚动条
		var pos = DOM.EPos("Title")[1];
		var s = DOM.bodyScroll();
		var t = s.top;
		if(t > pos + 100){
			window.scrollBy(- s.left, pos - t);
		}
		DOM.E("PageTo" + PAGE).className = "";
		DOM.E("PageTo" + p).className = "cur";
		PAGE = p;
	}
}
init();
function init(){
	if(top.location != location){
		top.location = location;
	}
	//最后浏览的书签
	new ComicBookmark(BASEPATH, PATH, PAGE, DOM.E("Title").innerHTML).set(null);
	if(PATH.length > 0 && PATH.charAt(PATH.length - 1) == "/"){
		PATH = PATH.substring(0, PATH.length - 1);
	}
	//添加漫画计数器
	var vc = HTML.C("comicIds").split(",");
	if(Util.find(vc, ID) === false){
		dataLoader.regist("comicCounter:basepath=" + BASEPATH);
		HTML._C("comicIds", vc + "," + ID, 60, "comicspot.net", "/");
	}
	dataLoader.addCounter("comicPV");
	//添加设置书签的链接
	var ebs = DOM.E("BookmarkSetter");
	ebs.innerHTML = "<a href=\"javascript:window.open('http://shuqian.qq.com/post?from=3&title='+encodeURIComponent(document.title)+'&uri='+encodeURIComponent(document.location.href)+'&jumpback=2&noui=1','favit','width=930,height=470,left=50,top=50,toolbar=no,menubar=no,location=no,scrollbars=yes,status=yes,resizable=yes');void(0)\"" +
" style=\"float:left;text-decoration:none;color:#155da5;display:block;background:url('http://shuqian.qq.com/img/addstyle2.gif') no-repeat 0px 0px;height:23px;width:90px;padding:3px 2px 0px 23px;font-size:12px;\">收藏到QQ书签</a>" +
"<a href=\"javascript:window.open('http://cang.baidu.com/do/add?it='+encodeURIComponent(document.title.substring(0,76))+'&iu='+encodeURIComponent(location.href)+'&fr=ien#nw=1','_blank','scrollbars=no,width=600,height=450,left=75,top=20,status=no,resizable=yes'); void 0\"" +
"style=\"color:white;text-decoration:none;font-size:12px;font-weight:normal\">" +
"<SPAN style=\"float:right;padding:5px 5px 0; FONT-SIZE: 12px; MARGIN-LEFT: 10px; CURSOR: pointer;\">" +
"<IMG alt=添加到百度搜藏 src=\"http://cang.baidu.com/-/remote/fav1.jpg\" align=absMiddle border=0>添加到百度搜藏</SPAN></a>";
	if(hostBase == "comicspot.net"){
		var v = "<a href=\"javascript:new ComicBookmark('" + BASEPATH + "','" + PATH + "'," + PAGE + ",'" + DOM.E("Title").innerHTML +
			"').set(";
		ebs.innerHTML += v + "1)\">设置本页为书签 1</a> | " + v + "2)\">设置本页为书签 2</a><br/>" +
			"<a href='javascript:comicStore.set(BASEPATH, PATH, PAGE)'>将本漫画添加至我的漫画书架</a><br/>添加至书架后，该漫画更新时将会给您发送邮件通知";
	}
	ic.init();
}
function reportError(){
	var desc = window.prompt("您可以填写描述信息，按“确定”发送回报，按“取消”取消此次回报", "");
	if(desc != null){
		var url = "/script/action/comic/reportError.php?basepath=" + BASEPATH +
			"&path=" + PATH + "&page=" + PAGE + "&desc=" + encodeURIComponent(desc);
		XML.load(url, {onload:function(d){
			switch(d){
			case "param error": d = "未指定漫画路径或页数"; break;
			case "ok": d = "已保存回报数据，感谢您的支持，修复情况会在论坛的“网站消息”版发布，敬请关注"; break;
			}
			alert(d);},
			plain:true});
	}
}
