$(document).ready(function() {
	var articleId = GetQueryString("articleId");
	var professorId = GetQueryString("professorId");
	var userid = $.cookie("userid");
	if(userid == "null") {
		$(".goMsgbox").hide();
	} else {
		$("#login").hide();
	}
	var industryShow = function(data, $id) {
		if(data != undefined && data.length != 0) {
			var subs = new Array();
			if(data.indexOf(',')) {
				subs = data.split(',');
			} else {
				subs[0] = data;
			}
			if(subs.length > 0) {
				var html = [];
				for(var i = 0; i < subs.length; i++) {
					html.push("<li>" + subs[i] + "</li>");
				};
				document.getElementById($id).innerHTML = html.join('');
			}
		}
	}

	function proInfoMain() {
		$.ajax({
			url: "/ajax/article/query",
			dataType: 'json', //数据格式类型
			type: 'GET', //http请求类型
			data: {
				"articleId": articleId
			},
			timeout: 10000, //超时设置
			success: function(data) {
				var $info = data.data || {};
				if(data.success && data.data) {	
					var oTime = $info.createTime.substring(0, 4) + "." + $info.createTime.substring(4, 6) + "." + $info.createTime.substring(6, 8) 
					$("#creTime").text(oTime);
					$profesor = data.data.professor;
					$("#articleName").text($info.articleTitle);
					$("#nameS,#proName").text($profesor.name);
					$("#orgName").text($profesor.orgName);
					$("#lineId").attr("href","information-brow.html?professorId="+$profesor.id);
					if($profesor.title) {
						if($profesor.office) {
							$("#title").text($profesor.title + " , ");
						} else {
							$("#title").text($profesor.title);
						}
					}
					if($profesor.office) {
						$("#office").text($profesor.office);
					}
					if($profesor.department) {
						$("#department").text($profesor.department);
					}
					if($profesor.address) {
						$("#address").text($profesor.address);
					}
					if($profesor.hasHeadImage) {
						$("#headImg,#proHead").attr("src", "/images/head/" + $info.professorId + "_l.jpg")
					}
					if($info.subject) {
						industryShow($info.subject, "subjectList");
					} else {
						$("#subjectList").parent().hide();
					}
					if($info.industry) {
						industryShow($info.industry, "industryList");
					} else {
						$("#industryList").parent().hide();
					}
					if(!$info.subject&&!$info.industry){
						$(".resAbout").hide();
					}
					var proTitle = document.getElementById("proTitle");
					var proOffice = document.getElementById("proOffice");
					var proOrg = document.getElementById("proOrg");
					var proAddress = document.getElementById("proAddress");
					if($info.professor.title && $info.professor.office && $info.professor.orgName && $info.professor.address) {
						proTitle.innerText = $info.professor.title + "，";
						proOffice.innerText = $info.professor.office + "，";
						proOrg.innerText = $info.professor.orgName + " | ";
						proAddress.innerText = $info.professor.address;
					} else if(!$info.professor.title && $info.professor.office && $info.professor.orgName && $info.professor.address) {
						proOffice.innerText = $info.professor.office + "，";
						proOrg.innerText = $info.professor.orgName + " | ";
						proAddress.innerText = $info.professor.address;
					} else if($info.professor.title && !$info.professor.office && $info.professor.orgName && $info.professor.address) {
						proTitle.innerText = $info.professor.title + "，";
						proOrg.innerText = $info.professor.orgName + " | ";
						proAddress.innerText = $info.professor.address;
					} else if($info.professor.title && $info.professor.office && !$info.professor.orgName && $info.professor.address) {
						proTitle.innerText = $info.professor.title + "，";
						proOffice.innerText = $info.professor.office + " | ";
						proAddress.innerText = $info.professor.address;
					} else if($info.professor.title && $info.professor.office && $info.professor.orgName && !$info.professor.address) {
						proTitle.innerText = $info.professor.title + "，";
						proOffice.innerText = $info.professor.office + "，";
						proOrg.innerText = $info.professor.orgName;
					} else if(!$info.professor.title && !$info.professor.office && $info.professor.orgName && $info.professor.address) {
						proOrg.innerText = $info.professor.orgName + " | ";
						proAddress.innerText = $info.professor.address;
					} else if(!$info.professor.title && $info.professor.office && !$info.professor.orgName && $info.professor.address) {
						proOffice.innerText = $info.professor.office + " | ";
						proAddress.innerText = $info.professor.address;
					} else if(!$info.professor.title && $info.professor.office && $info.professor.orgName && !$info.professor.address) {
						proOffice.innerText = $info.professor.office + "，";
						proOrg.innerText = $info.professor.orgName;
					} else if($info.professor.title && !$info.professor.office && !$info.professor.orgName && $info.professor.address) {
						proTitle.innerText = $info.professor.title + " | ";
						proAddress.innerText = $info.professor.address;
					} else if($info.professor.title && !$info.professor.office && $info.professor.orgName && !$info.professor.address) {
						proOffice.innerText = $info.professor.title + "，";
						proAddress.innerText = $info.professor.orgName;
					} else if($info.professor.title && $info.professor.office && !$info.professor.orgName && !$info.professor.address) {
						proTitle.innerText = $info.professor.title + "，";
						proOffice.innerText = $info.professor.office;
					} else if(!$info.professor.title && !$info.professor.office && !$info.professor.orgName && $info.professor.address) {
						proAddress.innerText = $info.professor.address;
					} else if(!$info.professor.title && !$info.professor.office && $info.professor.orgName && !$info.professor.address) {
						proOrg.innerText = $info.professor.orgName;
					} else if(!$info.professor.title && $info.professor.office && !$info.professor.orgName && !$info.professor.address) {
						proOffice.innerText = $info.professor.office;
					} else if($info.professor.title && !$info.professor.office && !$info.professor.orgName && !$info.professor.address) {
						proTitle.innerText = $info.professor.title;
					}
					if($info.articleContent) {
						$("#main_content").html($info.articleContent);
						var oImg = $("#main_content").find("img");
						for(var i = 0; i < oImg.length; i++) {
							(function(n) {
								$("#main_content").find("img").eq(n).load(function() {
									var imgWidth = $("#main_content").find("img").eq(n).width();
									var pWidth = $("#main_content").find("p").width();
									if(imgWidth > pWidth) {
										$("#main_content").find("img").eq(n).css({
											"width": "100%"
										});
									}
								})
							})(i);
						}
					}
				}
//				//留言分享操作栏				
//				var height=$(".resTop").height()-$(window).height();
//				var mainH = $("body");
//				$(window).scroll( function() { 
//					//console.log(height,mainH.scrollTop())
//				   if(mainH.scrollTop()>=height){
//				   	$('.share-nav').removeClass('fixed');
//				   }
//				   if(mainH.scrollTop()<height){
//				   	$('.share-nav').addClass('fixed');
//				   }
//				} );
		
			},
			error: function() {
				return;
			}
		});
	}

	var proArticle = function() {
		/*科研文章*/
		$.ajax({
			url: "/ajax/article/qaPro",
			dataType: 'json', //数据格式类型
			type: 'GET', //http请求类型
			data: {
				"professorId": professorId
			},
			timeout: 10000, //超时设置
			success: function(data) {
				if(data.success) {
					var $data = data.data;					
					if($data.length == 1) {
						$("div:contains('相关文章')").parents(".rightBlock").hide();
						return;
					}
					for(var i = 0; i < data.data.length; i++) {
						if(articleId != $data[i].articleId) {
							var articleImg = $data[i].articleImg;
							(!articleImg) ? articleImg = "../images/default-artical.jpg": articleImg = '/data/article/' + $data[i].articleImg;
							
							if($data[i].industry) {
								var subs = new Array();
								if($data[i].industry.indexOf(',')) {
									subs = $data[i].industry.split(',');
								} else {
									subs[0] = $data[i].industry;
								}
								if(subs.length > 0) {
									var indu = "";
									for(var n = 0; n < subs.length; n++) {
										indu += "<span>" + subs[n] + "</span>";
									}
								}
							} else {
								var indu = "";
							}
							var add = '<li>'
							add += '<a href="articalInfo.html?articleId=' + $data[i].articleId + '&professorId=' + professorId + '">'
							add += '<div class="art_topicBox">'
							add += '<div class="art_img"style="background:url(' + articleImg + ') center center no-repeat"></div>'
							add += '<div class="art_tbox">'
							add += '<h6 id="artical_topic" >' + $data[i].articleTitle + '</h6>'
							add += '</div>'
							add += '</div>'
							add += '<div class="tagsBox">'
							add += indu
							add += '</div>'
							add += '</a>'
							add += '</li>'

							$(".otherRes").append(add);
						}
					}
				}
			},
			error: function() {
				return;
			}
		});
	}

	function message() {
		$.ajax({
			url: "/ajax/leaveWord/ql",
			dataType: 'json', //数据格式类型
			type: 'GET', //http请求类型
			data: {
				"articleId": articleId
			},
			timeout: 10000, //超时设置
			success: function(data) {
				var $info = data.data || {};
				if(data.success && data.data) {
					$(".commentList").html("");
					$(".message").text($info.length);
					for(var i = 0; i < $info.length; i++) {
						var time = $info[i].createTime.substring(0, 4) + "年" + $info[i].createTime.substring(4, 6) + "月" + $info[i].createTime.substring(6, 8) + "日 " + $info[i].createTime.substring(8, 10) + ":" + $info[i].createTime.substring(10, 12);
						if($info[i].professor.hasHeadImage){
							var img='/images/head/' + $info[i].professor.id + '_l.jpg'
						}else{
							var img='../images/default-photo.jpg'
						}
						var string = '<li>'
						string += '<a class="proinfor clearfix">'
						string += '<div class="headblock floatL" style="width: 60px;"><img class="headimg headRadius" src="'+img+'" width="100%"></div>'
						string += '<div class="media-body floatL">'
						string += '<div><span class="listtit">'+$info[i].professor.name+'</span><span class="thistime">'+time+'</span></div>'
						string += '<p class="listtit3">'+$info[i].content+'</p>'
						string += '</div></a></li>'
						$(".commentList").append(string);
					}
				}
			},
			error: function() {
				return;
			}
		});
	}
	function userInformation(){
		$.ajax({
			url: "/ajax/professor/editBaseInfo/"+userid,
			dataType: 'json', //数据格式类型
			type: 'GET', //http请求类型
			timeout: 10000, //超时设置
			success: function(data) {
				console.log(data);
				if(data.success && data.data) {
					if(data.data.hasHeadImage==1){
					$("#limg").attr("src","/images/head/" + data.data.id + "_l.jpg")
					}
				}
			},
			error: function(XMLHttpRequest) {
				console.log(XMLHttpRequest)
			}
		});
	}
	function article() {
		$.ajax({
			url:"/ajax/leaveWord",
			dataType: 'json', //数据格式类型
			type: 'POST', //http请求类型
			data: {
				"articleId": articleId,
				"sender": userid,
				"content": $("textarea").val(),
			},
			timeout: 10000, //超时设置
			success: function(data) {
				var $info = data.data || {};
				if(data.success && data.data) {
					document.getElementsByTagName("textarea")[0].value = "";
					document.getElementById("meSend").style.background="#dadada";
					message();
				}
			},
			error: function() {
				return;
			}
		});
	}
	function trim(str) { //删除左右两端的空格
		　　
		return str.replace(/(^\s*)|(\s*$)/g, "");　　
	}
	document.getElementsByTagName("textarea")[0].addEventListener("keyup", function() {
		var valueLength = document.getElementsByTagName("textarea")[0].value;
		var length = trim(valueLength);
		if(length) {
			$("#meSend").addClass("colorAdd");
			document.getElementById("meSend").style.background="#ff9900";		
		} else {
			$("#meSend").removeClass("colorAdd");
			document.getElementById("meSend").style.background="#dadada";
		}
	});
	$(".goMsgbox").on('click', '.colorAdd', function() {
		article();
	});
	proInfoMain();
	proArticle();
	message();
	userInformation();
	
})