<?php
/*
Plugin Name: CG Comment Reply
Version: 0.1&nbsp;<strong>&alpha;</strong>
Plugin URI: http://www.caesarsgrunt.com/
Description: Enhances the appearance and functionality of the new reply-to-comments feature in 2.7-hemorrhage.
Author: Caesar&nbsp;Schinas
Author URI: http://www.caesarsgrunt.com/
*/
function cg_comment_reply() {
	ob_start();
?>
	<div id="replydiv" style="display:none;">
		<p id="replyhandle" style="cursor:default;">
			<!--<a id='show-settings-link' class='button-secondary' style='float:right; cursor:pointer; font-size:0.8em; line-height:inherit; padding-right:5px;'>More Details</a>-->
			<?php _e('Reply'); ?>
		</p>
		<form action="" method="post" id="replyform">
			<input type="hidden" name="user_ID" id="user_ID" value="<?php echo $current_user->ID; ?>" />
			<input type="hidden" name="action" value="replyto-comment" />
			<input type="hidden" name="comment_ID" id="comment_ID" value="" />
			<input type="hidden" name="comment_post_ID" id="comment_post_ID" value="" />
			<input type="hidden" name="position" id="position" value="<?php echo $position; ?>" />
			<input type="hidden" name="checkbox" id="checkbox" value="1" />
			<input type="hidden" name="mode" id="mode" value="<?php echo $mode; ?>" />
			<?php wp_nonce_field( 'replyto-comment', '_ajax_nonce', false ); ?>
			<?php wp_comment_form_unfiltered_html_nonce(); ?>
			
			<div id="replycontainer"><textarea rows="5" cols="50" name="replycontent" tabindex="10" id="replycontent"></textarea></div>
			<p id="replyerrtext" class="error" style="display:none; padding:10px; margin:2px 0; border:none;"></p>
			
			<p id="replysubmit">
				<input id="close-button" type="button" onclick="commentReply.close();" class="button" value="<?php _e('Cancel'); ?>" />
				<input id="reply-button" type="button" onclick="commentReply.send();" class="button button-highlighted" value="<?php _e('Submit Reply'); ?>" />
				<input id="retry-button" type="button" onclick="commentReply.open();" class="button button-highlighted" style="display:none;" value="<?php _e('Go Back'); ?>" />
			</p>
		</form>
	</div>
	<script type='text/javascript'>
		jQuery(function($){
			commentReply.open = function(c, p) {
				$('#replyerrtext, #retry-button').hide();
				$('#replycontainer, #ed_reply_qtags, #reply-button').show();
				
				$('#replydiv #comment_post_ID').val(p);
				$('#replydiv #comment_ID').val(c);
				
				if ($('td#replyerror').is(':visible')) {
					$('td#replyerror').hide();
					$('#comment-'+c).after($('td#replydiv').show());
				}
				else if (!$('td#replydiv').is(':visible')) {
					var tr = document.createElement('tr');
					var td = tr.appendChild(document.createElement('td'))
					$(td).attr('colspan',5).hide();
					$('#comment-'+c).after(tr);
					$(td).append($('#replydiv').contents());
					$('#replydiv').remove();
					$('#replyform, #replyhandle').hide();
					$(td).attr('id','replydiv').fadeIn('fast').css('border','1px solid #CCC');
					$('#replyform, #replyhandle').slideDown('fast');
				}
				else {
					$('#comment-'+c).after($('td#replydiv'));
				}
				
				$('#replycontent').focus().keyup(function(e){
					if (e.which == 27) commentReply.close(); // close on Escape
				});
				
				$('#replycontainer').resizable({
					handles : 's',
					minHeight : 100,
					maxHeight : 350,
					containment : '#wpwrap',
					resize : function(e,o) {
						h = o.size.height;
						$('#replycontainer').height(h);
						$('#replycontainer').width('auto');
					},
				});
		
				$('.ui-resizable-s').css({
					border: '0 none',
					width: '11px',
					height: '12px',
					background: 'transparent url(images/se.png) no-repeat scroll 0 0',
					right: '2px',
					left: 'auto',
					bottom: '2px',
					position: 'absolute'
				});
				
				setTimeout(function(){
					var replyBottom = $('#replydiv').offset().top + $('#replydiv').height();
					var scrollTop = (window.pageYOffset||document.body.scrollTop||document.documentElement.scrollTop);
					var viewportHeight = (document.documentElement.clientHeight || self.innerHeight || document.body.clientHeight || 0) + 0;
					var scrollBottom = scrollTop + viewportHeight;
			
					if ( scrollBottom - 20 < replyBottom )
						window.scroll(0, replyBottom - viewportHeight + 25);
				}, 500);
			}
			commentReply.close = function() {
				$('#replyform, #replyhandle').slideUp('fast');
				$('#replydiv').fadeOut('fast');
				
				$('#replycontent').val('');
				
				return false;
			}
			commentReply.error = function(r) {
				var er = r.statusText;
		
				if ( r.responseText )
					er = r.responseText.replace( /<.[^<>]*?>/g, '' );
		
				if ( er ) {
					var height = $('#replycontainer').height() + $('#ed_reply_qtags').height() - 13;
					$('#replycontainer, #ed_reply_qtags, #reply-button').hide();
					$('#replyerrtext').show().css('height', height);
					$('#retry-button').show().focus();
					
					$('#replyerrtext').html(er)
					$('#close-button').css('outline','none').focus().keyup(function(e) {
						if (e.which == 27) commentReply.close(); // close on Escape
					});
				}
			}
		});
	</script>
<?php
	$form = ob_get_contents();
	ob_end_clean();
	return $form;
}
add_filter('wp_comment_reply','cg_comment_reply');
?>
