/**
 * WordPress jQuery-Ajax-Comments v1.29 by Willin Kan.
 * URI: http://willin.heliohost.org/?p=1271
 */
var $j = jQuery.noConflict();
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 edit_mode = '1', // 再編輯模式 ( '1'=開; '0'=不開 )
		ajax_php_url = js_url.replace('-ajax.js','-ajax.php'),
		wp_url = js_url.substr(0, js_url.indexOf('wp-content')),
		pic_sb = wp_url + 'wp-admin/images/wpspin_dark.gif', // 提交 icon
		pic_no = wp_url + 'wp-admin/images/no.png',          // 錯誤 icon
		pic_ys = wp_url + 'wp-admin/images/yes.png',         // 成功 icon
		txt1 = '<div id="loading"><img src="' + pic_sb + '" style="vertical-align:middle;" alt=""/> 正在提交, 稍后...</div>',
		txt2 = '<div id="error">#</div>',
		txt3 = '"><img src="' + pic_ys + '" style="vertical-align:middle;" alt=""/> 提交成功',
		edt1 = ', 在你刷新本页面之前可以<a rel="nofollow" class="comment-reply-link" href="#edit" onclick=\'return addComment.moveForm("',
		edt2 = ')\'>再编辑</a>',
		cancel_edit = '取消编辑',
		edit, num = 1, comm_array=[]; comm_array.push('');

jQuery(document).ready(function($j) {
		$jcomments = $j('#comments'); // 評論數的 ID
		$jcancel = $j('#cancel-comment-reply-link'); cancel_text = $jcancel.text();
		$jsubmit = $j('#commentform #submit'); $jsubmit.attr('disabled', false);
		$j('#comment').after( txt1 + txt2 ); $j('#loading').hide(); $j('#error').hide();
		$jbody = (window.opera) ? (document.compatMode == "CSS1Compat" ? $j('html') : $j('body')) : $j('html,body');

/** submit */
$j('#commentform').submit(function() {
		$j('#loading').slideDown();
		$jsubmit.attr('disabled', true).fadeTo('slow', 0.5);
		if ( edit ) $j('#comment').after('<input type="text" name="edit_id" id="edit_id" value="' + edit + '" style="display:none;" />');

/** Ajax */
	$j.ajax( {
		url: ajax_php_url,
		data: $j(this).serialize(),
		type: $j(this).attr('method'),

		error: function(request) {
			$j('#loading').slideUp();
			$j('#error').slideDown().html('<img src="' + pic_no + '" style="vertical-align:middle;" alt=""/> ' + request.responseText);
			setTimeout(function() {$jsubmit.attr('disabled', false).fadeTo('slow', 1); $j('#error').slideUp();}, 3000);
			},

		success: function(data) {
			$j('#loading').hide();
			comm_array.push($j('#comment').val());
			$j('textarea').each(function() {this.value = ''});
			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;

// comments
		if ( ! edit && $jcomments.length ) {
			n = parseInt($jcomments.text().match(/\d+/));
			$jcomments.text($jcomments.text().replace( n, n + 1 ));
		}

// show comment
		new_htm = '" id="new_comm_' + num + '"></';
		new_htm = ( parent == '0' ) ? ('\n<ol style="clear:both;" class="commentlist' + new_htm + 'ol>') : ('\n<ul class="children' + new_htm + 'ul>');

		ok_htm = '\n<span id="success_' + num + txt3;
		if ( edit_mode == '1' ) {
			div_ = (document.body.innerHTML.indexOf('div-comment-') == -1) ? '' : 'div-';
			ok_htm = ok_htm.concat(edt1, div_, 'comment-', parent, '", "', parent, '", "respond", "', post, '", ', num, edt2);
		}
		ok_htm += '</span><span></span>\n';

		$j('#respond').before(new_htm);
		$j('#new_comm_' + num).hide().append(data);
		$j('#new_comm_' + num + ' li').append(ok_htm);
		$j('#new_comm_' + num).fadeIn(4000);

		$jbody.animate( { scrollTop: $j('#new_comm_' + num).offset().top - 200}, 900);
		countdown(); num++ ; edit = ''; $j('*').remove('#edit_id');
		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)
		}
		}
	}); // end Ajax
  return false;
}); // end submit

/** comment-reply.dev.js */
addComment = {
	moveForm : function(commId, parentId, respondId, postId, num) {
		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');
		if ( edit ) exit_prev_edit();
		num ? (
			t.I('comment').value = comm_array[num],
			edit = t.I('new_comm_' + num).innerHTML.match(/(comment-)(\d+)/)[2],
			$jnew_sucs = $j('#success_' + num ), $jnew_sucs.hide(),
			$jnew_comm = $j('#new_comm_' + num ), $jnew_comm.hide(),
			$jcancel.text(cancel_edit)
		) : $jcancel.text(cancel_text);

		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)
		}

		 !comm ? ( 
			temp = t.I('wp-temp-form-div'),
			t.I('comment_parent').value = '0',
			temp.parentNode.insertBefore(respond, temp),
			temp.parentNode.removeChild(temp)
		) : comm.parentNode.insertBefore(respond, comm.nextSibling);

		$jbody.animate( { scrollTop: $j('#respond').offset().top - 180 }, 400);

		if ( post && postId ) post.value = postId;
		parent.value = parentId;
		cancel.style.display = '';

		cancel.onclick = function() {
			if ( edit ) exit_prev_edit();
			var t = addComment, temp = t.I('wp-temp-form-div'), respond = t.I(t.respondId);

			t.I('comment_parent').value = '0';
			if ( temp && respond ) {
				temp.parentNode.insertBefore(respond, temp);
				temp.parentNode.removeChild(temp);
			}
			this.style.display = 'none';
			this.onclick = null;
			return false;
		};

		try { t.I('comment').focus(); }
		catch(e) {}

		return false;
	},

	I : function(e) {
		return document.getElementById(e);
	}
}; // end addComment

function exit_prev_edit() {
		$jnew_comm.show(); $jnew_sucs.show();
		$j('textarea').each(function() {this.value = ''});
		edit = '';
}

var wait = 15, submit_val = $jsubmit.val();
function countdown() {
	if ( wait > 0 ) {
		$jsubmit.val(wait); wait--; setTimeout(countdown, 1000);
	} else {
		$jsubmit.val(submit_val).attr('disabled', false).fadeTo('slow', 1);
		wait = 15;
  }
}

});// end jQ

jQuery(document).ready(function($j){
	$j('.reply').click(function() {
	var atname = $j(this).prevAll().find('.fn').text();
	$j("#comment").attr("value","@"+$j.trim(atname)+": ").focus();
});
})
$j(function(){
    var $jcomment = $j('#comment');
    var $jstr  =  $j('.str');
    var time;
    $jcomment.focus(function(){
        time = window.setInterval( substring,100 );
    });   
    function substring() {
        var val = $jcomment.val();
        var length = val.length;
        if( $jstr.html() != (length) ){
            if(length==0){
                $j(".wordcount")[0].firstChild.nodeValue = "Typed ";
                $jstr.html(length);
            }else{
                $j(".wordcount")[0].firstChild.nodeValue = "Typed ";
                $jstr.html(length);
            }
        }
    }
});
