$(function(){
loginStatus();//读取用户头像
	 	var userid = $.cookie("userid");
	 	var professorId = GetQueryString("professorId");
	 	var name;
	 	if(professorId){

	 		var subjectShow = function (data){
				if(data != undefined &&  data.length != 0 ){
					var subs = new Array();
					if(data.indexOf(',')){
						subs = data.split(',');
					}else{
						subs[0] = data;
					}
					if(subs.length>0){
						for (var i = 0; i < subs.length; i++) {							
							$("#subjectShow").append("<div class='acad'>"+subs[i]+"</div>");		
								
						};
					}	
				}			
			}
			var industryShow = function (data){

				if(data != undefined && data.length != 0  ){

					var subs = new Array();
					if(data.indexOf(',')){
						subs = data.split(',');
					}else{
						subs[0] = data;
					}

					if(subs.length>0){
						for (var i = 0; i < subs.length; i++) {
							
							$("#industryShow").append("<li>"+subs[i]+"</li>");		
								
						};
					}	
				}			
			}

			// var eduBgShow = function (data){

			// 	if(data != undefined && data.length != 0  ){

			// 		var subs = new Array();
			// 		if(data.indexOf(',')){
			// 			subs = data.split(',');
			// 		}else{
			// 			subs[0] = data;
			// 		}

			// 		if(subs.length>0){
			// 			for (var i = 0; i < subs.length; i++) {
			// 				$("#industryList").append("<div class='acad'>"+ subs[i] +"<span class='remove'><img src='images/move.png'></span></div>");
			// 				$("#industryShow").append("<li>"+subs[i]+"</li>");		
								
			// 			};
			// 		}	
			// 	}			
			// }

			var eduBgShow = function(data) {

				if(data.length>0){
					for (var i = 0; i < data.length; i++) {

						var showHtml = "<li>";
						if(typeof(data[i].school) !== "undefined"){
							showHtml += data[i].school;
						}else{
							showHtml += "不详";
						}
						showHtml += "-";
						if(typeof(data[i].college) !== "undefined"){
							
							showHtml += data[i].college;
						}else{
							showHtml += "不详";
						}
						showHtml += "-";
						if(typeof(data[i].major) !== "undefined"){
							
							showHtml += data[i].major;
						}else{
							showHtml += "不详";
						}
						showHtml += "-";
						if(typeof(data[i].degree) !== "undefined"){
							
							showHtml += data[i].degree;
						}else{
							showHtml += "不详";
						}
						showHtml += "-";
						if(typeof(data[i].year) !== "undefined"){
							
							showHtml += data[i].year;
						}else{
							showHtml += "不详";
						}
						showHtml += "</li>";
						 $("#eduBgShow").append(showHtml);
						// $("#eduBgShow").append("<li>"+data[i].school+"－"+data[i].college+"－"+data[i].major+"－"+data[i].degree+"－"+data[i].year+"</li>");
						
					}
				}
			}

			var timeJobShow = function(data) {

				if(data.length>0){
					for (var i = 0; i < data.length; i++) {
						$("#timeJobShow").append("<li>"+data[i].company+"－"+data[i].title+"－"+data[i].startMonth+"-"+data[i].stopMonth+"</li>");
					}
				}
			}

			var projectShow = function(data){
				if(data.length>0){
					for (var i = 0; i < data.length; i++) {
						if(data[i].descp=="undefined"){
							$('.spancon').css({display:"none"});
						}  
						$("#projectShow").append("<li>名称："+data[i].name+"<br><span class='spancon'>内容："+data[i].descp+"</span></li>");
					}  
				}
			}

			var paperShow = function(data){
				if(data.length>0){
					for (var i = 0; i < data.length; i++) {
						$("#paperShow").append("<li>"+data[i].name+"</li>");
						
					}
				}
			}

			var patentShow = function(data){
				if(data.length>0){
					for (var i = 0; i < data.length; i++) {
						$("#patentShow").append("<li>"+data[i].name+"</li>");
						
					}
				}
			}

			var honorShow = function(data){
				if(data.length>0){
					for (var i = 0; i < data.length; i++) {
						$("#honorShow").append("<li>"+data[i].name+"</li>");
						
					}
				}
			}
			var researchAreaShow = function ($datas,$datarecords){
				if($datas != undefined &&  $datas.length != 0 ){
					for(var i =0 ; i< $datas.length;++i){
						var $data = $datas[i];
						var $photos = [];
						//获取头像
						if($datarecords.length>0){
							$photos = getRecords($datarecords,$data.caption);
						}
						var isAgree = $.inArray(userid, $photos);

						var showDiv= "<div class='listbox listbox-browse clear'><div class='list-browse'><span class='like'>"+$data.count+"</span>"+$data.caption+"</div><span class='plus' data-pid='"+$data.professorId+"' data-caption='"+$data.caption+"' data-isagree='"+isAgree+"'></span><div class='like-num'>";
						if($photos.length>10){
							showDiv += "<div class='triangleR'></div>";
						}
						
						if($photos.length>0){
							for(var j =0 ; j< $photos.length;++j){
								showDiv += "<span class='like-people'><img class='like-h' src='/images/head/"+$photos[j]+"_s.jpg'></span>";
								
							} 
						}
						showDiv += "</div></div>";
						$("#researchAreaShow").append(showDiv);
						
						$(".like-h").load(function(){								
						})
						.error(function(){
							$(this).attr("src","/images/default-photo.jpg");
						});	
							
					}
				}			
			}
			var getRecords = function ($researchAreaLogs,caption){
			     var ret=[];
			     for(var i = 0 ;i < $researchAreaLogs.length ; i++){
			     	if(caption==$researchAreaLogs[i].caption){
			     		ret.push($researchAreaLogs[i].opreteProfessorId);
			     	}
			     }
			     return ret;
			}


			$("#researchAreaShow").on("click",".plus",function(){
				if(userid && userid != null && userid != "null"){
					$.ajax({
						"url" : $(this).data("isagree")>-1?"/ajax/researchArea/unAgree":"/ajax/researchArea/agree",
						"type" : "POST",
						"data" : {"targetId":$(this).data("pid"),"targetCaption":$(this).data("caption"),"opId":userid},
						"contentType" : "application/x-www-form-urlencoded",
						"success" : function($data) {
							if ($data.success) {
								$.get("/ajax/professor/info/" + professorId, function($data) {
					    			if ($data.success) 
					    			{
										var $info = $data.data;
										if ($info) 
										{
											$("#researchAreaShow").empty("")
											if ($info.researchAreas)
											{
												researchAreaShow($info.researchAreas,$info.researchAreaLogs);
											}
										}
										
									}	
									})
							} else {
								$.MsgBox.Alert("message",$data.msg);
							}
						}
					});
				}else{
					$.MsgBox.Alert("消息","请登录后点赞");
					/*location.href="login.html"*/
				}
				
			})

	    	$.get("/ajax/professor/info/" + professorId, function($data) {
	    			if ($data.success) {
						var $info = $data.data;
						if ($info) {
							//展示专家的信息
							console.log($data);
							if($info.authentication==false) {
								$(".modify").css("background-image","url(../images/icon-unauth.png)");
							}
							$("#nameS").text($info.name);
							if($info.office) {
								$("#office").text($info.office+"，");
							}
							if($info.title) {
								$("#titleS").text($info.title);	
							}
							if($info.department) {
								$("#industryS").text($info.department+"，");
							}
							if($info.orgName) {
								$("#orgNameS").text($info.orgName);
							}
							if($info.address) {
								$("#address").text($info.address);
							}																			
							$("#headImage").attr("src","/images/head/"+professorId+"_l.jpg");
								$("#headImage").load(function(){									
								})
								.error(function(){
									$("#headImage").attr("src","/images/default-photo.jpg");
								});
							$("#orgImage").attr("src","/images/org/"+$info.orgId+".jpg");
								$("#orgImage").load(function(){									
								})
								.error(function(){
									$("#orgImage").attr("src","/images/default-icon.jpg");
								});
							$("#descpS").text($info.descp);
							//显示专家信息到保存数据
							name=$info.name;
							$("#name").val($info.name);
							$("#title").val($info.title);
							$("#orgId").val($info.orgId);
							//$("#organ").attr("href","organization-brow.html?orgId="+$info.orgId+"");
							//console.log($info.orgId);
							$("#orgName").val($info.orgName);
							$("#department").val($info.department);														
							if($info.descp==undefined&&$info.subject==undefined&&$info.researchAreas.length ==0&&$info.industry==undefined&&$info.edus.length ==0&&$info.jobs.length ==0&&$info.projects.length==0&&$info.papers.length ==0&&$info.patents.length ==0&&$info.honors.length ==0){								
								$("div:contains('专家信息')").parents(".menublock").hide();
							}
							if($info.descp){
								$("#descp").val($info.descp);
							}else{
								$("span:contains('个人简介')").parents(".introduction").hide();
								$("a:contains('个人简介')").hide();
							}
							
							//展示专家的学科							
							if($info.subject){
								subjectShow($info.subject);
							}else{
								$("span:contains('学术领域')").parents(".introduction").hide();
								$("a:contains('学术领域')").hide();
							}
							
							//研究方向						
							if ($info.researchAreas.length) {
								researchAreaShow($info.researchAreas,$info.researchAreaLogs);
							}else{							
								$("span:contains('研究方向')").parents(".introduction").hide();
								$("a:contains('研究方向')").hide();
							}
							//展示专家的行业							
							if($info.industry){
								industryShow($info.industry);
							}else{
								$("span:contains('应用行业')").parents(".introduction").hide();
								$("a:contains('应用行业')").hide();
							}
							
							//教育北京						
							if ($info.edus.length) {
								eduBgShow($info.edus);
							}
							else{
								$("span:contains('教育背景')").parents(".introduction").hide();
								$("a:contains('教育背景')").hide();
							}
							//兼职
							if ($info.jobs.length) {
								timeJobShow($info.jobs);
							}else{
								$("span:contains('社会兼职')").parents(".introduction").hide();
								$("a:contains('社会兼职')").hide();
							}
							//项目
							if ($info.projects.length) {
								projectShow($info.projects)
							}else{
								$("span:contains('项目经历')").parents(".introduction").hide();
								$("a:contains('项目经历')").hide();
							}

							//论文
							if ($info.papers.length) {
								paperShow($info.papers);
							}else{
								$("span:contains('著作、论文、文章')").parents(".introduction").hide();
								$("a:contains('著作、论文、文章')").hide();								
							}
							//专利
							if ($info.patents.length) {
								patentShow($info.patents);
							}else{
								$("span:contains('专利')").parents(".introduction").hide();
								$("a:contains('专利')").hide();	
							}
							//荣誉
							if ($info.honors.length) {
								honorShow($info.honors);
							}else{
								$("span:contains('荣誉及奖项')").parents(".introduction").hide();
								$("a:contains('获奖')").hide();	
							}
							 
					//下滑滚动不覆盖底部 
							var d;
							var t;
							$(function(){
							/*向下滚动时，header背景变半透明*/
							var top5=$(".content-left").height();
						
							var top4=$("#container").height();				
							var top3=$(window).height();						
							$(document).scroll(function(){
							var top1 =$(document).scrollTop();	
							var top2=$("#container").height()-top3+(top3-80-top5)-10;//变成绝对定位	
							if(top1>=300){
										$(".content-left").css({"position":"fixed","top":"80px"});
										if(top1>=top2)
									{
										$('.information-content').css("position","static");
										$(".content-left").css("position", "absolute");		
										$(".content-left").css("top", top4-top5-10+"px");													
									}
									else{
											$('.information-content').css("position","relative");
											$(".content-left").css({"position":"fixed","top":"80px"});	
									}	
								}
							else{
							$(".content-left").css({"position":"static"});	
							
							}
							
							})	
							});
							//下滑滚动不覆盖底部 结束
						}
					}
				}
			);
	    }
		$("#hsearch").on("click",function (){
	   		var searchContent = $("#hsearchContent").val();
	   		location.href = "search.html?searchContent="+searchContent;
	  });



//进入自己的页面，上传图像部分
$(".head-left").hover(function(){
	$(".replace-photo").animate({"bottom":"0px"});						   						   
	},function(){
	$(".replace-photo").animate({"bottom":"-50px"});	
		
		})
//专家信息点击换背景颜色
$(".subsidebar").click(function(){
	$(".subsidebar").removeClass("subcolor");
	$(".subsidebar").eq($(".subsidebar").index(this)).addClass("subcolor");						  
});
//拿到点击的顺序，然后求出局文档的距离
$(".subsidebar").click(function(){
	var index=$(".subsidebar").index(this);
	var offset=$(".introduction").eq(index).offset();

		$("body").animate({ 
			scrollTop:offset.top-80 //��body��scrollTop����pos��top����ʵ���˹��� 
			},1000); 
		 
		 })
var resou;
//科研资源
	$.ajax({		
		"url":"/ajax/resource/qapro",
		"type":"get",
		"data":{"professorId":professorId},
		"async": false,
		"success":function(data){
//			console.log(data);
			resou=data.data.length;
			if(data.data.length==0){
				$("#infor-browse").css("display","none");
				$(".resoubtn").css("display","none");
				$("span:contains('科研资源')").parents(".introduction").hide();
				$("a:contains('科研资源')").hide();
			}
			if(data.data.length > 0 && data.data.length <= 3){
				$("#infor-browse").css("display","block");
				$(".resoubtn").css("display","none");
			}
			var add="";
			if(data.success)
				{
					for(var i=0;i<data.data.length;i++){
						add='<div class="sharesrc">'
						add+='<div class="shareshow">'
	                	add+='<a class="remess" resourceId='+data.data[i].resourceId+'><img src="/images/resource/'+data.data[i].resourceId+'.jpg" width="100%" height="100%" class="resourceImg"></a>'
	                 	add+='<div class="shareopeart"><a class="sharebtn resourceConsult" professorId='+data.data[i]["professorId"]+'>咨询</a><a class="sharebtn applicant">申请</a></div>'
	             		add+='</div>'
	             		add+='<p>资源名称：<span class="presou consultTitleVal" resourceConsultTitle = "">'+ data.data[i].resourceName+'</span></p>'
	             		add+='<p class="briefinfor">简介:'+data.data[i].supportedServices+'</p>'
	        			add+='</div>'
	        			$("#sharescrollbox").append(add);
						$('.applicant').eq(i).attr("resourceId",data.data[i].resourceId);
						
						$(".resourceImg").eq(i).attr("src","/images/resource/"+data.data[i].resourceId+".jpg");
						$(".resourceImg").load(function(){})
							.error(function(){
								$(this).attr("src","/images/default-resource.jpg");
							});	
						
						add="";
					}
					//资源轮播
					var $content = $(".sharescrollbox");
					var childcount = $content.find(".sharesrc").length; 
					Carousel(3,3, childcount, $content, $(".resounext"), $(".resouprev"));
					//科研资源中点咨询
					$(".introduction").find(".resourceConsult").bind("click",clickResourceConsult);
					
					function Carousel(inde, num, childcount, obj, next, prev) {
						next.click(function() {	
							if (!obj.is(":animated")) { 
								if (num < childcount) { 
								
									num++;
									obj.animate({
										left: "-=212px"
									}, 600);
								}
							}
						});
						prev.click(function() {
							if (!obj.is(":animated")) {
								if (num > inde) { 
									num--;
									obj.animate({
										left: "+=212px"
									}, 600);
								}
							}
						});
					}
				}
				else
				{
					$.MsgBox.Alert("消息","系统异常!");
				}
		},
		"error":function(){$.MsgBox.Alert('message','failddd')}
	});
	
	function getIdentity(professorId){		
		$.ajax({	
		"url":"ajax/professor/"+professorId,
		"type":"get",
		"async": false,
		"success":function(data){
			if(data.success)
			{																	
				//console.log(data);
				$("option")[0].value=data.data.name;
				$("option")[1].value=data.data.organization.name;
				//console.log($("option")[0].value);
				//console.log($("option")[1].value);
			}
			else
			{
				$.MsgBox.Alert("消息","系统异常!");
			}
		},
		"error":function(){$.MsgBox.Alert('message','failddd')}
		})	
	}
	//查看资源详细信息
	$('#sharescrollbox').on("click",'.remess',function(){
		var resourceID=$(this).attr("resourceId");
		ResourceMessage();
		$.ajax({		
			"url":"/ajax/resource/"+resourceID,
			"type":"get",
			"async": true,
			"success":function(info){
				if(info.success)
					{
					console.log(info);
					$("#resourceName").text(info.data.resourceName);
					$("#supportedServices").text(info.data.supportedServices);
					$("#hopePayMethod").text(info.data.hopePayMethod) ;
					$("#cooperationNotes").text(info.data.cooperationNotes) ;
					$("#subject").text(info.data.subject);
					$("#industry").text(info.data.industry);
					$("#descp").text(info.data.descp);
					$("#professor").text(name);
					$(".resouImage").attr("src","/images/resource/"+resourceID+".jpg");
					$(".resouImage").load(function(){})
						.error(function(){
							$(this).attr("src","/images/default-resource.jpg");
						});
					}
					else
					{
						$.MsgBox.Alert('message',"系统异常!");
					}
			},
			"error":function(){$.MsgBox.Alert('message','failed')}
		});  
	})
//资源申请入口
	$('.introduction').on("click",'.applicant',function(){
		if(userid && userid != null && userid != "null"){
			ResourceApply();//调用资源申请HTML创建函数
			$(".limitBox").removeClass("limitBox");
	        $(".limitwords").removeClass("limitwords");
	        $(".limitwordbox").removeClass("limitwordbox");
			//获取当前时间并且格式化时间
			var myDate = new Date();
			var nowYear = myDate.getFullYear();
			var nowMonth = myDate.getMonth()+1;
			var nowDate = myDate.getDate();
			if(nowMonth<10){
				nowMonth="0"+nowMonth.toString();
			}
			if(nowDate<10){
				nowDate="0"+nowDate.toString();
			}
			var now = nowYear + "-" + nowMonth + "-" + nowDate;
			$( "#datepicker").val(now);					
			$( "#datepicker" ).datepicker({ "dateFormat":" yy-mm-dd"});			
			$(".timelo").css("display","none");
			$(".namecon").text(name);
			$(".applysure").hide();
			$(".btnboxup").hide();
			var index1=$(this).parent().parent().parent().index();
			var resName=$('.presou').eq(index1).text();					
			var resourceID=$(this).attr("resourceId");			
			$.ajax({		
				"url":"/ajax/resource/"+resourceID,
				"type":"get",
				"async": false,
				"success":function(info)
				{
					if(info.success)
					{						
						if(info.data.images.length)
						{
							$(".resouImage").attr("src","/images/resource/"+resourceID+".jpg");
						}
						else{
							$(".resouImage").attr("src","/images/default-resource.jpg");
						}
					}
				}
		})					
			$(".log").text(resName);			
			getIdentity(userid);			
			$("#deliver").on("click",function()
					{	
				
					var $data = {};
					$data.demandId = userid;
					$data.resourceId=resourceID;
					$data.detailDemand=$(".detail").val();
					//去除时间里面的-  和字符串前后的空格
					$data.deliverTime=replaceStr($.trim($(".dealTime").val()));
				    //alert(replaceStr($.trim($(".dealTime").val())));
				    //alert(replaceStr($.trim($(".dealTime").val())).length);
					
					$data.operationStatus=1;
					$data.applysquare=$("select option:selected").val();	
					//console.log($data.applysquare);
					
					
					$.ajax({
						"url" : "/ajax/operation",
						"type" : "POST",
						"success" : function(rdata) {
							if (rdata.success) {
								$.MsgBox.Alert('message','资源"'+ resName +'"申请发送成功!');
								$(".blackcover").remove();
								$("body").css("position","static");
								//location.reload(true);
							} else {
								$.MsgBox.Alert('message','资源"'+ resName +'"申请发送失败，请重新申请!');
							}
						},
						"data" :  $data,
						"beforeSend":function(){/*console.log(this.data)*/},
						"contentType" :  "application/x-www-form-urlencoded",
						dataType : "json"
					});
			});
		}else{
			$.MsgBox.Alert("消息","您还未登录，请登录之后进行资源申请");
		}	
	});
	
	//科研资源中的咨询
	function clickResourceConsult(){
		var professorId = $(this).attr("professorId");
		consultHandler();
		
		$("ul.menucon").children().eq(1).addClass("clicknow");
		var consultTitleVal = $(this).parent().parent().parent().find(".consultTitleVal").text();
		$("#consultTitle").val("关于"+consultTitleVal+"的咨询");
		
	};
	


	//===============点击咨询==============
	var userid = $.cookie("userid");//登陆人id====重复代码
 	var professorId = GetQueryString("professorId");//专家id====重复代码
	//点击科研资源的咨询
 	$("#consultbtn,#conbtn").bind('click',consultHandler);	
	

 	
	function consultHandler() {		
			if( userid=="null"){
			$.MsgBox.Alert("message","请先登录");
			location.href="login.html";
		}
		ConsultApply();
		//点击的发送
		$("#sendConsultBtn").click(function(){
			sendConsultHandler($(this).attr("proId"));
		});
		
		if(userid && userid != null && userid != "null"){
			concultProInfo(professorId);
			
		}else{
			$.MsgBox.Alert("message","请登录");
		}
	};
	//合作历史及评价
	var ass;
	$.ajax({		
		"url":"/ajax/consult/pqAssessHis",
		"type":"get",
		"data":{"professorId":professorId},
		"async": false,
		"success":function(response) {		
			if(response.success) {				
				var $data=response.data.data;
				ass=$data.length;
				if($data.length==0) {
					$("span:contains('合作历史及评价')").parents(".introduction").hide();
					$("a:contains('合作历史及评价')").hide();
				}
				for(var i=0;i<$data.length;i++) {									
				var add="";								
				 var assessTime=$data[i]["assessTime"].substr(0,4) + "-" +  $data[i].assessTime.substr(4,2) + "-" +  $data[i].assessTime.substr(6,2) + " "
				+  $data[i].assessTime.substr(8,2)+ ":" + $data[i].assessTime.substr(10,2);
				add+='<div class="evaluate">'
                	add+='<div class="lefteva">'
                      add+='<a  class="evaimg">'
                    if($data[i].professor.hasHeadImage) {
                    	add+='<img src="/images/head/'+$data[i].professor.id+'_m.jpg" width="100%" height="100%">'
                    }else{
                    	add+='<img src="/images/default-photo.jpg" width="100%" height="100%">'
                    }
                      
                      add+='</a>'
                      add+='<p>'+$data[i].professor.name+'</p><p>'
//                      if($data[i].professor.office) {
//                      	 add+='<span>'+$data[i].professor.office+' ，</span>'
//                      }
//                     if($data[i].professor.title) {
//                     	 add+='<span>'+$data[i].professor.title+'</span>'
//                     }                     
                      add+='</p><p>'
//                      if($data[i].professor.department) {
//                      	 add+='<span>'+$data[i].professor.department+' ，</span>'
//                      }
//                     if($data[i].professor.orgName) {
//                     		add+='<span>'+$data[i].professor.orgName+'</span>'
//                     }                            
                      add+='</p>'
                      add+='</div>'
                      add+='<div class="righteva">'
                      add+='<ul class="evasort">'                     
                      add+='<li>咨询时间：<span>'+assessTime+'</span></li>'
                      add+='<li>咨询主题：<span>'+$data[i].consultTitle+'</span></li>'
                      add+='</ul>'                      
                      add+='<div class="evastarbox" id="evastarboxx">'
                           add+='<span class="evastar"></span>'
                           add+='<span class="evastar"></span>'
                           add+='<span class="evastar"></span>'
                           add+='<span class="evastar"></span>'
                           add+='<span class="evastar"></span>'
                      add+='</div>'
                      if($data[i].assessContant){
                      	 add+='<p class="evabrief">'+$data[i].assessContant+' </p>'
                      }                             
                      add+='</div>'
                add+='</div>'
                	var $add=$(add);
                $(".shareabout").append($add);
                 $add.find(".evastar:lt("+$data[i].assessStar+")").addClass('staspan');     
               }
			}
		}
})	
if(resou==0&&ass==0){
	$('div:contains("合作机会")').parents(".menublock").hide();
}
})

