jQuery(document).ready(function($){
var i = 0, got = -1, len = document.getElementsByTagName('script').length;
while ( i <= len && got == -1){
	var js_url = document.getElementsByTagName('script')[i].src,	
			got = js_url.indexOf('comments-ajax.js'); i++ ;
}
var	ajax_php_url = js_url.replace('-ajax.js','-ajax.php'),
		wp_url = js_url.substr(0,js_url.indexOf('/wp-content/')),
		pic_sb_url = wp_url + '/wp-admin/images/wpspin_light.gif',
		pic_no_url = wp_url + '/wp-admin/images/no.png',
		pic_ys_url = wp_url + '/wp-admin/images/yes.png',
		txt1 = ' style="display: none;background: url(',
		txt2 = ') no-repeat left;padding-left:20px;',
		txt3 = '<div id="commentload"'+ txt1 + pic_sb_url + txt2 + '">正在提交, 請稍候...</div>',
		txt4 = '<div id="commenterror"'+ txt1 + pic_no_url + txt2 + 'margin: 0 auto;">#</div>',
		txt5 = '\n<div style="margin-top:1em;"><ol class="commentlist" id="new_comm_',
		txt6 = '\n<ul class="children" id="new_comm_',
		txt7 = '" style="display: none;">',
		txt8 = '\n<span id="success_',
		txtb, num = 1, $new_comm;
				$('#submit').attr("disabled",false);
				$('#submit').after( txt3 + txt4 );
$('#commentform').submit(function(){
				$('#submit').attr("disabled",true).fadeTo('slow', 0.3);
	$.ajax({
				url: ajax_php_url,
				data: $('#commentform').serialize(),
				type: 'POST',
		beforeSend: function(){
				$('#commenterror').hide();
				$('#commentload').slideDown();
				},
		error: function(request){
				$('#commentload').slideUp();
				$('#commenterror').show('slow').html(request.responseText);
				setTimeout(function(){$('#submit').attr('disabled',false).fadeTo('slow', 1);$('#commenterror').slideUp();}, 3000);
				},
		success: function(data){
				$('textarea').each(function(){this.value=''});	
				$('#commentload').hide();
		var t = addComment, cancel = t.I('cancel-comment-reply-link'),
				temp = t.I('wp-temp-form-div'), respond = t.I(t.respondId),
				post = t.I('comment_post_ID').value, parent = t.I('comment_parent').value,
		 		num_text = num.toString();
	if ( parent == '0'){new_htm = txt5 + num_text + txt7 + '</ol>' }
	else {new_htm = txt6 + num_text + txt7 + '</ul>';
		is_div = document.getElementsByTagName('ol')[0].innerHTML.indexOf('div-');
		if ( is_div == -1 ){txtb = ''} else {txtb = 'div-'};
			 }
				new_htm = new_htm + txt8 + num_text;
				$('#respond').before(new_htm);
		var $new_comm = $('#new_comm_' + num_text);
				$new_comm.append(data).show(500);
				countdown();
				num++ ;
		cancel.style.display = 'none';
		cancel.onclick = null;
		t.I('comment_parent').value = '0';
if ( temp && respond ){
		temp.parentNode.insertBefore(respond, temp);
		temp.parentNode.removeChild(temp)}
				}
			});
  return false;
	});
addComment = {
	moveForm : function(commId, parentId, respondId, postId) {
		var t = this, div, comm = t.I(commId), respond = t.I(respondId), cancel = t.I('cancel-comment-reply-link'), parent = t.I('comment_parent'), post = t.I('comment_post_ID');
		$('#commenterror').hide();
		t.respondId = respondId;
		postId = postId || false;
		if ( ! t.I('wp-temp-form-div') ) {
			div = document.createElement('div');
			div.id = 'wp-temp-form-div';
			div.style.display = 'none';
			respond.parentNode.insertBefore(div, respond)
		}
		if ( post && postId && comm )
			comm.parentNode.insertBefore(respond, comm.nextSibling);
			post.value = postId;
			parent.value = parentId;
			cancel.style.display = '';
		cancel.onclick = function() {
			var t = addComment, temp = t.I('wp-temp-form-div'), respond = t.I(t.respondId);
			$('#commenterror').hide();
			this.style.display = 'none';
			this.onclick = null;
			t.I('comment_parent').value = '0';
		if ( temp && respond ){
			temp.parentNode.insertBefore(respond, temp);
			temp.parentNode.removeChild(temp)}
			return false;
		};
		try { t.I('comment').focus(); }
		catch(e) {}
		return false;
	},
	I : function(e) {
		return document.getElementById(e)
	}
};
var wait = 15, submit_val = $('#submit').val();
function countdown(){	
	if ( wait == 0 ){
		$('#submit').val(submit_val).attr('disabled',false).fadeTo('slow', 1);
		wait = 15;
	} else {
		$('#submit').val(wait); wait--; setTimeout(countdown,1000);	
  }
};
})