$(document).ready(function() {
	var articleId = GetQueryString("articleId");
	var professorId = GetQueryString("professorId");
	var oFlag = GetQueryString("oFlag");
	console.log(oFlag)
	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);
					$("#articleName").text($info.articleTitle);
					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.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 articletitle = $info.articleTitle + "-科袖网";
					window.setTimeout(function() {
						document.title = articletitle;
					}, 500);
				}
			},
			error: function() {
				return;
			}
		});
	}

	var proArticle = function() {
		var $f=new Object();
		if(oFlag==1){
			$f.orgId=professorId;
		}else{
			$f.professorId=professorId;
		}
		/*科研文章*/
		$.ajax({
			url: (oFlag==1)?"/ajax/article/qaOrg":"/ajax/article/qaPro",
			dataType: 'json', //数据格式类型
			type: 'GET', //http请求类型
			data: $f,
			timeout: 10000, //超时设置
			success: function(data) {
				if(data.success) {
					var $data = data.data;	
					console.log(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>'
							if(oFlag==1){
								add += '<a href="articalInfo.html?oFlag=1&articleId=' + $data[i].articleId + '&professorId=' + professorId + '">'
							}else{
								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(){
		if(oFlag==1){
			$.ajax({
			url: "/ajax/org/" + professorId,
			type: "GET",
			timeout: 10000,
			dataType: "json",
			beforeSend: function() {},
			success: function(data, textState) {
				if(data.success) {
					var $data = data.data;					
					$("#nameS").text($data.name);
					$("#proName").text($data.name);
					if($data.authStatus == 3) {
						$("#authFlag,#proTitle").replaceWith("<em class='authiconNew authicon-com-ok' title='认证企业'></em>");
					} else {
						$("#authFlag,#proTitle").replaceWith("<em class='authiconNew authicon-com-no' title='未认证企业'></em>");
					}
					if($data.hasOrgLogo) {
						$("#headImg,#proHead").attr("src", "/images/org/" + $data.id + ".jpg");
					} else {
						$("#headImg,#proHead").attr("src", "images/default-icon.jpg");
					}
					if($data.foundTime) {
						var oTime = timeGeshi($data.foundTime);
						$("#createTime").text(oTime);
					} 
					if($data.city) {
						$("#ocity").text($data.city);
					}
					
					if($data.orgSize) {
						switch($data.orgSize) {
							case '1':
								$("#qualificationList").text("50人以内")
								break;
							case '2':
								$("#qualificationList").text("50-100人")
								break;
							case '3':
								$("#qualificationList").text("100-200人")
								break;
							case '4':
								$("#qualificationList").text("200-500人")
								break;
							case '5':
								$("#qualificationList").text("500-1000人")
								break;
							default:
								$("#qualificationList").text("1000人以上")
								break;
						}
					} 
					if($data.orgType) {
						switch($data.orgType) {
							case '2':
								$("#orgType").text("国有企业");
								break;
							case '3':
								$("#orgType").text("上市企业");
								break;
							case '4':
								$("#orgType").text("合资企业");
								break;
							case '5':
								$("#orgType").text("私人企业");
								break;
							case '6':
								$("#orgType").text("外资企业");
								break;
							default:
								$("#orgType").text("初创企业");
								break;
						}
					}
					
				}
			},
			error: function(XMLHttpRequest, textStats, errorThrown) {
				$.MsgBox.Alert('提示', '服务器请求失败')
			}
		})
		}else{
			$.ajax({
			url: "/ajax/professor/editBaseInfo/"+professorId,
			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")
					}*/
					var $profesor=data.data;
					$("#nameS,#proName").text($profesor.name);
					var oString = todStr($profesor.title, $profesor.office, $profesor.department);
					$("#proTitle").text(oString);
					if($profesor.title) {
						if($profesor.office) {
							$("#orgType").text($profesor.title + " , "+$profesor.office);
						} else {
							$("#orgType").text($profesor.title);
						}
					}else{
						if($profesor.office) {
							$("#orgType").text($profesor.office);
						}
					}
					if($profesor.department) {
						$("#qualificationList").text($profesor.department);
					}
					if($profesor.orgName){
						$("#createTime").text($profesor.orgName)
					}
					if($profesor.address) {
						$("#ocity").text($profesor.address);
					}
					if($profesor.hasHeadImage) {
						$("#headImg,#proHead").attr("src", "/images/head/" + $profesor.id + "_l.jpg")
					}else{
						$("#headImg,#proHead").attr("src", "images/default-photo.jpg")
					}
				}
			},
			error: function(XMLHttpRequest) {
				console.log(XMLHttpRequest)
			}
		});
		}
	}
	/*职位职称所属机构*/
	function todStr() {
		var arr = new Array(),
			i;
		for(i in arguments) {
			if(arguments[i]) {
				arr.push(arguments[i])
			}
		}
		return arr.join();
	}
	/*时间格式转换*/
	function timeGeshi(otm) {
		var otme = otm.substring(0, 4) + "-" + otm.substring(4, 6) + "-" + otm.substring(6, 8);
		return otme;
	}
	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();
	});
	function luser(){
		$.ajax({
			url: "/ajax/professor/editBaseInfo/"+userid,
			dataType: 'json', //数据格式类型
			type: 'GET', //http请求类型
			timeout: 10000, //超时设置
			success: function(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)
			}
		});
	}
	proInfoMain();
	proArticle();
	message();
	userInformation();
	luser();
})