$(document).ready(function() {
	loginStatus(); //判断个人是否登录
	var userid = $.cookie("userid");
	var userName = $.cookie("userName");
	var paperId = GetQueryString("paperId");

	ifcollectionAbout(paperId,$("#collectBtn"), 5)
	getPaperMe();
	pageViewsVal();
	if(userid && userid != "null" && userid != null){
		$(".ifLoginOn").removeClass("displayNone");
	}else{
		$(".ifLoginUn").removeClass("displayNone");
	}
	//点击收藏按钮
	$("#collectBtn").on('click', function() {
		if(userid && userid != null && userid != "null") {
			if($(this).is('.icon-collected')){
				cancelCollectionAbout(paperId,$(this), 5)
			} else {
				collectionAbout(paperId,$(this), 5);
			}
		}else{
			$.MsgBox.Alert("提示", "请先登录再进行收藏");
			$("#mb_btn_ok").val("去登录");
			var aele = document.createElement('a');
			$("#mb_btnbox").append(aele);
			$("#mb_btnbox a").css({
				'display': "block",
				'width': '100%',
				'height': '40px',
				'position': 'absolute',
				'bottom': '-6px',
				'left': '0'
			});
			aele.setAttribute('href', '../login.html');
		}
	});
	
	//关键词标签点击进去搜索
	$(".tagList").on("click","li",function(){
		var tagText = $(this).find("p").text();
		location.href = "searchNew.html?searchContent=" + tagText + "&tagflag=6";
	})
	
	//点击进入个人详情页面
	$("#aboutAuthors").on("click","li>a",function(){
		var oDataId = $(this).attr("data-id");
		if(oDataId.substring(0,1)!="#"){
			$(this).attr("href","userInforShow.html?professorId="+oDataId);
		}else{
			$(this).attr("href","javascript:void(0)");
		}
	})
	$("#aboutAuthors").on("click","li.lastBtn",function(){
		$("#aboutAuthors li").css("display","block");
		$(this).hide();
	})
	//点击关注按钮
	$("#aboutAuthors").on('click',"span.attenSpan", function() {
		var pId=$(this).parent().siblings("a").attr("data-id");
		if(userid && userid != null && userid != "null") {
			if($(this).is('.attenedSpan')){
				cancelCollectionAbout(pId,$(this),1)
			} else {
				collectionAbout(pId,$(this),1);
			}
		}else{
			$.MsgBox.Alert("提示", "请先登录再进行关注");
			$("#mb_btn_ok").val("去登录");
			var aele = document.createElement('a');
			$("#mb_btnbox").append(aele);
			$("#mb_btnbox a").css({
				'display': "block",
				'width': '100%',
				'height': '40px',
				'position': 'absolute',
				'bottom': '-6px',
				'left': '0'
			});
			aele.setAttribute('href', '../login.html');
		}
	});

	
	/*获取论文信息*/
	function getPaperMe() {
		$.ajax({
			"url": "/ajax/ppaper/qo",
			"type": "GET",
			"success": function(data) {
				console.log(data);
				if(data.success) {
					paperHtml(data.data);
					getPaperAuthors(data.data.id)
					var paperName = data.data.name + "-科袖网";
					document.title = paperName;
				}
			},
			"data": {
				"id": paperId
			},
			dataType: "json",
			'error': function() {
				$.MsgBox.Alert('提示', '服务器连接超时!');
			}
		});
	}
	/*获取论文作者信息*/
	function getPaperAuthors(stritrm) {
		$.ajax({
			"url": "/ajax/ppaper/authors",
			"type": "GET",
			"success": function(data) {
				console.log(data);
				if(data.success) {
					if(data.data.length>0){
						$("#aboutAuthors .lastBtn span").text(data.data.length);
						for(var i=0;i<data.data.length;i++){
							var authTy="",authTit="",baseInfo="",ifPoint="",imgbg="../images/default-photo.jpg";
							if(data.data[i].professorId.substring(0, 1) != "#"){
								$.ajax({
									type:"get",
									url:"/ajax/professor/editBaseInfo/" + data.data[i].professorId,
									async:false,
									success:function($proData){
										console.log($proData)
										if($proData.success){
											var showPro = $proData.data;
											if(showPro.hasHeadImage == 1) {
												imgbg = "/images/head/" + showPro.id + "_l.jpg";
											} else {
												imgbg = "../images/default-photo.jpg";
											}
											ifPoint = "pointThis";
											//认证
											var oSty = autho(showPro.authType,showPro.orgAuth,showPro.authStatus);
											authTy = oSty.sty;
											authTit = oSty.title;
											
											var title = showPro.title || "";
											var orgName = showPro.orgName || "";
											var office = showPro.office || "";
											if(orgName!=""){
												if(title != "") {
													baseInfo = title + "," + orgName;
												}else{
													if(office!=""){
														baseInfo = office  + "," + orgName;	
													}else{
														baseInfo = orgName;	
													}
												}
											}else{
												if(title != "") {
													baseInfo = title;
												}else{
													if(office!=""){
														baseInfo = office;	
													}else{
														baseInfo = "";	
													}
												}
											}
											var str="";
											str +='<li class="flexCenter"><a href="" class="'+ ifPoint +'" data-id="'+ showPro.id +'">'
											str +='<div class="madiaHead useHead" style="background-image:url('+ imgbg +')"></div>'
											str +='<div class="madiaInfo" style="margin-top:-4px" >'
											str +='<p class="ellipsisSty"><span class="h1Font">'+ showPro.name +'</span><em class="authiconNew '+ authTy +'" title="'+ authTit +'"></em></p>'
											str +='<p class="h2Font ellipsisSty">'+ baseInfo +'</p>'
											str +='</div></a>';
											if(showPro.id==userid){
												str +=''
											}else{
												str +='<div class="goSpan"><span class="attenSpan">关注</span></div>';
											}
											str +='</li>';
											var $str=$(str);
											$("#aboutAuthors .lastBtn").before($str);
											if(showPro.id!=userid){
												ifcollectionAbout(showPro.id,$str.find(".attenSpan"),1);
											}
										}
									}
								})
							}else{
								var str="";
								str +='<li class="flexCenter"><a data-id="'+ data.data[i].professorId +'">'
								str +='<div class="madiaHead useHead" style="background-image:url('+ imgbg +')"></div>'
								str +='<div class="madiaInfo">'
								str +='<p class="ellipsisSty"><span class="h1Font">'+ data.data[i].name +'</span></p>'
								str +='</div></a>';
								if(data.data[i].name==userName){
									str +='<div class="goSpan"><span class="ifMe" flag="1">是我本人</span></div>'
								}else{
									str +='<div class="goSpan"><span class="yaoqing">邀请'
									str +='<div class="shareCode clearfix"><div class="floatL qrcodeUser"></div>'
									str +='<div class="shareWord floatR"><p>打开微信“扫一扫”,<br/>打开网页后点击屏幕右上角“分享”按钮</p></div>'
									str +='</div></span></div>';
								}
								
								str +='</li>';
								var $str=$(str);
								$("#aboutAuthors .lastBtn").before($str);
							}
							
							if(data.data.length<5){
								$("#aboutAuthors li").css("display","block");
								$(".lastBtn").hide();
							}else{
								$("#aboutAuthors li:lt(3)").css("display","block");
							}
						}
						
						//邀请
						$('.goSpan').on("mouseenter",".yaoqing",function(){
							$(this).find('.shareCode').stop(true,false).fadeIn();
						}).on("mouseleave",".yaoqing",function(){
							$(this).find('.shareCode').stop(true,false).fadeOut();
						});
						//邀请作者
						var Qcu=document.getElementsByClassName("qrcodeUser");
						for(var i=0;i<Qcu.length;i++){
							var qrcode= new QRCode(Qcu[i], {
								width : 100,
								height : 100
							});
							makeCode();
						}
						function makeCode(){
							var hurl = window.location.host;
							if(userid) {
								var elurl = "http://" + hurl + "/e/I.html?i=" + s16to64(paperId)+"&d="+s16to64(userid)+"&f=1";
							} else{
								var elurl = "http://" + hurl + "/e/I.html?i=" + s16to64(paperId)+"&f=1";
							}
							qrcode.makeCode(elurl);
						}
						
						//是我本人
						$('.goSpan').on("click",".ifMe",function(){
							var oF=$(this).attr("flag");
							if(oF==1){
								$.MsgBox.Confirm("提示", "确认这是您发表的论文?", daoRuPaper);
								$(this).attr("flag","0");
							}else{
								return;
							} 
						});
						

					}
				}
			},
			"data": {
				"id": stritrm
			},
			dataType: "json",
			'error': function() {
				$.MsgBox.Alert('提示', '服务器连接超时!');
			}
		});
	}
	function daoRuPaper(){
		$.ajax({
			"url": "/ajax/ppaper/ass",
			"type": "POST",
			"data": {
				id:paperId,
				uid:userid,
				author: userName
			},
			dataType: "json",
			"success": function(data) {
				if(data.success) {
					if(data.data){
						$(".ifMe").text("导入成功").css("background","#ccc");
					}
				}
			},
			'error': function() {
				$.MsgBox.Alert('提示', '服务器连接超时!');
			}
		});
	}
	/*处理论文html代码*/
	function paperHtml($da) {
		$("#paperName").text($da.name); //名字
		$("#pageView").text($da.pageViews); //浏览量
		$("#paperAbstract").text($da.summary); //摘要内容
		if(!$da.cn4periodical){
			$da.cn4periodical=""
		}
		if(!$da.en4periodical){
			$da.en4periodical=""
		}
		if(!$da.cn4periodical && !$da.en4periodical){
			$("#paperJournal").parents("li").hide();
		}else{
			$("#paperJournal").text($da.cn4periodical +" " + $da.en4periodical);
		}

		if(!$da.pubDay){
			$("#paperVolume").parents("li").hide();
		}else{
			$("#paperVolume").text($da.pubDay);
		}
		if($da.keywords != undefined && $da.keywords.length != 0 ){
			var subs = new Array();
			if($da.keywords.indexOf(',')){
				subs = $da.keywords.split(',');
			}else{
				subs[0] = $da.keywords;
			}
			if(subs.length>0){
				patentRelatedList(subs)
				for (var i = 0; i < subs.length; i++) 
				{
					$(".tagList").append('<li><p class="h2Font">'+ subs[i] +'</p></li>');
				};
			}else{
				$(".tagList").hide();
			}
		}		
		var weibopic = "http://" + window.location.host + "/images/default-paper.jpg"
		var weibotitle = $da.name;
		var weibourl = window.location.href;
		$("#weibo").attr("href","http://service.weibo.com/share/share.php?appkey=3677230589&title="+encodeURIComponent(weibotitle)+"&url="+encodeURIComponent(weibourl)+"&pic="+encodeURIComponent(weibopic)+"&content=utf-8"+"&ralateUid=6242830109&searchPic=false&style=simple");
	}
	isAgreeNum()
	function isAgreeNum() {
		var data = {"id": paperId}
		$.ajax({		
			url:"/ajax/ppaper/agreeCount",
			data:data,
			dataType: 'json', //数据格式类型
			type: 'get', //http请求类型
			timeout: 10000,
			async: true,
			success: function(data) {
				if(data.success){
					if(userid && userid != "null" && userid != null) {
						isAgree(data.data) //文章点赞
					} else {
						$(".thumbBtn").html("赞 <span>" + data.data + "</span>");
					}
				}
			},
			error: function() {
				$.MsgBox.Alert('提示',"服务器链接超时");
			}
		});
	}
	/*判断论文是否被赞*/
function isAgree(articleAgree) {
	var data = {"id": paperId,"uid":userid }
	$.ajax({		
		url:"/ajax/ppaper/agree",
		data:data,
		dataType: 'json', //数据格式类型
		type: 'get', //http请求类型
		timeout: 10000,
		async: true,
		success: function(data) {
			if(data.success){
				if(data.data){
					$(".thumbBtn").html("已赞 <span>"+articleAgree+"</span>");
					$(".thumbBtn").addClass("thumbedBtn").css("cursor","auto");
					
				}else{
					$(".thumbBtn").html("赞 <span>"+articleAgree+"</span>");
					$(".thumbBtn").addClass("thunbgo");
				}
			}
		},
		error: function() {
			$.MsgBox.Alert('提示',"服务器链接超时");
		}
	});
}
//论文点击点赞
$('.thumbBlock').on("click",".thunbgo",function(){
	if (userid && userid != "null" && userid != null) {
		addAgree();
	}else{
		$.MsgBox.Alert('提示',"请先登录再进行点赞");
		$("#mb_btn_ok").val("去登录");
		var aele = document.createElement('a');
		$("#mb_btnbox").append(aele);
		$("#mb_btnbox a").css({
			'display': "block",
			'width': '100%',
			'height': '40px',
			'position': 'absolute',
			'bottom': '-6px',
			'left': '0'
		});
		aele.setAttribute('href', '../login.html');
	}
})
/*点赞*/
function addAgree() {
	console.log(paperId)
	var data = {"uid": userid,"id": paperId}
	$.ajax({		
		url:"/ajax/ppaper/agree",
		data:data,
		dataType: 'json', //数据格式类型
		type: 'POST', //http请求类型
		timeout: 10000,
		async: true,
		success: function(data) {
			if(data.success){
				var articleAgreeval = $(".thumbBtn span").text();
				$(".thumbBtn").html("已赞 <span>"+(parseInt(articleAgreeval)+1)+"</span>");
				$(".thumbBtn").addClass("thumbedBtn").css("cursor","auto");
				$(".thumbBtn").removeClass("thunbgo");
			}
		},
		error: function() {
			$.MsgBox.Alert('提示',"服务器链接超时");
		}
	});
}
/*留言模块*/
limitObj(".msgCont",200)
$("#meSendtt").on("click", function() {
	article();
})
//查看更多留言
$("#moreArtical").on("click",function(){
	vcreateTime = $(".commentList li").last().attr("data-time");
	orderKey = $(".commentList li").last().attr("data-key");
	message(5,1);
})
//删除留言
$(".commentList").on("click",".messageDel",function(){
	var commenid = $(this).attr("data-id");
	articledel(commenid)
})

//发布留言
function article() {
	$.ajax({
		url:"/ajax/leaveWord/paper",
		dataType: 'json', //数据格式类型
		type: 'POST', //http请求类型
		data: {
			"paperId": paperId,
			"sender": userid,
			"content": $(".msgCont").val(),
		},
		timeout: 10000, //超时设置
		success: function(data) {
			var $info = data.data || {};
			if(data.success && data.data) {
				message(5,2);
			}
			$(".msgCont").val("");
			$(".msgconNum").find("span").text(0);
		},
		error: function() {
			$.MsgBox.Alert('提示', '服务器请求失败')
		}
	});
}

message(5,2);
function message(rows,num) {
	if(num==1){
		var data = {"paperId": paperId,"createTime": vcreateTime,"orderKey": orderKey,"rows": rows}	
	}else{
		var data = {"paperId": paperId,"rows": rows}	
	}
	$.ajax({
		url: "/ajax/leaveWord/ql/paper",
		dataType: 'json', //数据格式类型
		type: 'GET', //http请求类型
		data: data,
		timeout: 10000, //超时设置
		success: function(data) {
			if(data.success) {
				 if(data.data != ""){
				 	if(num==2){
						$(".commentList").html("");	
					}
					if(data.data.length > 4){
						$("#moreArtical").removeClass("displayNone");
					}else{
						$("#moreArtical").addClass("displayNone");
					}
					for(var i = 0; i < data.data.length; i++) {
						var itemlist = '<li class="flexCenter" data-time="" data-key="">';
							itemlist += '<a href class="userhref"> <div class="madiaHead useHead useHeadMsg"></div></a>';
							itemlist += '<div class="madiaInfo">';
							itemlist += '<p><a href class="userhref"><span class="h1Font messageName"></span></a><em class="authiconNew" title="科袖认证专家"></em><span class="commenttime"></span></p>';
							itemlist += '<p class="h2Font messageContent"></p>';
							itemlist += '<div class="operateSpan"><span class="callBack">回复</span><span class="messageDel">删除</span></div>';
							itemlist += '</div></li>';
							$itemlist = $(itemlist);
							$(".commentList").append($itemlist);
							var datalist = data.data[i];
							$itemlist.find(".messageName").text(datalist.professor.name);
							$itemlist.find(".messageContent").text(datalist.content);
							var userType = autho(datalist.professor.authType, datalist.professor.orgAuth, datalist.professor.authStatus);
								$itemlist.find(".authiconNew").attr("title", userType.title);
								$itemlist.find(".authiconNew").addClass(userType.sty);
							if(datalist.professor.hasHeadImage==1) {
								$itemlist.find(".useHeadMsg").attr("style", "background-image: url(/images/head/" + datalist.professor.id + "_l.jpg);");
							}	
							if(datalist.professor.id==userid){
								$itemlist.find(".messageDel").show();
							}
							$itemlist.attr("data-time", datalist.createTime);
							$itemlist.attr("data-key", datalist.orderKey);
							$itemlist.find(".messageDel").attr("data-id", datalist.id);
							$itemlist.find(".userhref").attr("href", "userInforShow.html?professorId="+datalist.professor.id);
							var createtime = datalist.createTime;
							$itemlist.find(".commenttime").text(commenTime(createtime));
					}
				 }else{
				 	if(num==2){
						$(".commentList").html("");	
					}else{
						$("#moreArtical").addClass("displayNone");
					}
					
				 }
			}
		},
		error: function() {
			$.MsgBox.Alert('提示', '服务器请求失败')
		}
	});
}
	
//删除自己的留言
function articledel(commenid) {
	$.ajax({
		url:"/ajax/leaveWord/delete",
		dataType: 'json', //数据格式类型
		type: 'POST', //http请求类型
		data: {
			"id": commenid,
		},
		timeout: 10000, //超时设置
		success: function(data) {
			if(data.success) {
				message(5,2);
			}
		},
		error: function() {
			$.MsgBox.Alert('提示', '服务器请求失败')
		}
	});
}	

	//您可能感兴趣的论文
	paperInterestingList()
	function paperInterestingList(){
		$.ajax({
			"url" : "/ajax/ppaper/ralatePapers",
			"type" :  "GET" ,
			"dataType" : "json",
			"data" :{
				"paperId":paperId
			},
			//"async":false,
			"traditional": true, //传数组必须加这个
			"success" : function(data) {
				if(data.success) {
					var dataStr=data.data
					if(dataStr.length > 0){
						$("#interPaper").show();
						var itemlist = '';
						$("#paperList").html("");
						for(var i = 0; i < dataStr.length; i++) {
							var moreInf=""
							if(!dataStr[i].cn4periodical){
								dataStr[i].cn4periodical="";
							}
							if(!dataStr[i].en4periodical){
								dataStr[i].en4periodical="";
							}
							if(!dataStr[i].pubDay){
								dataStr[i].pubDay="";
							}
							moreInf = dataStr[i].cn4periodical+ " " +dataStr[i].en4periodical+ " " +dataStr[i].pubDay;
							var itemlist = '<li>';
							itemlist += '<a class="flexCenter" target="_blank" href="paperShow.html?paperId=' + dataStr[i].id +'" class="linkhref"><div class="madiaHead paperHead"></div>';
							itemlist += '<div class="madiaInfo">';
							itemlist += '<p class="h1Font ellipsisSty">'+ dataStr[i].name +'</p>';
							itemlist += '<p class="h2Font ellipsisSty">作者:'+ dataStr[i].authors.substring(0, dataStr[i].authors.length - 1) +'</p>';
							itemlist += '<p class="h2Font ellipsisSty">期刊:'+ moreInf +'</p>';
							itemlist += '</div></a></li>';
							$itemlist = $(itemlist);
							$("#paperList").append($itemlist);
					}	}
				}
			},
			"error":function(){
				$.MsgBox.Alert('提示','链接服务器超时')
			}
		});
	}
	//浏览量
	function pageViewsVal() {
		$.ajax({
			"url": "/ajax/ppaper/incPageViews",
			"type": "POST",
			"dataType": "json",
			"data": {
				"id": paperId
			},
			"success": function(data) {
				console.log(data);
				if(data.success) {}
			},
			"error": function() {
				$.MsgBox.Alert('提示', '链接服务器超时')
			}
		});
	}
	
	//纠错反馈
	$(".correctSubmit").on("click",function(){
		var cntCon=$(this).siblings(".correctCon").val();
		var cntUser="";
		if(userid && userid != null && userid != "null") {
			cntUser = userid;
		}
		if(cntCon.length>500){
			$.MsgBox.Alert('提示', '纠错反馈内容不得超过500个字');
			return;
		}else{
			$.ajax({
				"url": "/ajax/feedback/error/paper",
				"type": "POST",
				"dataType": "json",
				"async": true,
				"data": {
					"id": paperId,
					"cnt":cntCon,
					"user":cntUser
				},
				"success": function(data) {
					if(data.success) {
						backSuccessed();
					}
				},
				"error": function() {
					$.MsgBox.Alert('提示', '链接服务器超时')
				}
			});
		}
	})
	function patentRelatedList(array){
		$.ajax({
			"url":"/ajax/ppatent/assPatents",
			"type" :  "GET" ,
			"dataType" : "json",
			"data" :{
				"kws":array
			},
			"traditional": true, //传数组必须加这个
			"success" : function(data) {
				if(data.success) {
					var dataStr=data.data
					if(dataStr.length > 0){
						$("#patentrelate").removeClass("displayNone");
						for(var i = 0; i < dataStr.length; i++) {
							var itemlist ='<li style="min-height:56px;"><a href="patentShow.html?patentId='+dataStr[i].id+'"><p class="h2Font ellipsisSty-2" style="line-height:20px;"><em class="circlePre"></em>'+ dataStr[i].name +'</p></a></li>'
							$(".recentlyList").append(itemlist);
						}
					}
				}
			},
			"error":function(){
				$.MsgBox.Alert('提示', '链接服务器超时')
			}
		});
	}
})