﻿var needReInit = true;
function readCookie(name) {
	var cookieValue = "";
	var search = name + "=";
	if(document.cookie.length > 0) 	{ 
		offset = document.cookie.indexOf(search);
		if (offset != -1) { 
offset += search.length;
end = document.cookie.indexOf(";", offset);
if (end == -1) end = document.cookie.length;
cookieValue = unescape(document.cookie.substring(offset, end))
		}
	}
	return cookieValue;
}
// Example:
// writeCookie("myCookie", "my name", 24);
// Stores the string "my name" in the cookie "myCookie" which expires after 24 hours.
function writeCookie(name, value, hours) {
	var expire = "";
	if(hours != null) {
		expire = new Date((new Date()).getTime() + hours * 3600000);
		expire = "; expires=" + expire.toGMTString() + "; path=/";
	}
	document.cookie = name + "=" + escape(value) + expire;
}
function initBookMark() {
	
	intpart = parseInt(bookMark);
	firstdotpart = bookMark.substr(2,1) ? bookMark.substr(2,1) : '0';
	if (intpart == 10) {
		firstdotpart = '';
	} else {
		firstdotpart = '.'+firstdotpart;
	}
	bookMarkHTML = '<strong class="gradNum" id="showMyMark">'+intpart+'<span>'+firstdotpart+'</span></strong>';
	bookMarkHTML += '<div class="gradStar">';		
	for (i=1; i<=10; i++) {
		bookMarkHTML += ('<img id="imgMark'+i+'" src="'+ Webimgurl +'Icon/star/'+(i<=parseInt(bookMark)?'1':'3')+'.gif" '+(myMark?'':'onmouseover="drawcurrentmark('+i+')" onmouseout="outimgmark()" onclick="postMyMark('+i+');" style="cursor:hand"')+' alt="" />');		
	}		
	if (myMark) {
		//animeMarkHTML += '<p>您的评分为<span>'+myMark+'</span>分 <a href="javascript:void(null)" onclick="deleteMyMark()">[删除]</a></p>';
		bookMarkHTML += '<p id="tempHintText">您的评分为<span style="color:#EA8758;font-weight:bold"> '+myMark+' </span>分</p>';
	} else {
		bookMarkHTML += '<p id="tempHintText">移动星星可以评分</p>';
	}
	bookMarkHTML += '</div>';
	
	$("#bookMark").html(bookMarkHTML);
}
function outimgmark() {
	needReInit = true;
	setTimeout('checkIfReInit()', 500);
}
// 滑动效果打分
function drawcurrentmark(mark) {
	needReInit = false;;
	for (i=1; i<=10; i++){
		if (i<=mark){
document.getElementById('imgMark'+i).src=Webimgurl+'Icon/star/1.gif';
		} else {
document.getElementById('imgMark'+i).src=Webimgurl+'Icon/star/3.gif';
		}
	}
	if (mark>0 && mark<=3) {
		$("#tempHintText").html('很差的作品，不推荐观看');
	} else if (mark>=4 && mark<=6) {
		$("#tempHintText").html('普普通通的作品');
	} else if (mark>=7 && mark<=8) {
		$("#tempHintText").html('还不错的作品，推荐一看');
	} else if (mark>=9 && mark<=10) {
		$("#tempHintText").html('经典中的经典，不可错过');
	}
	mark = mark + '';
	myintpart = parseInt(mark);
	myfirstdotpart = mark.substr(2,1) ? mark.substr(2,1) : '0';
	if (myintpart == 10) {
		myfirstdotpart = '';
	} else {
		myfirstdotpart = '.'+myfirstdotpart;
	}
	$("#showMyMark").html(myintpart+'<span>'+myfirstdotpart+'</span>');
}
function checkIfReInit() {
	if (needReInit) {
		initBookMark();
	}
}




function postMyMark(mark) {
//if(!login) {
//goLogin();
//return;
//}
writeCookie('acgoocomic'+comicid,mark,24);
myMark = mark;
bookMarkNumber++;
bookMarkTotal += myMark;
bookMark = bookMarkNumber?String(bookMarkTotal/bookMarkNumber):'0.0';
$.ajax({
url : "/proxy/ajaxProxyComic/Mark?"+Math.random(),
type : "post",
dataType : "html",
data : "myMark="+mark+"&type=postMyMark&bookMarkString="+bookMarkString,
contentType: "application/x-www-form-urlencoded; charset=utf-8",
error : function() {
	writeCookie('acgoocomic'+comicid,null,0);
	jAlert('未知错误，请稍后再试！');
}, 
success : function(xml) {
	initBookMark();
}
});
}
function tabOnOff(curtab, no) {
if(curtab == '') {
	curtab = document.getElementById('tab'+no);
	location.href = '#detailTab';
}
for(i=1; i<=3 ; i++){
	taborder = "tab" + i;
	tab = document.getElementById(taborder);
	tab.src = tab.src.replace('on','');
}
curtemp = curtab.src.split('.gif');
curtab.src = curtemp[0].concat('on.gif');

switch(no) {
	case '2' :	// 
		load_comment(1, "comment_div2");
		$("#comment_div").css('display', 'none');
		$("#comment_div2").css('display', 'block');
		break;
	case '3' :	// 
		loadNote(1, "comment_div2");
		$("#comment_div").css('display', 'none');
		break;
	case '1' :	// 
		$("#comment_div2").css('display', 'none');
		$("#comment_div").css('display', 'block');
		load_comment(1);
		break;
}             
}

function iTabOnOff(curtab, no) {
if(curtab == '') {   
curtab = document.getElementById('tab'+no);
location.href = '#detailTab';
}

for(i=1; i<=3 ; i++){
taborder = "tab" + i;
tab = document.getElementById(taborder);
tab.src = tab.src.replace('on','');
}
curtemp = curtab.src.split('.gif');  
curtab.src = curtemp[0].concat('on.gif');

switch(no) {
case '1' :
	$("#comment_div2").css('display', 'none');
	$("#comment_div").css('display', 'block');
	load_comment(1);
	break;
case '2' :
	load_comment(1, "comment_div2");
	$("#comment_div").css('display', 'none');
	$("#comment_div2").css('display', 'block');
	break;
case '3' :
	loadNote(1, "comment_div2");
	$("#comment_div").css('display', 'none');
	break;
}
}






function load_comment(page, id) {
	if( id == undefined ) {
		id = "comment_div";
	}
	$.ajax( {   
		url : "/title/Comment/list?dbay="+ comicid +":" + page+ ":"+Math.random(),
		type : "Get",
		dataType : "xml",
		success : function(xml) {
			board_load(xml, id);
		}
	});
	window.setTimeout("save_state('comment_div')", 1000);
}

function insert_cmt(item_seq, cmt) {
	var rtnUrl = escape(location.href);
    
	$.ajax({
 		url : '/title/Comment/insert?grid=dbay:comic:title:'+ comicid,
		type : "post",
		dataType : "xml", 
		data : "item_seq=" + item_seq + "&comment=" + encodeURIComponent(cmt) + "&rtnUrl=" + rtnUrl,
		contentType: "application/x-www-form-urlencoded; charset=utf-8",
		success : function(xml) {
			var code = $(xml).find("ReturnCode").text();
			codeMessage(code);
			tabOnOff(document.getElementById('tab2'),'2');
		}
	});
}



function loadNote(page, id) {
	if( id == undefined ) {
		id = "comment_div";
	}
	var note_div = document.getElementById("comment_div");
	note_div.innerHTML = ajaxLoaderImage;

	$.ajax( {
		url : "/title/note/list?dbay="+ comicid +":" + page+ ":"+Math.random(),
		type : "GET",
		dataType : "xml",
		error : function() {
			jAlert('error');
		},  
		success : function(xml) {
			note_load(xml, id);
		}

	});
}



function insertNote(cmt) {
	var rtnUrl = escape(location.href);

	if(!login) {
		goLogin();
		return;
	}

	if(cmt == '') {
		jAlert("请输入您认为经典的场面或对白");
		return;
	}
	
	$.ajax({
		url : "/title/note/insert?dbay=dbay:comic:title:"+ comicid +":dbay="+Math.random(),
		type : "post",
		dataType : "xml", 
		data : "comment=" + encodeURIComponent(cmt),
		contentType: "application/x-www-form-urlencoded; charset=utf-8",
		error : function() {
			jAlert('error');
		},   
		success : function(xml) {
			var code = $(xml).find("ReturnCode").text();
			codeMessage(code);
			tabOnOff(document.getElementById('tab3'),'3');
		}  
	});
}

function recommendNote(seq, isWriter) {
	if(!login) {
		goLogin();
		return;
	}

	if(isWriter == 'T') {
		jAlert("您不能推荐自己发布的信息");
		return;
	}

	$.ajax({
		url : "/title/note/recommend?dbay=dbay:comic:title:"+ comicid +":dbay="+Math.random(),
		type : "post",
		dataType : "xml",
		data : "seq=" + encodeURIComponent(seq),
		contentType: "application/x-www-form-urlencoded; charset=utf-8",
		error : function() {
			jAlert('error');
		}, 
		success : function(xml) {
			var code = $(xml).find("ReturnCode").text();
				codeMessage(code);
			    tabOnOff(document.getElementById('tab3'),'3');
		}
	});
}
function load_Chapter(page) {
	$.ajax( {   
		url : "/title/Chapter/list?page=" + page + ":"+Math.random(),
		type : "POST",
		dataType : "html",
		data :"s="+iChapter,
				error : function() {
		}, 
		success : function(xml) {
			$("#Chapter_div").html(xml);
		}
	});
}
function deleteNote(seq) { 
    $.ajax({
		url : "/title/note/delete?dbay=acgoo:comic:"+ comicid +":"+Math.random(),
		type : "post",
		dataType : "xml",
		data : "seq=" + encodeURIComponent(seq),
		success : function(xml) {
				var code = $(xml).find("ReturnCode").text();
				codeMessage(code);
				tabOnOff(document.getElementById('tab3'),'3');
			}  
	});
}