﻿//下载电子书开始
function download(bid,vid,uid) {
	if(vid!=null&&vid>0) bid=bid+"_"+vid;
	if(uid==null||uid==0) uid=17;
	window.open("http://www.17k.com/onebookdownload.do?method=download&BOOKID="+bid+"&UID="+uid);
}
//下载电子书结束

var isGetBookScore=false;
var isGetBookInfo=false;
var isDing=false;
var isPingFen=false;
var isDinged=new Array();
var isPingFened=new Array();

//取得书积分信息开始
function getBookScore(bookid,bookvid) {
 var url="/main/bookScore/bookScore.do?method=getBookScore&bid=" + bookid +"&vid=" +bookvid;
 if (isGetBookScore) return;
 isGetBookScore=true; 
 $("#average").html("0");
 $("#ballot").html("0");
 $.get(url,
   function(data){    
	var giveFiveCnt=0;
	var giveFourCnt=0;
	var giveThreeCnt=0;
	var giveTwoCnt=0;
	var giveOneCnt=0;
	var totalDingCnt=0;	
	var cntFen=0;
	var sumFen=0;
	var vagFen=0;	
	eval(data);
	isGetBookScore=false;
	if(retValue) {
		cntFen=giveFiveCnt*5+giveFourCnt*4+giveThreeCnt*3+giveTwoCnt*2+giveOneCnt*1;
		sumFen=giveFiveCnt+giveFourCnt+giveThreeCnt+giveTwoCnt+giveOneCnt;
		avgFen=sumFen==0?0:Math.round(cntFen/sumFen);
		$("#average").css({backgroundImage:"url(http://img.17k.com/images/bookdown/star"+avgFen+".gif)", backgroundRepeat:"no-repeat"});
		$("#average").html("("+sumFen+")");
		$("#ballot").html(""+totalDingCnt+"");
	}
   } 
 )
}
//取得书积分信息结束


//取得书基础信息开始
function getBookInfo(bookid) {
 var bookjs="/html/books/"+Math.floor(bookid/100/100/100)+"/"+Math.floor(bookid/100/100)+"/"+Math.floor(bookid/100)+"/"+bookid+"/b_info_"+bookid+".js";
 if (isGetBookInfo) return;
 isGetBookInfo=true; 
 $("#bookname").html("载入中...");
 $("#author").html("载入中...");
 $("#synopsis").html("载入中...");
 $.get(bookjs,
   function(data){
	var bookName="";
	var authorName="";
	var authorNameEncode="";
	var introduction="";
	eval(data);
	isGetBookInfo=false; 
    $("#bookname").html(bookName);
    $("#author").html(authorName.substring(0,8)+" <a href=\"/main/author.do?method=getAuthInfo&penName="+authorNameEncode+"\" target=\"_blank\">[作者专栏]</a>");
    $("#synopsis").html(introduction.substring(0,40)+"...");
   } 
 )
}
//取得书基础信息结束

//评分开始
function rateHover(value){
 $("#gradeBT").toggleClass("star"+value);
 if(value==1){
  $("#gradetxt").html("很不喜欢");
 }else if(value==2){
  $("#gradetxt").html("不喜欢");
 }else if(value==3){
  $("#gradetxt").html("还行");
 }else if(value==4){
  $("#gradetxt").html("喜欢");
 }else if(value==5){
  $("#gradetxt").html("非常喜欢");
 }
}

function rateOut(){
 $("#gradeBT").removeClass();
 $("#gradetxt").html("还行");
}

function do_pingfen(score){
 var url="/main/bookScore/bookScore.do?method=do_pingfen&score=" + score + "&bid=" + $("#bookbox").attr("bookid") +"&vid=" + $("#bookbox").attr("bookvid");
 if(isPingFen) return;
 for(i=0;i<isPingFened.length;i++){
  if(isPingFened[i]==$("#bookbox").attr("bookid")) return;
 }
 isPingFen=true; 
 $.get(url,
   function(data){
	var retValue=false;  
	var giveFiveCnt=0;
	var giveFourCnt=0;
	var giveThreeCnt=0;
	var giveTwoCnt=0;
	var giveOneCnt=0;
	var cntFen=0;
	var sumFen=0;
	var vagFen=0;
	eval(data);
	isPingFen=false;
	if(retValue) {
		cntFen=giveFiveCnt*5+giveFourCnt*4+giveThreeCnt*3+giveTwoCnt*2+giveOneCnt*1;
		sumFen=giveFiveCnt+giveFourCnt+giveThreeCnt+giveTwoCnt+giveOneCnt;
		avgFen=sumFen==0?0:Math.round(cntFen/sumFen);
		$("#average").css({backgroundImage:"url(http://img.17k.com/images/bookdown/star"+avgFen+".gif)", backgroundRepeat:"no-repeat"});
		$("#average").html("("+sumFen+")");
		isPingFened.push($("#bookbox").attr("bookid"));
	}
   } 
 )
}
//评分操作结束


//顶操作开始
function do_ding(){
 var url="/main/bookScore/bookScore.do?method=do_ding&bid=" + $("#bookbox").attr("bookid") +"&vid=" + $("#bookbox").attr("bookvid");
 if (isDing) return;
 for(i=0;i<isDinged.length;i++){
  if(isDinged[i]==$("#bookbox").attr("bookid")) return;
 }
 isDing=true; 
 $.get(url,
   function(data){    
	var retValue=false;
	var totalDingCnt=0;
	eval(data);
	isDing=false;
	if(retValue) {
    	$("#ballot").html(totalDingCnt);
		isDinged.push($("#bookbox").attr("bookid"));
	}
   } 
 )
}
//顶操作结束

//浮动层开始
//解决IE下offset相对上层，FF下offset相对body
function posxy(d){
 var e=[0,0];
 el=d;
 while(el){
  e[0]=e[0]+el.offsetLeft;
  e[1]=e[1]+el.offsetTop;
  el=el.offsetParent;
 }
 return e;
}

function showbookbox(e,bookid,bookvid){
 var mouseX=posxy(e)[0]+10;
 var mouseY=posxy(e)[1]+50;
 getBookScore(bookid,bookvid);
 getBookInfo(bookid,bookvid);
 $("#bt1 a:first").each(function(){this.href="/main/bookCommentFront.do?method=frontSearch&bookId="+bookid+"&type=3"});
 $("#bt2 a:first").each(function(){this.href="javascript:download("+bookid+","+bookvid+",17)"});
 $("#bt2 a:last").each(function(){this.href="/main/ebook/bookDetail.do?method=getEbookDetail&bid="+bookid+"&vid="+bookvid});
 $("#bookbox").show();
 $("#bookbox").css({top:mouseY-5+"px",left:mouseX+5+"px"});
 $("#bookbox").attr({bookid:bookid,bookvid:bookvid});
}

function closebookbox(e){
 var toElement=e.relatedTarget||e.toElement;
 while(toElement){
  if(toElement.id=="bookbox")return;
  toElement=toElement.parentNode;
 }
 $("#bookbox").hide();
}
//浮动层结束
