$(function(){
	    var newc=GetQueryString("lookFor");
	    console.log(newc);
	    if(newc==0||newc==""){
	       var c=0;	
	    }else{
	       var c=1;	
	       $(".lookwho option[value='1']").attr("selected","selected");
	    }
		var userid = $.cookie("userid");
		var  subject;
		var industry;
		var pageSize;
		var sub="PRO_SUBJECT";
		var indu="PRO_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/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) {							
						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'><img class='images' style='width:100%;'  src='/images/head/"+$data.data.data[i].id+"_l.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(typeof($data.data.data[i].title) !== "undefined"){
									dataHtml += $data.data.data[i].title;
								}
								dataHtml += ",";
								if(typeof($data.data.data[i].department) !== "undefined"){
									dataHtml += $data.data.data[i].department;
								}
								dataHtml += ",";
								dataHtml += $data.data.data[i].organization.name;
								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(typeof($data.data.data[i].address) !== "undefined"){
									dataHtml +=	"<p>"+$data.data.data[i].address+"</p>";
								}
					
								dataHtml += "</div>";
								dataHtml += "</div>";	
								dataHtml += "<div class='operbox'><span class='operbtn consultbtn'>咨询</span></div>";
								
								$("#professorList").append(dataHtml);
								
								
								//获取头像		
								$(".images").load(function(){								
								})
								.error(function(){
									$(this).attr("src","/images/default-photo.jpg");
								});	
								
								
								//点击查看当前信息
								(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);

							}
							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},
 					"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" href=""><img src="" class="resourceImg" width="100%" height="100%"></a>'
 			                    add+='<div class="synopsis">'
 			                    add+='<div><a class="resouname" href="">'+$da[i].resourceName+'</a></div>'
 			                    add+='<div class="belongname">'+$da[i].professor.name+'<img class="modicon" src="images/rz.png" width="28"></div>'
 			                    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">咨询</span><span class="operbtn applybtn appl" resourceId='+$da[i].resourceId+' resourceName='+$da[i].resourceName+' professor='+$da[i].professor.name+'>申请</span></div>'
 			                	add+='</div>'
 			                	$("#professorList").append(add);
 			                    
 			                   $(".resourceImg").eq(i).attr("src","/images/resource/"+$da[i].resourceId+".jpg");
 								$(".resourceImg").load(function(){})
 									.error(function(){
 										$(this).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","请登录");
 						}
 					},				
 				});	
 			}
			
 		}
		
 		var subjectList = function (){
 			$.get("../ajax/dataDict/"+sub,function(data){ 			
    			if(data.success){
    				$("#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(i>20)
    						break;
    				}
    			}else{
    				$.MsgBox.Alert("message",data.msg);
    			}
    		})
 		}
 		var Address = function (){
 			$.get("../ajax/dataDict/PRO_ADDRESS",function(data){ 			
    			if(data.success){    				
    				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){
    				$("#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 (){			
			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();
//		}
//		
//	})
	
	 //判断是搜寻专家还是资源
	 $(".floatL").change(function(){
			c=$(".floatL option:selected").val();
			$("#industry").find(".no2").addClass("colorbg").siblings().removeClass("colorbg");
			$("#subject").find(".nox").addClass("colorbg").siblings().removeClass("colorbg");
			key=$("#searchContent").val();
			industry="";
			subject="";
		if(c==0){
			sub="PRO_SUBJECT";
			indu="PRO_INDUSTRY";
			$(".class1").eq(2).show();
		}
		else{			
			sub="RES_SUBJECT";
			indu="RES_INDUSTRY";
			$(".class1").eq(2).hide();
		}		
		industryList();
		subjectList();		
			$("#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);
});
	 //资源申请
	 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创建函数
			//获取当前时间并且格式化时间
			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");	
			
			$(".resouImage").attr("src","/images/resource/"+resourceID+".jpg");
			$(".resouImage").load(function(){})
				.error(function(){
					$(this).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 clickConsultFn(){
			ConsultApply();
			
			//点击的发送
			$("#sendConsultBtn").on("click",sendConsultHandler);
			var professorId = $(this).attr("id");
			if(userid && userid != null && userid != "null"){
				$.ajax({
					"url" :"/ajax/professor/"+ professorId,
					"type" : "get",
					"data" : {"professorId":professorId},
					"contentType" : "application/x-www-form-urlencoded",
					"success" : function(response) {
						if (response.success) {
							console.log(response);
							$("#professorName").text(response.data["name"]);
							$("#professorName").attr("professorId",response.data["id"]);
							$("#professorTitle").text(response.data["title"]);
							
							if(response.data["department"] == "" || response.data["department"] == null){
								$("#profOrganization").text(response.data["organization"]["name"]);
							}else{
								$("#profDepartment").text(response.data["department"]+",");
								$("#profOrganization").text(response.data["organization"]["name"]);
								
							}
							if(response.data["adress"] == "" || response.data["adress"] == null ){
								$("#adress").html("");
							}else{
								$("#profAdress").text(response.data["adress"]);
							}

							
						} else {
							$.MsgBox.Alert("message","请登录");
						}
					},
					"complete":function(){
						
					}
				});
			}
		
	 };
	 
	 function sendConsultHandler(){
		 	var professorId = $("#professorName").attr("professorId");
			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",
					"beforeSend":function(){console.log(this.data)},
					"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("消息","咨询申请成功");
					},
				});
			}

		}
	 
	 
});
 