$(function(){
		   
	$(document).keydown(function(event){ 
		event = event || window.event;
		if(event.keyCode==13){	
			return false;
			};
		})
	//咨询列表奇偶显示样式
	$('.li_table>ul:gt(1):even').addClass('lt_even');
	//咨询列表详细咨询数据显示控制(全部)
	$('#showAllDetails').toggle(
		function(){
			$('li[class=ask_one_info]').show();
			$('li.lt_ask_id a').addClass('icon_minus');
			$(this).html('全部收拢');
		},
		function(){
			$('li[class=ask_one_info]').hide();
			$('li.lt_ask_id a').removeClass('icon_minus');
			$(this).html('全部展开');
		}
	)
	//咨询列表详细咨询数据显示控制
	$('li.lt_ask_id a').click(
		function(){
			if($(this).parent().parent().find('li[class=ask_one_info]').css('display')=='none'){
				$(this).parent().parent().find('li[class=ask_one_info]').slideDown();
				$(this).addClass('icon_minus');
			}else{
				$(this).parent().parent().find('li[class=ask_one_info]').slideUp();
				$(this).removeClass('icon_minus');
			}
		}
	)
	$('.li_table ul:gt(0)').hover(
		function(){
			$(this).addClass('lt_hover_BG');
		},
		function(){
			$(this).removeClass('lt_hover_BG');
		}
	)
	
	//咨询频道科室选择显示控制
	$('.side_wrap .dept_select>li>h3').click(
		function(){
			$(this).parent().find('ol').toggle();
			$(this).toggleClass('dept_slideUp');
		}
	)
	
	//标准科室咨询显示页面隐藏、显示咨询详细信息
	$('#icon_adinfo_more').click(
		function(){
			if($('#askInfoMore').css('display')=='none'){
				$('#askInfoMore').slideDown();
				$(this).addClass('arrowGreen_up').html('关闭详情');
			}else{
				$('#askInfoMore').slideUp();
				$(this).removeClass('arrowGreen_up').html('显示详情');
			}
		}
	)
	//right arrow_1
	//标准科室咨询显示页面隐藏、显示回复详细信息
	$('dd[class=adr_time] span').click(
		function(){
			if($(this).parent().parent().find('dt').css('display')=='none'){
				$(this).parent().parent().find('dt').show();
				$(this).parent().parent().find('dd[class=adr_cont]').show();
				$(this).parent().parent().find('dd[class=adr_grade]').show();
				$(this).removeClass('arrow_1');
			}else{
				$(this).parent().parent().find('dt').hide();
				$(this).parent().parent().find('dd[class=adr_cont]').hide();
				$(this).parent().parent().find('dd[class=adr_grade]').hide();
				$(this).addClass('arrow_1');
			}
		}
	)
})

function sKey(s,o){
	if(s!=''){
		tmp = $(o+' ol li').hide().filter(':contains('+ s +')').show();
	}else{
		$(o+' ol li').show();
	}
}
/*******************************************************\
*	关闭一个显示的BLOCK
*	o 为事件发生对象
*	e 为要打开的元素
\********************************************************/
function closeBlock(e){
	if($(e).css('display') == 'block'){
		$(e).fadeOut();
	}
}

/*******************************************************\
*	打开一个隐藏的BLOCK
*	o 为事件发生对象
*	e 为要打开的元素
\********************************************************/
function openBlock(e){
	if($(e).css('display') == 'none'){
		$(e).fadeIn();
	}
}

/*******************************************************\
*	选中一个值
*	a selected value
*	b selected text
*	c display input ID
*	d hidden input ID
\********************************************************/
function setValue(a,b,c,d){
	$('body').append('<span id="tmp" style="display:none;">'+b+'</span>');
	$('#tmp span').remove();
	b = $('#tmp').html();
	$('#tmp').remove();
	$(c).val(b);
	$(d).val(a);
}

//快速咨询页面获取科室热门咨询
function getQuickAskDeptList(id,s){
	$('#relDeptList').html('<h3>'+s+'热门咨询</h3>正在获取'+s+'热门咨询...');
	$.ajax({
		type: "POST",
		url: "/ask/?c=get_quick_ask_list&a=true",
		data: {'id':id,'dpt_name':s},
		success: function(data){
			$('#relDeptList').html(data);
		},
		error: function(){
			alert('致命错误！');
		}
	});
}
//高级咨询页面获取科室热门咨询
function getAskDeptList(id,s){
	$('#deptHotAskList').html('<div class="c_title"><h2>'+s+'</h2>浏览次数最多</div><div class="c_cont">正在获取数据...</div>');
	$.ajax({
		type: "POST",
		url: "/ask/?c=get_adv_ask_list&a=true",
		data: {'id':id,'dpt_name':s},
		success: function(data){
			$('#deptHotAskList').html(data);
		},
		error: function(){
			alert('致命错误！');
		}
	});
}

function doingTip(s){
	if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
		$("body","html").css({height: "100%", width: "100%"});
		$("html").css("overflow","hidden");
		$("html body").css("overflow","auto");
		$("#body_fixed_box").css("position","absolute");
	}
	if($("#body_fixed_box").html() == null){
		$('body').append('<div id="body_fixed_box" style="z-index:9999"><span>'+s+'</span></div>');
		$('#body_fixed_box').fadeIn();
	}else{
		$('#body_fixed_box').html('<span>'+s+'</span>');
	}
}
function undoingTip(){
	$('#body_fixed_box').fadeOut(500,function(){$(this).remove()});
}

//提交快速咨询ajax
function submitQuickAsk(){
	$("#code_error").html("");
	var options = {
		url:'/ask/?c=ask_add&a=true&action=add',
		type:'post',
		beforeSubmit:function(){
			$('#quickAsk').find(':input').attr('disabled','disabled');
			doingTip('正在提交数据...');
		},
		success:function(data){
			if(data == 1){
				doingTip('提交成功！3秒钟后关闭...');
				setTimeout('undoingTip();tb_remove()',3000);
			}
			else{
				doingTip(data);
				$('#quickAsk').find(':input').removeAttr('disabled');
				rechange_auth_code();
			}
		},
		error:function(){
			doingTip('致命错误！');
			rechange_auth_code();
		}
	};
	$('#quickAsk').ajaxSubmit(options);
}

//快速搜索医生ajax
function searchAskDoctor(s){
	if(s == '' || s=='输入完整的医生姓名...'){
		$('#deptListForDoctor').after('<div id="searchDoctor_tmp" class="showDept"><ul><li>请输入医生名字进行搜索！</li></ul></div>');
		setTimeout("closeBlock('#searchDoctor'),$('#searchDoctor_tmp').remove();",3000);
		
		return false;
	}
	if($('#searchDoctor').html() == null){
		$('#deptListForDoctor').after('<div id="searchDoctor" class="showDept"><ul><li>正在搜索'+s+'医生...</li></ul></div>');
	}else{
		$('#searchDoctor').html('<ul><li>正在搜索'+s+'医生...</li></ul>');
		openBlock('#searchDoctor');
	}
	$.ajax({
		type: "POST",
		url: "/ask/?c=search_ask_doctor&a=true",
		data: {'keyword':s},
		success: function(data){
			$('#searchDoctor').html(data);
		},
		error: function(){
			alert('致命错误！');
		}
	});
}

//获取真实科室列表ajax
function getRealDept(id,s){
	$('#deptListForDoctor').after('<div id="realDeptList" class="showDept"><ul><li>正在读取'+s+'所有真实科室...</li></ul></div>');
	$.ajax({
		type: "POST",
		url: "/ask/?c=get_real_dept_list&a=true",
		data: {'id':id,'dpt_name':s},
		success: function(data){
			$('#realDeptList').html(data);
		},
		error: function(){
			alert('致命错误！');
		}
	});
}

//获取医生ajax
function getRealDeptDoctor(value,id,s){
	value = value+'&id='+id+'&s='+s;
	$('#realDeptDoctor').remove();
	if($('#realDeptDoctor').html() == null){
		$('#deptListForDoctor').after('<div id="realDeptDoctor" class="showDept"><ul><li>正在读取'+s+'医生列表...</li></ul></div>');
	}else{
		$('#realDeptDoctor').html('<ul><li>正在读取'+s+'医生列表...</li></ul>');
	}
	$.ajax({
		type: "GET",
		url: "/ask/?c=get_real_dept_doctor&a=true",
		data: value,
		success: function(data){
			$('#realDeptDoctor').html(data);
		},
		error: function(){
			alert('致命错误！');
		}
	});
}

//获取某医生咨询活跃情况ajax
function getAskDetails(id,s){
	$('#showAskDetails').html('正在获取'+s+'的咨询活跃情况...').fadeIn();
	$.ajax({
		type: "POST",
		url: "/ask/?c=get_ask_details&a=true",
		data: {'id':id,'s':s},
		success: function(data){
			if(data != 1){
				$('#showAskDetails').html(data);
				$('#advanced_form button[type=submit]').removeAttr('disabled');
			}else{
				$('#showAskDetails').html('<span style="color:red">此医生未回复咨询过多，请选择其他医生进行咨询！</span>');
				$('#advanced_form button[type=submit]').attr('disabled','disabled');
			}
		},
		error: function(){
			alert('致命错误！');
		}
	});	
}

//添加一个文件上传框
function addInput(){
	max_num = 5;//最多上传文件数
	input_index = $('#uploadAskFile :input[type=file]').size();
	i = input_index + 1;
	tmp = '<li><label>名称<input type="text" name="describe'+i+'" /></label> <input type="file" check="file_safe" canBeNull="true" warning="文件类型只能为 gif,jpg,jpeg,png,bmp,tiff,pdf,doc" name="file'+i+'" /> <a onclick="javascript:deleteInput(this)" href="#1">-删除</a></li>\n';
	if(i>max_num){//最多上传文件数
		$('#addUploadInput').html('最多只能添加'+max_num+'个附件！').removeAttr('onclick');
		tmp = '';
	}
	$('#uploadAskFile li:last').after(tmp);
}
//删除一个文件上传框
function deleteInput(o){
	$(o).parent().remove();
	$('#addUploadInput').attr('onclick','addInput()').html('+添加');
}

//装载联系人信息
function contacts_js(contacts_id,user_id){
	$.ajax({
		type: "GET",
		url:  "/ask/?c=get_contact&a=true",
		data: "contacts_id="+contacts_id+"&user_id="+user_id,
		success: function(data){
			$('#contacts_add').html(data);
		},
		error: function(){
		}
	});
}

//评分
function fraction_vote(reply_id,value,total_score,dpt_id){
	$('#fraction'+reply_id+' input').attr("disabled","disabled");
	$('#fraction'+reply_id+' font').css({position:'absolute',border:'1px solid #f60',background:'#fee'}).show().html('正在评分...');
	$.ajax({
		type: "GET",
		url:  "/p/"+dpt_id+"/fraction_vote?a=true",
		data: "value="+value+"&reply_id="+reply_id,
		success: function(data){
			$('#fraction'+reply_id+' input').removeAttr("disabled");
			if (data == 'false'){
				$('#fraction'+reply_id+' font').css({position:'absolute',border:'1px solid #f60',background:'#fee'}).show().html('您已经评过分了');
				setTimeout(function(){$('#fraction'+reply_id+' font').fadeOut()},3000);
			}else{
				total_score = parseInt(total_score)+parseInt(value);
				total_count = parseInt($('#fraction_count_'+reply_id).html()) + 1;
				fraction_percent = Math.round(total_score/total_count*10)/10;
				$('#fraction_num_'+reply_id).html(fraction_percent);
				$('#fraction_count_'+reply_id).html(total_count);
				$('#fraction'+reply_id+' font').css({position:'absolute',border:'1px solid #f60',background:'#fee'}).show().html('评分成功!');
				setTimeout(function(){$('#fraction'+reply_id+' font').fadeOut()},3000);

			}
		},
		error: function(){
		}
	});
}

//添加评论
function commentAdd(content,nodes,object_id,module,dpt_id,owner_id){
	$("#code_error").html("");
	auth_code = $('#code_name').val();//验证码
	if(content == ''){
	   alert("你忘记写评论内容了吧？~呵呵");
	}else{
		$("#newComment input").attr("disabled","disabled");
		$("#newComment textarea").attr("disabled","disabled");
		$("#newComment h4").html("正在提交评论数据...");
	    for(var i=0;i<=nodes.length;i++){
		   if(nodes[i].checked){
			 manner_data = "is_anonymous="+nodes[i].value;
			  break;
		    }
	     }
		$.ajax({
			type: "POST",
			url: "/p/"+dpt_id+"/comment_add?a=true",
			data: manner_data+"&content="+content+"&object_id="+object_id+"&module="+module+"&owner_id="+owner_id+"&auth_code="+auth_code,
			success: function(data){
				if(data == 2){
					$("#code_error").html("验证码错误!");
					$("#newComment input").removeAttr("disabled");
					$("#newComment textarea").removeAttr("disabled");
				}
				else{
					$("#newComment h4").fadeIn(1000).html("评论成功！你可以再写一条评论~");
					$("#newComment input").removeAttr("disabled");
					$("#newComment textarea").removeAttr("disabled").val("");
					tmp_html = $("#suggestionList ol").html();
					$("#suggestionList ol").html(data).append(tmp_html);
					rechange_auth_code();
				}
			},
			error: function(){
				$("#newComment h4").html("评论失败，请重试！");
				$("#newComment input").removeAttr("disabled");
				$("#newComment textarea").removeAttr("disabled");
			}
		});
   }
}

//评论投票
//num == 1为支持  不然为反对
function comment_vote(vote_id,num,vote,dpt_id){
    $.ajax({
		type: "GET",
		url:  "/p/"+dpt_id+"/comment_vote?a=true",
		data: "vote_id="+vote_id+"&num="+num+"&vote="+vote,
		success: function(data){
			if (num == '1'){
				if(!isNaN(Number(data))){
				   $('#suggestionList span[name='+vote_id+'supported]').html("得票数: "+data);
				}else{
				   $('#suggestionList span[name='+vote_id+'supported]').html(data);
				}
			}else{
				if(!isNaN(Number(data))){
				   $('#suggestionList span[name='+vote_id+'opposed]').html("得票数: "+data);
				}else{
				   $('#suggestionList span[name='+vote_id+'opposed]').html(data);
				}	
			}
		},
		error: function(){
		}
	});	
}

//添加编辑标签
function edit_my_tag(ask_id,dpt_id){
	if (ask_id != ''){
		$('#message_succ').html('');
		tag_name = $('#tag_id').val();
		if (tag_name == ''){
			alert('标签名称不能为空!');
		}
		else{
			$('#message_succ').html('正在提交...');
			$.ajax({
				type: "POST",
				url: "/p/"+dpt_id+"/edit_my_tag?a=true",
				data: "ask_id="+ask_id+"&tag_name="+tag_name,
				success: function(data){
					if (data == 0){
						alert('编辑错误,请重试!');
					}else{
						$('#message_succ').html('操作成功,1.5秒后关闭该窗口');
						$('#new_my_tag').html(data);
						setTimeout('tb_remove()',1500);
					}
				},
				error: function(){	
				}
			});
		}
	}
}

