$(function(){
	    var newc=GetQueryString("lookFor");
	    if(newc==0||newc==""){
	       var c=0;	
	    }else{
	       var c=1;	
//	       $(".lookwho option[value='1']").attr("selected","selected");
	       $("#div_select").text("找资源");
	    }
		var userid = $.cookie("userid");
		var  subject;
		var subb=$.trim(GetQueryString("subject"));
		var industry;
		var pageSize;
		var sub="SUBJECT";
		var indu="INDUSTRY";
		var address= $("#address .colorbg")[0].title;
		$("#searchContent").val(GetQueryString("searchContent")); 
		$("#hsearchContent").val(GetQueryString("searchContent")); 
		var key=$("#searchContent").val();
 		var getData = function (key,subject,industry,pageSize,pageNo,isbind,add)
 		{ 			
 			if(c==0)
 			{
 				$.ajax({"url" : "/ajax/professor/pqBaseInfo",
					"type" : "GET",
					"data" : {"key":key,"subject":subject,"industry":industry,"pageSize":pageSize,"pageNo":pageNo,"address":add},
					"contentType" : "application/x-www-form-urlencoded",
					"success" : function($data) {							
						if ($data.success) {
							console.log($data);
							$("#professorList").html("");
							for(var i = 0 ; i < $data.data.data.length ; i++){
								//创建HTML标签  
								var dataHtml = "<div class='item'>";									
								dataHtml += "<a  class='infoi'  href='information-brow.html?professorId="+$data.data.data[i].id+"' ><div class='photo1'>"
								if($data.data.data[i].hasHeadImage) {
									dataHtml += "<img class='images' style='width:100%;'  src='/images/head/"+$data.data.data[i].id+"_l.jpg'/></div></a>";
								}
								else {
									dataHtml += "<img class='images' style='width:100%;'  src='/images/default-photo.jpg'/></div></a>";
								}
								dataHtml += "<div class='synopsis'>";
								dataHtml += "<a href='information-brow.html?professorId="+$data.data.data[i].id+"' class='named'>";
								if(typeof($data.data.data[i].name) !== "undefined"){
									dataHtml += $data.data.data[i].name;
								}
								dataHtml += "</a><a href='information-brow.html?professorId="+$data.data.data[i].id+"' class='modify'></a>";
								dataHtml += "<div class='position'>";
								if($data.data.data[i].office){
									dataHtml += $data.data.data[i].office+", ";
								}
								if($data.data.data[i].title){
									dataHtml += $data.data.data[i].title+", ";
								}								
								if($data.data.data[i].department){
									dataHtml += $data.data.data[i].department+", ";
								}
								if($data.data.data[i].orgName){
									dataHtml += $data.data.data[i].orgName;
								}	
								if($data.data.data[i].address){
									dataHtml += " | "+$data.data.data[i].address;
								}
								dataHtml += "</div>";
								dataHtml += "<div class='science'>";
								if(typeof($data.data.data[i].subject) !== "undefined"){
									dataHtml += $data.data.data[i].subject
								}
								dataHtml += "</div>";
								dataHtml += "<div class='industry'>";
								if(typeof($data.data.data[i].industry) !== "undefined"){
									dataHtml += $data.data.data[i].industry;
								}
								if($data.data.data[i].researchAreas.length ){								
									dataHtml +=	"<p>"+$data.data.data[i].researchAreas[0].caption+"</p>";
									console.log($data.data.data[i].researchAreas[0].caption);
								}
					
								dataHtml += "</div>";
								dataHtml += "</div>";
								if($data.data.data[i].id!=userid){
									dataHtml += "<div class='operbox'><span class='operbtn onlyConsultbtn' id='"+$data.data.data[i]['id']+"'>咨询</span></div>";
								}
								
								
								$("#professorList").append(dataHtml);
								if($data.data.data[i].authentication==false) {
									$(".modify").eq(i).css("background-image","url(../images/icon-unauth.png)")
								}
								//点击查看当前信息
								(function(i){
									$('.photo1').click(function()
								{
									if(userid==$data.data.data[i].id)
									{
										$(".infoi").attr("href","information.html");	
									}										
								})
								}
									
								)(i);
								//点击查看当前信息
								(function(i){
									$('.named').click(function()
								{
									if(userid==$data.data.data[i].id)
									{
										$(".named").attr("href","information.html");	
									}										
								})
								}
									
								)(i);

							}
							//找专家点击咨询
							$(".onlyConsultbtn").bind("click",clickSeachProConsultHandler);
							
							if(isbind == true){
								$(".tcdPageCode").createPage({
							        pageCount:Math.ceil($data.data.total/pageSize),
							        current:$data.data.pageNo,
							        backFn:function(p){
							        	getData(key,subject,industry,pageSize,p,false);
							        }
							    });
							}
							
						} else {
							$.MsgBox.Alert("message",$data.msg);
						}
					}
				});
 			}
 			else
 			{
 				//查询我的所有资源
 				$.ajax({
 					"url" :"/ajax/resource/pq",
 					"type" : "get",	
 					"data" : {"key":key,"subject":subject,"industry":industry,"pageSize":pageSize,"pageNo":pageNo,"address":add},
 					"contentType" : "application/x-www-form-urlencoded",
 					"success" : function(data) 
 					{console.log(data);
 						if (data.success) 
 						{
 							$("#professorList").html("");	
 							var $da=data.data.data;
 							for(var i=0;i<$da.length;i++)
 							{
 								var add='<div class="item">'
 			                    add+='<a class="resourcephoto" ><img src="" class="resourceImg remess" width="100%" height="100%" resourceId='+$da[i].resourceId+'></a>'
 			                    add+='<div class="synopsis">' 			                  
 			                     add+='<div class="cousultName"><a class="resouname" href="">'+$da[i].resourceName+'</a></div>'
 			                     if($da[i].professor.name){
 			                    	 add+='<div class="belongname">'+$da[i].professor.name+'<img class="modicon" src="images/onauth.png" width="20" ></div>'  
 			                    		  } 			                   
 			                   if($da[i].professor.title==undefined){
 			                	  $da[i].professor.title="";
 			                   }
 			                  if($da[i].professor.department==undefined){
 			                	 $da[i].professor.department="";
 			                   }
 			                 if($da[i].professor.orgName==undefined){
 			                	$da[i].professor.orgName="";
			                   }
 			                    add+='<div class="positionbox">'+$da[i].professor.title+$da[i].professor.department+$da[i].professor.orgName+'</div>'
 			                    if($da[i].subject)
 			                    {
 			                    	add+='<div class="sciencebox">'+$da[i].subject+'</div>'
 			    				}	
 								if($da[i].industry)
 								{
 									add+='<div class="industrybox">'+$da[i].industry+'</div>'
 								}	
 								if($da[i].supportedServices)
 								{
 									 add+='<div class="provideservice">可提供服务：'+$da[i].supportedServices+'</div></div>' 
 								}	                                              
 			                    add+='<div class="operbox"><span class="operbtn consultbtn" id="'+$da[i]["professor"]["id"]+'">咨询</span><span class="operbtn applybtn appl" resourceId='+$da[i].resourceId+' resourceName='+$da[i].resourceName+' professor='+$da[i].professor.name+'>申请</span></div>'
 			                	add+='</div>'
 			                		var $add=$(add);
 			                	$("#professorList").append($add);			                	
 			                	if($da[i].professor.authentication==false) {
 			                		$add.find(".modicon").attr("src","images/icon-unauth.png");
								}
 			                   if(data.data.data[i].images.length)
 								{
 								$add.find(".resourceImg").attr("src","/images/resource/"+$da[i].resourceId+".jpg");
 								}
 								else{
 									$add.find(".resourceImg").attr("src","/images/default-resource.jpg");
 								}			                 
 							}
 							if(isbind == true){
								$(".tcdPageCode").createPage({
							        pageCount:Math.ceil(data.data.total/pageSize),
							        current:data.data.pageNo,
							        backFn:function(p){
							        	getData(key,subject,industry,pageSize,p,false);
							        }
							    });
							}
 						}
 						 else
 						 {
 							$.MsgBox.Alert("message","请登录");
 						}
 						
 						//找资源中点击咨询
						$(".consultbtn").bind("click",clickResouceConsultHandler);
 					},				
 				});	
 			}
			
 		}
		
 		var subjectList = function (){
 			$.get("../ajax/dataDict/"+sub,function(data){ 			
    			if(data.success){
    				if(data.data.length <=10) {
    					$(".andmore").eq(0).hide();
    				} 
    				$("#subject").find(".sub").remove();
    				for(var i = 0 ; i < data.data.length; i++){
    					$("#subject").append("<a href='#' class='sub'title='"+data.data[i].caption+"'>"+data.data[i].caption+"</a>");	
                         if(subb) {
    						if(subb==data.data[i].caption) {
    							$('.sub').eq(i).addClass('colorbg').siblings().removeClass("colorbg");
    						}
    					}
    					if(i>20)
    						break;
    				}
    			}else{
    				$.MsgBox.Alert("message",data.msg);
    			}
    		})
 		}
 		var Address = function (){
 			$.get("../ajax/dataDict/ADDRESS",function(data){ 			
    			if(data.success){  
    				if(data.data.length <=10) {
    					$(".andmore").eq(2).hide();
    				} 
    				for(var i = 0 ; i < data.data.length; i++){
    					$("#address").append("<a href='#' title='"+data.data[i].caption+"'>"+data.data[i].caption+"</a>");	
    					if(i>20)
    						break;
    				}
    			}else{
    				$.MsgBox.Alert("message",data.msg);
    			}
    		})
 		}
 		var industryList = function (){
 			$.get("../ajax/dataDict/"+indu,function(data){ 			
    			if(data.success){
    				if(data.data.length <=10) {
    					$(".andmore").eq(1).hide();
    				} 
    				$("#industry").find(".indu").remove();
    				for(var i = 0 ; i < data.data.length; i++){
    					$("#industry").append("<a href='#' class='indu'title='"+data.data[i].caption+"'>"+data.data[i].caption+"</a>");	
    					if(i>20)
    						break;
    				}
    			}else{
    				$.MsgBox.Alert("message",data.msg);
    			}
    		})
 		}		
		var findProfessor = function (){			
			if(subb){
				subject=subb;
			}else{
				subject = $("#subject .colorbg")[0].title;
			}
			industry = $("#industry .colorbg")[0].title;
			pageSize = 4;
			getData(key,subject,industry,pageSize,1,true,address);
		}
		$("#search1").on("click",function(){
			$("#hsearchContent").val($("#searchContent").val());
			key = $("#searchContent").val();
			$("#professorList").remove();
			$(".aboutpro").append('<div class="about-in" id="professorList"></div>')
			$(".tcdPageCode").remove();
			$(".aboutpro").append('<div class="tcdPageCode"></div>');
			getData(key,subject,industry,4,1,true,address);
		})
		$("#hsearch1").on("click",function(){
			$("#searchContent").val($("#hsearchContent").val());
			key = $("#hsearchContent").val();
			$("#professorList").remove();
			$(".aboutpro").append('<div class="about-in" id="professorList"></div>')
			$(".tcdPageCode").remove();
			$(".aboutpro").append('<div class="tcdPageCode"></div>');
			getData(key,subject,industry,4,1,true,address);
		})

		subjectList();
 		industryList();
		findProfessor();
		Address();
		$(".class1").eq(0).on("click","a",function (){
			$(this).addClass("colorbg").siblings().removeClass("colorbg");
			key=$("#searchContent").val();
			subject = $(this).attr('title');	
			$("#professorList").remove();
			$(".aboutpro").append('<div class="about-in" id="professorList"></div>')
			$(".tcdPageCode").remove();
			$(".aboutpro").append('<div class="tcdPageCode"></div>');
			getData(key,subject,industry,4,1,true,address);
			
		});	
		$(".class1").eq(1).on("click","a",function (){
			$(this).addClass("colorbg").siblings().removeClass("colorbg");
			industry = $(this).attr('title');
			key=$("#searchContent").val();
			$("#professorList").remove();
			$(".aboutpro").append('<div class="about-in" id="professorList"></div>')
			$(".tcdPageCode").remove();
			$(".aboutpro").append('<div class="tcdPageCode"></div>');
			getData(key,subject,industry,4,1,true,address);		
    });	
		$(".class1").eq(2).on("click","a",function (){
			$(this).addClass("colorbg").siblings().removeClass("colorbg");
			address = $(this).attr('title');
			key=$("#searchContent").val();
			$("#professorList").remove();
			$(".aboutpro").append('<div class="about-in" id="professorList"></div>')
			$(".tcdPageCode").remove();
			$(".aboutpro").append('<div class="tcdPageCode"></div>');
			getData(key,subject,industry,4,1,true,address);		
    });	
	/*搜索分类展开*/
	  var listi = 0;
	 $(".andmore").click(function() {
		$(this).find(".updown").css("background-position", "left");
		$(this).parent().css("height", "52px");
		if (listi % 2 == 0) {
	
		} else {
			$(this).find(".updown").css("background-position", "right");
			$(this).parent().css("height", "auto");
	
		}
		listi++;
	 })
	 
	/*搜索分类背景切换*/
	$(".class1").on("click","a",function (){
		$(this).addClass("colorbg").siblings().removeClass("colorbg");
	}) 
	
	/*专家、资源菜单切换*/
//	$(".lookwho").change(function(){
//		//$(this).addClass("tabcurrent").siblings().removeClass("tabcurrent");
//		//$(".aboutcon").eq($(this).index()).show().siblings().hide();
//		if($(this).value == "0"){
//			$(".aboutpro").show();
//			$(".aboutresou").hide();	
//		}
//		else{
//			$(".aboutresou").show();
//			$(".aboutpro").hide();
//		}
//		
//	})
	
	 //判断是搜寻专家还是资源
	 $(".lishow li").on("click",function(){
			c=$(this).index();
			
			$("#industry").find(".no2").addClass("colorbg").siblings().removeClass("colorbg");
			$("#subject").find(".nox").addClass("colorbg").siblings().removeClass("colorbg");
			key=$("#searchContent").val();
			industry="";
			subject="";
			address="";
		if(c==0){
			sub="SUBJECT";
			indu="INDUSTRY";
		}
		else{			
			sub="SUBJECT";
			indu="INDUSTRY";
			
		}		
		industryList();
		subjectList();
		Address();
			$("#professorList").remove();
			$(".aboutpro").append('<div class="about-in" id="professorList"></div>')
			$(".tcdPageCode").remove();
			$(".aboutpro").append('<div class="tcdPageCode"></div>');
			getData(key,subject,industry,pageSize,1,true,address);
});
	 //查询资源详细信息
	 $('.about').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)
						{						
						$("#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(info.data.professor.name);
						$(".resouImage").attr("src","/images/resource/"+resourceID+".jpg");
						$(".resouImage").load(function(){})
							.error(function(){
								$(this).attr("src","/images/default-resource.jpg");
							});
						limitHeight();
						}
						else
						{
							$.MsgBox.Alert('message',"系统异常!");
						}
				},
				"error":function(){$.MsgBox.Alert('message','failed')}
			});  
		})
	 //资源申请
	 function getIdentity(professorId){		
			$.ajax({	
			"url":"ajax/professor/"+professorId,
			"type":"get",
			"async": false,
			"success":function(data){
				if(data.success)
				{																						
					$(".frmtype option")[0].value=data.data.name;
					$(".frmtype option")[1].value=data.data.organization.name;					
				}
				else
				{
					$.MsgBox.Alert("消息","系统异常!");
				}
			},
			"error":function(){$.MsgBox.Alert('message','failddd')}
			})	
		}
	 function replaceStr(s) 
		{ 
			var pattern = new RegExp("-");
			var rs = ""; 
			for (var i = 0; i < s.length; i++) { 
				rs = rs+s.substr(i, 1).replace(pattern, ''); 
			} 
			return rs; 
		} 	
	 $('.aboutpro').on("click",'.appl',function(){		
			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");
			var resName=$(this).attr("resourcename");
			var name=$(this).attr("professor");
			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");
						}
					}
				}
		})							
			$(".namecon").text(name);
			$(".applysure").hide();
			$(".btnboxup").hide();																	
			$(".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()));	
						$data.operationStatus=1;
						$data.applysquare=$(".frmtype option:selected").val();																			
						$.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"
						});
				});
		});
	 
	 
	 //找专家中 点击咨询处理函数
	 function clickSeachProConsultHandler (){
		 
		var professorId = $(this).attr("id");
		ConsultApply();
		//点击的发送
		$("#sendConsultBtn").click(function(){
			sendConsultHandler($(this).attr("proId"))
		});
		
		if(userid && userid != null && userid != "null"){
			concultProInfo(professorId);
			
		}else {
			$.MsgBox.Alert("message","请登录");
		}
			
	 };
	 
	 
	 //找资源中 点击咨询
	 function clickResouceConsultHandler(){
		var professorId = $(this).attr("id");
		ConsultApply();
		//默认选中资源咨询和标题
		$("ul.menucon").children().eq(1).addClass("clicknow");
		var consultTitleVal = $(this).parent().prev().find("a").text();
		console.log($(this).parent());
		console.log($(this).parent().prev());
		alert(consultTitleVal);
		$("#consultTitle").val("关于"+consultTitleVal+"的咨询");
		//点击的发送
		$("#sendConsultBtn").click(function(){
			sendConsultHandler($(this).attr("proId"));
		});
		
		if(userid && userid != null && userid != "null"){
			concultProInfo(professorId);

		}else {
			$.MsgBox.Alert("message","请登录");
		}
		 
	 };

	 
	/* function sendConsultHandler(){
		 	var professorId = $(this).attr("proId");
			var consult_type = $(".clicknow").text();//咨询类型
			var consult_title = $("#consultTitle").val();//咨询主题
			var consult_content = $("#consultcontent").val();//咨询内容
			var consultStr = {
					"consultType":consult_type,
					"consultTitle":consult_title,
					"consultContant":consult_content,
					"professorId":professorId,
					"consultantId":userid
			};
			
			if(consult_type == '' || consult_title == '' || consult_content == ''){
				$.MsgBox.Alert("message","请填写完整");
			};
			if(consult_type == ''){
				$.MsgBox.Alert("消息","请选择联系目的");
			}
			if(consult_title == ''){
				$.MsgBox.Alert("消息","请填写咨询目的");
			}
			if(consult_content == ''){
				$.MsgBox.Alert("消息","请填写咨询内容");
			}
			
			if(userid && userid != null && userid != "null" && consult_type != '' &&
					consult_title != '' && consult_content != ''
			){
				
				$.ajax({
					"url" :"/ajax/consult",
					"type" : "post",
					//传值：咨询类型、主题、内容、专家id、申请人id
					"data" : consultStr,				
					"contentType" : "application/x-www-form-urlencoded",
					"dataType" : "json",
					"success" : function(response) {
						consultId = response.data;//咨询id
						
						
					},
					"error":function (){
						$.MsgBox.Alert("消息","咨询申请失败");
					},
					
					"complete":function(){
						//$(".consultapply").remove();
						$(".blackcover").remove();
						$("body").css("position","static");
						$.MsgBox.Alert("消息","咨询申请成功");
					},
				});
			}

		}*/
	 
	 
});
 