function showCaptcha(id)
{
	var captcha = $('#'+id);
	if (captcha.css('display') == 'none') captcha.css('display', '');
}
var inedit = false;
function editcomment(rid)
{
	if (/^\d+?$/.test(rid) && inedit == false)
	{
		var comment = $('#comment_content_'+rid);
		inedit = true;
		editAjax(comment, rid);
	}
}
function editAjax(obj, rid)
{
	var formercomment = obj.html();
	obj.html(edit_comment_loading);
	$.ajax({
		type: 'GET',
		url: pb_url+'ajax.php',
		dataType: 'json',
		data: 'type=editcomment' + '&rid=' + rid  + '&verifyhash=' + verifyhash + '&random=' + Math.random(),
		success: function(data)
		{
			if (data[0]=='1')
			{
				formercomment = encodeURIComponent(formercomment);
	            obj.html('<textarea id="editcomment_'+rid+'" name="editcomment_'+rid+'" style="width:300px;height:100px;">'+data[1]+'</textarea>&nbsp;<input type="button" name="editconfig_'+rid+'" id="editconfig_'+rid+'" onclick="sendcommentedit(\''+formercomment+'\','+rid+')" value="'+comment_submit+'"/>&nbsp;<input type="button" name="caceledit_'+rid+'" id="caceledit_'+rid+'" onclick="cancleedit(\''+formercomment+'\','+rid+')" value="'+comment_cancel+'" />');
			}
			else
			{
				alert(data[1]);
				obj.html(formercomment);
				inedit = false;
			}
		}
	});
	return;
}
function cancleedit(obj,rid)
{
	$('#comment_content_'+rid).html(decodeURIComponent(obj));
	inedit = false;
}
function sendcommentedit(obj,rid)
{
	var editcomment = $('#editcomment_'+rid).val();
	$('#editconfig_'+rid).attr('disabled', 'disabled');
	if (editcomment.length < comment_minc || editcomment.length > comment_maxc)
	{
		alert(comment_length_error);
		$('#editconfig_'+rid).attr('disabled', '');
		$('#editcomment_'+rid).focus();
		return;
	}
	$.ajax({
		type: 'POST',
		url: pb_url+'ajax.php',
		dataType: 'json',
		data: 'type=sendeditcomment&comment='+encodeURIComponent(editcomment)+'&rid=' + rid + '&verifyhash=' + verifyhash + '&random=' + Math.random(),
		error: function(var1,var2,var3)
		{
			$('#comment_content_'+rid).hide().html(decodeURIComponent(obj)).show('slow');
		},
		success: function(data)
		{
			alert(data[1]);
			if (data[0] == '1')
			{
	            $('#comment_content_'+rid).hide().html(data[2]).show('slow');
			}
			else
			{
				$('#comment_content_'+rid).hide().html(decodeURIComponent(obj)).show('slow');
			}
		}
	});
	inedit = false;
	return;
}
function recomment(rid, author)
{
	var reply_body = $('#comment_reply_body');
	var quote = $.trim($('#comment_content_' + rid).text());
	var add = '';
	if (quote.length > 50)
	{
		add = "...";
	}
	quote = quote.substr(0, 50) + add;
	reply_body.val('[quote=' + author + ']'+quote+'[/quote]');
	reply_body[0].focus();
	return;
}
function comment(tid){
	var sendcomment = $('#send_comment' + tid);
  var comment = $('#comment_reply_body' + tid).val();
	var ctip = $('#c_tip' + tid);
	var captcha = $('#captcha').val();
	
  
  //sendcomment.attr('disabled', 'disabled');
	
  if (comment.length < comment_minc || comment.length > comment_maxc){
		
    ctip.css('display', 'block').html(comment_length_error);
		sendcomment.attr('disabled', '');
		return;
		
	}
	  
	commentAjax(ctip, 'type=comment&comment='+encodeURIComponent(comment)+'&tid='+tid+'&verifyhash=' + verifyhash + '&captcha=' + captcha, tid);
}

//增加一個tid參數，記錄文章編號
function commentAjax(obj, param, tid){
	
  obj.css('display', 'block').html(add_comment_loading);
	$.ajax({
		type: 'POST',
		url: pb_url+'ajax.php',
		dataType: 'json',
		data: param + '&random=' + Math.random(),
		 
		
		
    error: function(var1,var2,var3)
		{
			obj.html(ajax_response_failed);
		},
		success: function(data)
		{
			if (data[0] == '1-1')
			{
			  
				obj.html(data[1]);
	      $('#nocomment').remove();
	      $('#coment_reply' + tid).append(data[2]);
	      $('#comment_reply_body' + tid).val('');
	      obj.fadeOut(2000,function(){obj.css({display:'none'})});
			}else if (data[0]=='1-0'){
				obj.html(data[1]);
				$('#comment_reply_body' + tid).val('');
				obj.fadeOut(2000,function(){obj.css({display:'none'})});
			}else{
				obj.html(data[1]);
			}
			$('#captcha').val('');
			$('#checkcode').attr('src', pb_url+'checkcode.php?' + '&random=' + Math.random());
			$('#send_comment').attr('disabled', '');
		}
	});
	return;
}
//增加一個tid參數
function emoticon(text, tid) {
	var txtarea = document.getElementById('comment_reply_body' + tid);
	text = '[emote]' + text + '[/emote]';
	if (txtarea.createTextRange && txtarea.caretPos) {
		var caretPos = txtarea.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == '[emote]' ? text + '[/emote]' : text;
		txtarea.focus();
	} else {
		txtarea.value  += text;
		txtarea.focus();
	}
}
function storeCaret(textEl) {
	if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}
$(document).ready(function(){
	$('#content img').each(function(){
		if (this.width > 600){this.height=parseInt(this.height*600/this.width);this.width=600;}
	})
})

function reply(num){
  //alert (reply + "和" + num);
  //document.getElementById(reply).style.color = "green";
  //document.getElementById("comments_" + num).style.display = "inline";
  var replybutton = document.getElementById("comments_" + num)
  if(replybutton.style.display == "none"){
    replybutton.style.display = "block";
  }else{
    replybutton.style.display = "none" ;
    
  }  
}
