/*
===============================================================
CommentPress addComment with TinyMCE Javascript
===============================================================
AUTHOR			: Christian Wach <needle@haystack.co.uk>
LAST MODIFIED	: 30/04/2009
DEPENDENCIES	: tinymce.js
---------------------------------------------------------------
NOTES

This script is only included when the CommentPress theme is active 
and the browser is not a mobile one.

The moveForm() method is called by onclick attributes of the 
"Reply to this comment" links, which are auto-generated by WP.

This is a rewritten version of the inbuilt Wordpress addComment 
object for several reasons:

(1) The built-in Wordpress Javascript does not allow for the 
enabling of TinyMCE, *even though* Wordpress now ships with it 
built-in to the Admin Interface. TinyMCE must be de-activated 
before the respond div is moved, then re-enabled once the move 
has been completed.

(2) There is a name clash between TinyMCE's wrapper and the 
comment_parent input when comment threading is enabled. Both use 
id="comment_parent", thus preventing that variable from being 
posted when the form is submitted.

(3) CommentPress has a replytopara link parameter and an additional 
text_signature variable which need to be accounted for. See the 
moveFormToPara() method for details of the latter.

---------------------------------------------------------------
*/






/** 
 * @description: comment area handler object
 * @todo:
 *
 */	
addComment = {



	/** 
	 * @description: method for moving the comment form
	 * @todo:
	 *
	 */	
	moveForm : function( commentID, parentID, respondID, postID, textSig ) {
	

		
		// unload tinyMCE
		this.disableForm();
		


		// properties
		var div_e;
		var comm_e = this.I(commentID);
		var respond_e = this.I(respondID);
		var cancel_e = this.I('cancel-comment-reply-link');
		var parent_e = this.I('comment_parent');
		var post_e = this.I('comment_post_ID');
		var sig_e = this.I('text_signature');


		
		// sanity check
		if ( !comm_e || !respond_e || !cancel_e || !parent_e ) {
		
			// reload tinyMCE
			this.enableForm();
			
			// --<
			return;
			
		}



		// show respond element
		respond_e.style.display = 'block';

		// if we have them...
		if ( post_e && postID ) {
		
			// set comment_post_ID hidden input to postID
			post_e.value = postID;
			
		}
		
		// set comment_parent hidden input to parentID
		parent_e.value = parentID;
		//alert( 'parent_e.value set: ' + parent_e.value );
		
		// set text_signature hidden input to text signature
		if ( sig_e ) { sig_e.value = textSig; }

		// store respondID for cancel method to access
		this.respondID = respondID;
		


		// do we have a temp div?
		if ( ! this.I('wp-temp-form-div') ) {
			
			// create one
			div_e = document.createElement('div');
			div_e.id = 'wp-temp-form-div';
			div_e.style.display = 'none';
			respond_e.parentNode.insertBefore( div_e, respond_e );
			
		}



		// insert comment response area
		comm_e.parentNode.insertBefore( respond_e, comm_e.nextSibling );
		
		
		
		// show cancel link
		cancel_e.style.display = '';

		
		
		/** 
		 * @description: method for cancel button
		 * @todo:
		 *
		 */	
		cancel_e.onclick = function() {
		
			var x = addComment.resetForm();
			
			// --<
			return false;
	
		}

		
		
		// test for tinyMCE
		if ( $.is_object( tinyMCE ) ) {

			// reload tinyMCE
			this.enableForm();
		
		} else {
		
			// try and give focus to textarea - disabled since we use tinyMCE
			try { this.I('comment').focus(); }
			catch(e) {}
			
		}
		


		// --<
		return false;
		
	},
	
	
	
	/** 
	 * @description: method for moving the comment form to a paragraph block
	 * @todo:
	 *
	 */	
	moveFormToPara : function( paraNum, textSig, postID ) {
		
		// set paraID
		var paraID = 'reply_to_para-' + paraNum;
		


		// move the form
		addComment.moveForm( 
		
			paraID, 
			'0', 
			'respond', 
			postID,
			textSig
			
		);
		
		
		
		// --<
		return false;
		
	},
	

		
	/** 
	 * @description: utility reset form
	 * @todo:
	 *
	 */	
	resetForm : function() {
		
		// get our temp div element
		var temp_e = addComment.I('wp-temp-form-div');
		
		// get our comment response element
		var respond_e = addComment.I(addComment.respondID);
		
		// get cancel button
		var cancel_e = this.I('cancel-comment-reply-link');

		// sanity check
		if ( !temp_e || !respond_e ) {
		
			// --<
			return;
			
		}



		// unload tinyMCE
		addComment.disableForm();



		// unset comment parent value
		addComment.I('comment_parent').value = '0';
		
		// unset comment text signature value
		if ( addComment.I('text_signature') ) {
			addComment.I('text_signature').value = '';
		}
		
		
		
		// DOM manipulation
		temp_e.parentNode.insertBefore( respond_e, temp_e );
		temp_e.parentNode.removeChild( temp_e );
		
		
		
		// hide cancel link
		cancel_e.style.display = 'none';
		
		// disable this until next run
		cancel_e.onclick = null;
		
		
		
		// reload tinyMCE
		addComment.enableForm();



		// hide respond element
		respond_e.style.display = 'none';



		// --<
		return false;
		
	},
	
	
	
	/** 
	 * @description: utility get element ID method
	 * @todo:
	 *
	 */	
	I : function(e) {
	
		// --<
		return document.getElementById(e);
		
	},
	
	
	
	/** 
	 * @description: utility method for enabling the comment form
	 * @todo:
	 *
	 */	
	enableForm : function() {

		// test for tinyMCE
		if ( $.is_object( tinyMCE ) ) {
		
			// unload tinyMCE
			tinyMCE.execCommand('mceAddControl', false, 'comment');
			//alert( 'control removed' );
			
			this.tinyMCEenabled = true;
			
		}
		
	},

		
	/** 
	 * @description: utility method for disabling the comment form
	 * @todo:
	 *
	 */	
	disableForm : function() {

		// test for tinyMCE
		if ( $.is_object( tinyMCE ) ) {
		
			// unload tinyMCE
			tinyMCE.execCommand('mceRemoveControl', false, 'comment');
			//alert( 'control removed' );
			
			this.tinyMCEenabled = false;

		}
		
	}

		

}

