$(document).ready(function() {
	loginStatus(); //判断个人是否登录
	var userid = $.cookie("userid");
	$(".jqzoom").imagezoom();
	$("#thumblist").on("click", "li a", function() {
		$(this).parents("li").addClass("tb-selected").siblings().removeClass("tb-selected");
		$(".jqzoom").attr('src', $(this).find("img").attr("src"));
		$(".jqzoom").attr('rel', $(this).find("img").attr("src"));
	});
	$('.shareWeixin').hover(function() {
		$('.shareCode').stop(true, false).fadeToggle();
	});
	var resourceId = GetQueryString("resourceId");
	var professorId = "";
	getRecourceMe();
	/*获取资源信息*/
	function getRecourceMe() {
		$.ajax({
			"url": "/ajax/resource/queryOne",
			"type": "GET",
			"success": function(data) {
				console.log(data);
				if(data.success) {
					resourceHtml(data.data);
					var resourceName = data.data.resourceName + "-科袖网";
					window.setTimeout(function() {
						document.title = resourceName;
					}, 500);
				}
			},
			"data": {
				"resourceId": resourceId
			},
			dataType: "json",
			'error': function() {
				$.MsgBox.Alert('提示', '服务器连接超时！');
			}
		});
	}
	/*合作备注及性能参数存储换行格式*/
	function outHTML(selecter) {
		var getValue = selecter;
		var aa = "";
		if(getValue) {
			var endValue = ((getValue.replace(/<(.+?)>/gi, "&lt;$1&gt;")).replace(/ /gi, "&nbsp;")).replace(/\n/gi, "|");
			var cc = endValue.split("|");
			for(var i = 0; i < cc.length; i++) {
				aa += cc[i] + '<br/>'
			}
		}
		return aa;
	}
	/*处理资源html代码*/
	function resourceHtml($da) {
		$("#resourceName").text($da.resourceName); //名字
		$("#application").text($da.supportedServices); //应用用途
		if($da.editProfessor) {
			$("#person,#orgName").show();
			if($da.orgName) { //所属机构
				$("#organizationName").text($da.orgName).parents("li").show();
			}
			$("#nameS").text($da.editProfessor.name); //"office": "", //职位 "title": "", //职称
			if($da.editProfessor.title) {
				$("#titleOffice").text($da.editProfessor.title);
			} else {
				if($da.editProfessor.office) {
					$("#titleOffice").text($da.editProfessor.office);
				}
			}
			if($da.editProfessor.orgName) {
				$("#orgType").text($da.editProfessor.orgName);
			}
			professorId = $da.editProfessor.id;
			/*if(userid!=professorId) {
				$(".goSpan").show();
			}*/
			var professorFlag = autho($da.editProfessor.authType, $da.editProfessor.orgAuth, $da.editProfessor.authStatus);
			$("#authFlag").addClass(professorFlag.sty).attr("title", professorFlag.title);
			if($da.editProfessor.hasHeadImage == 1) {
				$("#headImg").css("background-image", 'url(/images/head/' + $da.editProfessor.id + '_l.jpg)');
			}
		}else {
			$("#enterprise,#resPerson").show();
			if($da.organization.hasOrgLogo) {
						$("#Qimg").attr("style", "background-image: url(/images/org/" + $da.organization.id + ".jpg);");
					}
					if($da.organization.authStatus==3){
						$("#QauthFlag").addClass("authicon-com-ok").attr("title", "认证企业");	
					}
					$("#Qname").text($da.organization.name);
					$("#Qindustry").text($da.organization.subject);
					if($da.organization.orgType==2){
						$("#QorgType").text("上市企业");
					}else if($da.organization.orgType==3){
						$("#QorgType").text("国有企业");
					}else if($da.organization.orgType==4){
						$("#QorgType").text("合资企业");
					}else if($da.organization.orgType==5){
						$("#QorgType").text("私人企业");
					}else if($da.organization.orgType==6){
						$("#QorgType").text("外资企业");
					}else if($da.organization.orgType==7){
						$("#QorgType").text("初创企业");
					}else{
						$("#QorgType").text("");
					}
		}
		if($da.spec) { //厂商型号
			$("#modelNumber").text($da.spec).parents("li").show();
		}
		if($da.parameter) { //性能参数
			$("#performancePa").html(outHTML($da.parameter)).parents("li").show();
		}
		if($da.cooperationNotes) { //合作备注
			$("#remarkContent").html(outHTML($da.cooperationNotes)).parents("li").show();
		}
		if($da.subject) {
			var oSub = $da.subject.split(",");
			var oSt = "";
			for(var i = 0; i < oSub.length; i++) {
				oSt += '<li><p class="h2Font">' + oSub[i] + '</p></li>'
			}
			$(".tagList").html(oSt);
		}
		if($da.descp) { //编辑器
			$("#descp").html($da.descp).parents("li").show();
		}
		//return;
		if($da.images.length) {
			$("#firstFigure").attr({
				"src": "/data/resource/" + $da.images[0].imageSrc,
				"rel": "/data/resource/" + $da.images[0].imageSrc
			}).parent().attr("href", "/data/resource/" + $da.images[0].imageSrc);
			var arr = "";
			for(var i = 0; i < $da.images.length; i++) {
				if($da.images[i] == $da.images[0]) {
					var oString = '<li class="tb-selected">' +
						'<div class="tb-pic tb-s66">' +
						'<a href="javascript:void(0);">' +
						'<img src="/data/resource/' + $da.images[i].imageSrc + '"/>' +
						'</a>' +
						'</div>' +
						'</li>'
				} else {
					var oString = '<li >' +
						'<div class="tb-pic tb-s66">' +
						'<a href="javascript:void(0);">' +
						'<img src="/data/resource/' + $da.images[i].imageSrc + '"/>' +
						'</a>' +
						'</div>' +
						'</li>'
				}

				arr += oString;
			}
			$("#thumblist").html(arr);
		}

	}
	/*点击名字及头像跳转个人浏览页面*/
	$("#nameS,#headImg").click(function() {
		location.href = "information-brow.html?professorId=" + professorId;
	})
	/*点击咨询*/
	$("#consultin").on("click", function() {
		clickResouceConsultHandler();
	});
	/*咨询函数*/
	function clickResouceConsultHandler() {
		if(userid && userid != "null" && userid != null) {
			ConsultApply();
			concultProInfo(professorId); //专家信息common.js中
			//默认选中资源咨询和标题
			for(var i = 0; i < $("ul.menucon").children().length; i++) {
				$("ul.menucon").children().eq(i).removeClass("clicknow");
				$("ul.menucon").children().eq(1).addClass("clicknow");
			}
			var consultTitleVal = $(".h2Font").text();
			$("#consultTitle").val("关于" + consultTitleVal + "的咨询");
			//点击的发送
			$("#sendConsultBtn").click(function() {
				sendConsultHandler(professorId); //common.js中
			});
		} 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');
		}

	};
	//点击专家关注
$('.attenSpan').click(function(){
	if (userid && userid != "null" && userid != null) {
		if($(this).is('.attenSpanyes')){
			 cancelCollectionExpert(professorId,1)
		}else{
			collectionExpert(professorId,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');
	}
})

//点击资源收藏
$('#attention em').click(function(){
	if (userid && userid != "null" && userid != null) {
		if($(this).is('.icon-collected')){
			 cancelCollectionExpert(resourceId,2)
		}else{
			 collectionExpert(resourceId,2);
		}	
	}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 ifCollection(getid,num) {
	var data = {"professorId": userid,"watchObject": getid}
	//alert(JSON.stringify(data))
	$.ajax({		
		url:"/ajax/watch/hasWatch",
		data:data,
		dataType: 'json', //数据格式类型
		type: 'get', //http请求类型
		timeout: 10000,
		async: true,
		success: function(data) {
			if(num==1){
				if(data.success && data.data != null) {
					$('.attenSpan').addClass("attenSpanyes");
					$('.attenSpan').text('已关注');
					zjreturnId = data.data.watchObject;
				} else {
					$('.attenSpan').removeClass("attenSpanyes");
					$('.attenSpan').text('关注');
				}
			}
			if(num==2){
				if(data.success && data.data != null) {
					$('#attention em').addClass("icon-collected").removeClass("icon-collect");
					wzreturnId = data.data.watchObject;
				} else {
					$('#attention em').removeClass("icon-collected").addClass("icon-collect");
				}
			}
			
		},
		error: function() {
			$.MsgBox.Alert('提示',"服务器链接超时");
		}
	});
}

/*关注专家*/
function collectionExpert(watchObject,num) {
	if(num==1){
		var data = {"professorId": userid,"watchObject": watchObject,"watchType": 1}
	}else{
		var data = {"professorId": userid,"watchObject": watchObject,"watchType": 3}
	}
	$.ajax({		
		url:"/ajax/watch",
		data:data,
		dataType: 'json', //数据格式类型
		type: 'POST', //http请求类型
		timeout: 10000,
		async: false,
		success: function(data) {
			if(num==1){
				if(data.success) {
					$('.attenSpan').addClass("attenSpanyes");
					$('.attenSpan').text('已关注');
					zjreturnId = data.data.watchObject;
				}
			}
			
			if(num==2){
				if(data.success) {
					$('#attention em').addClass("icon-collected").removeClass("icon-collect");
					wzreturnId = data.data.watchObject;
				}
			}
			
		},
		error: function() {
			$.MsgBox.Alert('提示',"服务器链接超时");
		}
	});
}

/*取消收藏专家*/
function cancelCollectionExpert(watchObject,num) {
	$.ajax({		
		url:"/ajax/watch/delete",
		data: {
			professorId: userid,
			watchObject: watchObject
		},
		dataType: 'json', //数据格式类型
		type: 'post', //http请求类型
		timeout: 10000,
		async: true,
		success: function(data) {
			console.log(data.success)
			if(num==1){
				if(data.success) {
					$('.attenSpan').removeClass("attenSpanyes");
					$('.attenSpan').text('关注');
				}	
			}
			if(num==2){
				if(data.success) {
					$('#attention em').removeClass("icon-collected").addClass("icon-collect");
				}	
			}
			
		},
		error: function(data) {
			$.MsgBox.Alert('提示',"服务器链接超时");
		}
	});

}
selUse();
	function selUse() {
		$.ajax({
			url: "/ajax/resource/qaLinkman",
			type: "GET",
			timeout: 10000,
			dataType: "json",
			async:true,
			data: {
				"resourceId": resourceId,
			},
			success: function(data, textState) {
				console.log(data)
				if(data.success) {
					unauthUser(data.data);
				}
			},
			error: function(XMLHttpRequest, textStats, errorThrown) {
				$.MsgBox.Alert('提示', '服务器请求失败')
			}
		})
	}
	function unauthUser($res) {
	var osting=""
	for(var i = 0; i < $res.length; i++) {
		var img;
		var styC="";
		var oClass = autho($res[i].professor.authType, $res[i].professor.orgAuth, $res[i].professor.authStatus);
		var oTitle="";
		if($res[i].professor.title) {
			oTitle=$res[i].professor.title;
		}else{
			if($res[i].professor.office) {
				oTitle=$res[i].professor.office;
			}
		}
		if($res[i].professor.hasHeadImage) {
				img = "/images/head/" + $res[i].professor.id + "_l.jpg";
			} else {
				img = "../images/default-photo.jpg"
			}
		var oSt = '<li>'
		oSt += '<div class="madiaHead useHead" id="userimg" style="background-image: url('+img+');"></div>'
		oSt += '<div class = "madiaInfo">'
		oSt += '<p class = "ellipsisSty">'
		oSt += '<span class = "h1Font" id="name">'+$res[i].professor.name+'</span><em class="authicon '+oClass.sty+'" title="'+oClass.title+'"></em >'
		oSt += '</p>'
		oSt += '<p class="h2Font ellipsisSty">'+oTitle+'</p>'
		oSt += '</div>'
		oSt += '<div class="'+styC+'" flag=1></div>'
		oSt += '</li>'
		osting+=oSt;
	}
	$("#expertli").html(osting);
}
})