/**
* HGContent
*
* The HGContent object is responsible for the organization of HGContent on the HGPage
*
* @param style_set
*          -style_set_hg_content
*          -style_set_hg_content_content_out
*          -style_set_hg_content_content_over
*          -style_set_hg_content_control
*          -style_set_hg_reply_out
*          -style_set_hg_reply_over
* @param content_block_factory
* @param content_container_container_factory
* @param content_container_factory
* @param reply_container_container_factory
* @param reply_container_factory
* @param uid: The user_id of a logged on user
* @param al: The access level of the logged on user
* @param ctid: The content_type_id of the content being represented
* @param cid: The content_id of the content being represented
* @param cal: The access level needed to reply to the content
* @param reply_count: How many replies there are to the current content
**/
function HGContent( 
	style_set, 
	content_block_factory ,
	content_container_container_factory ,
	content_container_factory ,
	reply_container_container_factory ,
	reply_container_factory ,
	uid, al, ctid, cid, cal , 
	reply_count
) { 

	this.style_set = style_set;

	this.content_block_factory = content_block_factory;
	this.content_container_container_factory = content_container_container_factory;
	this.content_container_factory = content_container_factory;
	this.reply_container_container_factory = reply_container_container_factory;
	this.reply_container_factory = reply_container_factory;

	this.user_id = parseInt( uid );
	this.access_level = parseInt( al );

	this.content_type_id = parseInt( ctid );
	this.content_id = parseInt( cid );
	this.content_access_level = parseInt( cal );

	this.hg_content_block = new this.content_block_factory( style_set + "_content_block" ).build();

	this.content_element = new this.content_container_container_factory( style_set ).build();
	this.content_control_element = new TabularWidgetLayoutFactory( style_set, 10 ).build();
	this.reply_element = new this.reply_container_container_factory( style_set ).build();

	this.hg_content_block.addContentNode( this.content_element.getWidgetDOM() );
	this.hg_content_block.addContentNode( this.content_control_element.getDOM() );
	this.hg_content_block.addContentNode( this.reply_element.getWidgetDOM() );

	//generate the load replies button
	var lrb = loadRepliesButtonFactory( this.content_type_id, this.content_id, this, reply_count );
	this.content_control_element.addWidget( lrb );

	//generate the refresh content button
	var rcb = refreshContentButtonFactory( this );
	this.content_control_element.addWidget( rcb );

	//generate the insert reply button
	var irb = insertContentReplyButtonFactory( this.content_type_id, this.content_id, this, this.style_set );
	this.content_control_element.addWidget( irb );

	//hide the insert reply button if access level is not high enough
	if ( ! ( parseInt( this.access_level ) <= parseInt( cal ) && this.access_level > 0 && this.user_id > 0  ) ) {
		irb.hideWidget();
	}

	//create the status widget
	this.status_widget = new StatusWidget( "status" );
	this.content_control_element.addWidget( this.status_widget );

	return true;

}

HGContent.prototype.setStatusToWorking = function ( msg ) { 

	this.status_widget.setStatus( "working", msg );

}

HGContent.prototype.clearStatus = function() { 

	this.status_widget.hideWidget();

}

/**
* HGContentRenderCommand
*
* A command to render HGContent
*
* @param x: The XML to render
* @param uid: The user_id of the logged on user
* @param al: The access level of the logged on user
**/
function HGContentRenderCommand( 
	style_set, 
	content_block_factory ,
	content_container_container_factory ,
	content_container_factory ,
	reply_container_container_factory ,
	reply_container_factory ,
	x, uid, al 
) { 

	this.style_set = style_set;

	this.content_block_factory = content_block_factory;
	this.content_container_container_factory = content_container_container_factory;
	this.content_container_factory = content_container_factory;
	this.reply_container_container_factory = reply_container_container_factory;
	this.reply_container_factory = reply_container_factory;

	this.x = x;
	this.user_id = parseInt( uid );
	this.access_level = parseInt( al );

	return true;

}

new HGContentRenderCommand();

HGContentRenderCommand.prototype.execute = function() { 

	//pull the HGResponse element
	var hgr = this.x.getElementsByTagName( "HGResponse" )[0];

	//pull signature information from the XML
	var hgs = hgr.getElementsByTagName( "HGSignature" )[0];
	var content_type_id = hgs.getElementsByTagName( "content_type_id" )[0].childNodes[0].nodeValue;
	var content_id = hgs.getElementsByTagName( "content_id" )[0].childNodes[0].nodeValue;
	var reply_access = hgs.getElementsByTagName( "reply_access" )[0].childNodes[0].nodeValue; 

	//pull the creator id
	var hgd = hgr.getElementsByTagName( "HGDescription" )[0];
	var creator_id = hgd.getElementsByTagName( "creator_id" )[0].childNodes[0].nodeValue;

	//pull the reply count
	var reply_count = 0;

	var hg_additional_information = hgr.getElementsByTagName( "HGAdditionalInformation" )[0];
	if ( hg_additional_information ) { 
		reply_count = hg_additional_information.getElementsByTagName( "reply_count" )[0].childNodes[0].nodeValue;
	}

	//create the HGContent object
	var h = new HGContent( 
		"hg_content", 		
		this.content_block_factory ,
		this.content_container_container_factory ,
		this.content_container_factory ,
		this.reply_container_container_factory ,
		this.reply_container_factory ,
		this.user_id, 
		this.access_level, 
		content_type_id, 
		content_id, 
		reply_access ,
		reply_count
	);

	//check to determine the editability of the content
	var editability = ( this.access_level == 1 && this.user_id != 0 ) || ( this.user_id == creator_id );

	var content_toggle_field = RenderHGContentHelper( this.x, editability, this.style_set );
	content_toggle_field.setRefreshCommand( new HGContentContentRefreshCommand( content_type_id, content_id, content_toggle_field ) );
	content_toggle_field.setSubmitCommand( new HGContentSubmitCommand( content_type_id, content_id, content_toggle_field ) );

	var title_toggle_field = RenderHGContentTitleHelper( this.x, editability, this.style_set );
	title_toggle_field.setRefreshCommand( new HGContentTitleRefreshCommand( content_type_id, content_id, title_toggle_field ) );
	title_toggle_field.setSubmitCommand( new HGContentTitleSubmitCommand( content_type_id, content_id, title_toggle_field ) );

	var description_div = RenderHGContentDescriptionHelper( this.x, editability, this.style_set );

	//create a content_container
	var content_container = new this.content_container_factory( this.style_set + "_content_container" ).build();

	content_container.addContentNode( title_toggle_field.getDOM() );
	content_container.addContentNode( description_div );
	content_container.addContentNode( content_toggle_field.getDOM() );

	//attach everything to the HGContent object
	h.content_element.addContentNode( content_container.getWidgetDOM() );

	//attach the HGContent to the target
	//this.target.appendChild( h.hg_content_block.DOMObject );

	return h;

}

/**
* RenderHGContentHelper
*
* A function to render HG Content From XML
**/
function RenderHGContentHelper( x, editability, style_set ) { 

	var hgr = x.getElementsByTagName( "HGResponse" )[0];

	var hgd = hgr.getElementsByTagName( "HGDescription" )[0];

	var content_div = RenderHGContentContentHelper( x, editability, style_set );

	var content_toggle_field = new EditableToggleField( 
		content_div,
		null, //submit command
		"new_content",
		null, //refresh command
		800 , //width
		style_set , //style set
		SimpleWidgetFactory ,
		false ,
		editability ,
		"WYSIWYG"
	);

	return content_toggle_field;

}

function RenderHGContentContentHelper( x, editability, style_set ) { 

	var hgr = x.getElementsByTagName( "HGResponse" )[0];
	var hgc = hgr.getElementsByTagName( "HGContent" )[0];
	var content = hgc.getElementsByTagName( "content" )[0].childNodes[0].nodeValue;
	var content_div = textToDOM( "div", content, style_set + "_rendered_content" );

	return content_div;

}

/**
* RenderHGContentDescriptionHelper
*
* A funtion to render HG Content Description from XML
**/
function RenderHGContentDescriptionHelper( x, editability, style_set ) { 


	var hgr = x.getElementsByTagName( "HGResponse" )[0];

	var hgd = hgr.getElementsByTagName( "HGDescription" )[0];

	//render the description elements
	var author_name = hgd.getElementsByTagName( "author_name" )[0].childNodes[0].nodeValue;
	var author_name_div = textToDOM( "span", "written by " + author_name + " ", style_set + "_content_description_element" );
	var date_created = hgd.getElementsByTagName( "date_created" )[0].childNodes[0].nodeValue;
	var date_created_div = textToDOM( "span", "on " + date_created, style_set + "_content_description_element" );

	var description_div = document.createElement( 'div' );
	description_div.appendChild( author_name_div );
	description_div.appendChild( date_created_div );

	return description_div;

}


/**
* RenderHGContentTitleHelper
*
* A function to render HG Content Title from XML
**/
function RenderHGContentTitleHelper( x, editability, style_set ) {

	var title_div = RenderHGContentTitleHelperHelper( x, editability, style_set );

	var title_toggle_field = new EditableToggleField( 
		title_div, 
		null, //submit command 
		"new_title", 
		null, //refresh command
		800 ,
		style_set ,
		SimpleWidgetFactory ,
		false , 
		editability ,
		"Text"
	);

	return title_toggle_field;

}

/**
* RenderHGContentTitleHelperHelper
**/
function RenderHGContentTitleHelperHelper( x, editability, style_set ) { 

	var hgr = x.getElementsByTagName( "HGResponse" )[0];

	var hgd = hgr.getElementsByTagName( "HGDescription" )[0];

	//render the title and create an editable toggle field from it
	var title = hgd.getElementsByTagName( "content_title" )[0].childNodes[0].nodeValue;
	var title_div = textToDOM( "div", title, style_set + "_content_title" );

	return title_div;

}


/**
* loadRepliesButtonFactory
*
* A factory to create a Load Replies Button
*
**/
function loadRepliesButtonFactory( content_type_id, content_id, hg_content, reply_count ) {

	var rbut = new SimpleButtonWidgetFactory( hg_content.style_set, new HGContentRepliesForContentPCommand( content_type_id, content_id, hg_content ) ).build();

	var lrimg = document.createElement( 'img' );
	lrimg.src = 'img/page/navigation/get_reply.gif';

	rbut.addContentNode( lrimg );
	rbut.addContentNode( textToDOM( "span", " Get Replies (" + reply_count + ")" ) );

	return rbut;
}

/**
* HGContentRepliesForContentPCommand
*
* Command to make AJAX request for content reply
*
* @param content_type_id
* @param content_id
* @param hg_content
*
**/
function HGContentRepliesForContentPCommand( 
	content_type_id , 
	content_id , 
	hg_content
) {

	this.content_type_id = content_type_id;
	this.content_id = content_id;
	this.hg_content = hg_content;

}

new HGContentRepliesForContentPCommand();

HGContentRepliesForContentPCommand.prototype.execute = function() { 

	this.hg_content.setStatusToWorking( "loading replies" );

	//set up the HGRequest
	var nreq = new HGRequestObject( "HGContentRepliesForContentPCommand" );
	nreq.addRequestElement( 'content_type_id', this.content_type_id );
	nreq.addRequestElement( 'content_id', this.content_id );

	var af = AJAXRequestFunctionFactory( this.hg_content, handleHGContentRepliesForContentPRequest );
	XMLHTTPRequestCoordinator( "php/HGContentGetRequestC.php", af, "GET", nreq );

}

function handleHGContentRepliesForContentPRequest( x, t ) {

	//render the replies
	var replies = RenderHGContentRepliesForContent( x, t );

	//append the replies to the target
	t.reply_element.clearContents();
	t.reply_element.addContentNode( replies.getWidgetDOM() );

	t.clearStatus();

}

/**
* RenderHGContentRepliesForContent
*
* Render HG Content Replies
*
**/
function RenderHGContentRepliesForContent( x, hg_content ) { 

	var hgr = x.getElementsByTagName( 'HGResponse' )[0];

	var hgc = hgr.getElementsByTagName( 'HGCompositePageElement' );

	var rhgr = new hg_content.reply_container_container_factory( hg_content.style_set + "_reply_container_container" ).build();
	
	for ( var i=0; i<hgc.length; i++ ) { 
		rhgr.addContentNode( renderHGContentRepliesForContentHelper( hgc[i], hg_content ).getWidgetDOM() );
	}

	return rhgr;

}	

function renderHGContentRepliesForContentHelper( hgc, hg_content ) { 

	var hgs = hgc.getElementsByTagName( 'HGSignature' )[0];
	var hgd = hgc.getElementsByTagName( 'HGDescription' )[0];
	var con = hgc.getElementsByTagName( 'HGContent' )[0];

	var content_type_id = hgs.getElementsByTagName( 'content_type_id' )[0].childNodes[0].nodeValue;
	var content_id = hgs.getElementsByTagName( 'content_id' )[0].childNodes[0].nodeValue;
	var reply_id = hgs.getElementsByTagName( 'reply_id' )[0].childNodes[0].nodeValue;

	var author_name = hgd.getElementsByTagName( 'author_name' )[0].childNodes[0].nodeValue;
	var creator_id = hgd.getElementsByTagName( 'creator_id' )[0].childNodes[0].nodeValue;
	var date_created = hgd.getElementsByTagName( 'date_created' )[0].childNodes[0].nodeValue;

	var return_reply = new hg_content.reply_container_factory( hg_content.style_set + "_reply_container" ).build();
	
	var return_description = document.createElement( 'div' );
	return_description.appendChild( textToDOM( "span", "Replied to by ", hg_content.style_set + "_reply_description_element" ) );
	return_description.appendChild( textToDOM( "span", author_name + " ", hg_content.style_set + "_reply_description_element" ) );
	return_description.appendChild( textToDOM( "span", " on ", hg_content.style_set + "_reply_description_element" ) );
	return_description.appendChild( textToDOM( "span", date_created, hg_content.style_set + "_reply_description_element" ) );

	var reply_tf_content = RenderHGContentReplyForContentHelper( hgc );

	//check the editability
	var editability = ( hg_content.access_level == 1 && hg_content.user_id > 0 ) || ( hg_content.user_id == creator_id );

	var reply_tf = new EditableToggleField( 
		reply_tf_content,
		null, //submit command
		"new_reply",
		null, //refresh command
		null , //width
		hg_content.style_set , //style set
		SimpleWidgetFactory ,
		false ,
		editability ,
		"WYSIWYG"
	);

	reply_tf.setRefreshCommand( new HGContentReplyRefreshCommand( content_type_id, content_id, reply_id, reply_tf ) );
	reply_tf.setSubmitCommand( new HGContentReplySubmitCommand( content_type_id, content_id, reply_id, reply_tf ) );

	return_reply.addContentNode( return_description );
	return_reply.addContentNode( reply_tf.getDOM() );

	return return_reply;

}

function RenderHGContentReplyForContentHelper( hgc ) { 

	var con = hgc.getElementsByTagName( 'HGContent' )[0];

	var content = hgc.getElementsByTagName( 'content' )[0].childNodes[0].nodeValue;

	var content_dom = textToDOM( "div", content, "hg_reply" );

	return content_dom;

}

/**
* refreshContentButtonFactory
*
* A factory to create a Refresh Content Button
*
* @param hg_content
*
**/
function refreshContentButtonFactory( hg_content ) {

	var rbut = new SimpleButtonWidgetFactory( hg_content.style_set, new HGRefreshFullContentCommand( hg_content.content_type_id, hg_content.content_id, hg_content ) ).build();

	var rcimg = document.createElement( 'img' );
	rcimg.src = 'img/page/navigation/refresh_content.gif';

	rbut.addContentNode( rcimg );
	rbut.addContentNode( textToDOM( "span", " Refresh Post" ) );

	return rbut;
}

/**
* HGRefreshFullContentCommand
*
* Refresh the Content Title and the Content itself
*
* @param content_type_id
* @param content_id
* @param hg_content
**/
function HGRefreshFullContentCommand( content_type_id, content_id, hg_content ) { 

	this.content_type_id = content_type_id;
	this.content_id = content_id;
	this.hg_content = hg_content;
	
}

new HGRefreshFullContentCommand();

HGRefreshFullContentCommand.prototype.execute = function() { 

	this.hg_content.setStatusToWorking( "refreshing content" );

	//set up the HGRequest
	var nreq = new HGRequestObject( "HGContentPCommand" );
	nreq.addRequestElement( 'content_type_id', this.content_type_id );
	nreq.addRequestElement( 'content_id', this.content_id );

	var af = AJAXRequestFunctionFactory( this.hg_content, handleHGRefreshFullContentRequest );
	XMLHTTPRequestCoordinator( "php/HGContentGetRequestC.php", af, "GET", nreq );

}

function handleHGRefreshFullContentRequest( x, t ) {

	//pull the HGResponse element
	var hgr = x.getElementsByTagName( "HGResponse" )[0];

	//pull signature information from the XML
	var hgs = hgr.getElementsByTagName( "HGSignature" )[0];
	var content_type_id = hgs.getElementsByTagName( "content_type_id" )[0].childNodes[0].nodeValue;
	var content_id = hgs.getElementsByTagName( "content_id" )[0].childNodes[0].nodeValue;

	//pull the creator id
	var hgd = hgr.getElementsByTagName( "HGDescription" )[0];
	var creator_id = hgd.getElementsByTagName( "creator_id" )[0].childNodes[0].nodeValue;

	//check to determine the editability of the content
	var editability = ( t.access_level == 1 && t.user_id != 0 ) || ( t.user_id == creator_id );

	var content_toggle_field = RenderHGContentHelper( x, editability, t.style_set );
	content_toggle_field.setRefreshCommand( new HGContentContentRefreshCommand( content_type_id, content_id, content_toggle_field ) );
	content_toggle_field.setSubmitCommand( new HGContentSubmitCommand( content_type_id, content_id, content_toggle_field ) );

	var title_toggle_field = RenderHGContentTitleHelper( x, editability, t.style_set );
	title_toggle_field.setRefreshCommand( new HGContentTitleRefreshCommand( content_type_id, content_id, title_toggle_field ) );
	title_toggle_field.setSubmitCommand( new HGContentTitleSubmitCommand( content_type_id, content_id, title_toggle_field ) );

	var description_div = RenderHGContentDescriptionHelper( x, editability, t.style_set );

	t.content_element.clearContents();


	//create a content_container
	var content_container = new t.content_container_factory( t.style_set + "_content_container" ).build();

	content_container.addContentNode( title_toggle_field.getDOM() );
	content_container.addContentNode( description_div );
	content_container.addContentNode( content_toggle_field.getDOM() );

	//attach everything to the HGContent object
	t.content_element.addContentNode( content_container.getWidgetDOM() );

	t.clearStatus();

}

/**
* HGContentContentRefreshCommand
*
* A command to refresh content already rendered within an editable toggle field
*
* @param content_type_id
* @param content_id
* @param target The EditableToggleField we are refreshing
**/
function HGContentContentRefreshCommand( content_type_id, content_id, target ) { 

	this.content_type_id = content_type_id;
	this.content_id = content_id;
	this.target = target;

}

new HGContentContentRefreshCommand();

HGContentContentRefreshCommand.prototype.execute = function() { 

	//set up the HGRequest
	var nreq = new HGRequestObject( "HGContentPCommand" );
	nreq.addRequestElement( 'content_type_id', this.content_type_id );
	nreq.addRequestElement( 'content_id', this.content_id );

	var af = AJAXRequestFunctionFactory( this.target, handleHGContentRefreshRequest );
	XMLHTTPRequestCoordinator( "php/HGContentGetRequestC.php", af, "GET", nreq );

	//set status of the toggle field
	this.target.setStatusToWorking( "refreshing" );

}

function handleHGContentRefreshRequest( x, t ) { 

	var ref_content = RenderHGContentContentHelper( x, null, t.style_set );
	t.swapContent( ref_content );
	t.clearStatus();

}

/**
* HGContentSubmitCommand
*
* Submit edit to content
*
* @param content_type_id
* @param content_id
* @param toggle_field The EditableToggleField being submitted
**/
function HGContentSubmitCommand( content_type_id, content_id, toggle_field ) {

	this.content_type_id = content_type_id;
	this.content_id = content_id;
	this.toggle_field = toggle_field;

}

new HGContentSubmitCommand();

HGContentSubmitCommand.prototype.execute = function() { 

	//set up the HGRequest
	var nreq = new HGRequestObject( "HGUpdateContentCommand" );
	nreq.addRequestElement( 'new_content', this.toggle_field.activeEditor.getHTML() );
	nreq.addRequestElement( 'content_type_id', this.content_type_id );
	nreq.addRequestElement( 'content_id', this.content_id );

	//create the AJAX Request Function
	var af = AJAXRequestFunctionFactory( this.toggle_field, handleHGContentSubmitRequest );
	
	//make the AJAX Request
	XMLHTTPRequestCoordinator( "php/HGContentPostRequestC.php", af, "POST", nreq );

	this.toggle_field.setStatusToWorking( "saving" );

}

function handleHGContentSubmitRequest( x, t ) {

	var success = HGPopupMessage( x );

	if ( success == 1 ) { 

		t.refresh_command.execute();

	}

	else { 

		t.setStatusFailed( "save unsuccessful" );

	}

}

/**
* HGContentTitleRefreshCommand
*
* A command to refresh a content title already rendered within an editable toggle field
*
* @param content_type_id
* @param content_id
* @param target The EditableToggleField we are refreshing
**/
function HGContentTitleRefreshCommand( content_type_id, content_id, target ) { 

	this.content_type_id = content_type_id;
	this.content_id = content_id;
	this.target = target;

}

new HGContentTitleRefreshCommand();

HGContentTitleRefreshCommand.prototype.execute = function() { 

	//set up the HGRequest
	var nreq = new HGRequestObject( "HGContentPCommand" );
	nreq.addRequestElement( 'content_type_id', this.content_type_id );
	nreq.addRequestElement( 'content_id', this.content_id );

	var af = AJAXRequestFunctionFactory( this.target, handleHGContentTitleRefreshRequest );
	XMLHTTPRequestCoordinator( "php/HGContentGetRequestC.php", af, "GET", nreq );

	//set status of the toggle field
	this.target.setStatusToWorking( "refreshing" );

}

function handleHGContentTitleRefreshRequest( x, t ) { 

	var ref_content = RenderHGContentTitleHelperHelper( x, null, t.style_set );
	t.swapContent( ref_content );
	t.clearStatus();

}

/**
* HGContentTitleSubmitCommand
*
* Submit edit to content title
*
* @param content_type_id
* @param content_id
* @param toggle_field The EditableToggleField being submitted
**/
function HGContentTitleSubmitCommand( content_type_id, content_id, toggle_field ) {

	this.content_type_id = content_type_id;
	this.content_id = content_id;
	this.toggle_field = toggle_field;

}

new HGContentTitleSubmitCommand();

HGContentTitleSubmitCommand.prototype.execute = function() { 

	//set up the HGRequest
	var nreq = new HGRequestObject( "HGUpdateContentTitleCommand" );
	nreq.addRequestElement( 'new_title', this.toggle_field.activeEditor.getHGFormElement("toggle_text_editor").getValue() );
	nreq.addRequestElement( 'content_type_id', this.content_type_id );
	nreq.addRequestElement( 'content_id', this.content_id );

	//create the AJAX Request Function
	var af = AJAXRequestFunctionFactory( this.toggle_field, handleHGContentTitleSubmitRequest );
	
	//make the AJAX Request
	XMLHTTPRequestCoordinator( "php/HGContentPostRequestC.php", af, "POST", nreq );

	this.toggle_field.setStatusToWorking( "saving" );

}

function handleHGContentTitleSubmitRequest( x, t ) {

	var success = HGPopupMessage( x );

	if ( success == 1 ) { 

		t.refresh_command.execute();

	}

	else { 

		t.setStatusFailed( "save unsuccessful" );

	}

}



/**
* HGContentReplyRefreshCommand
*
* A command to refresh a single content reply
*
* @param content_type_id
* @param content_id
* @param reply_id
* @param target The EditableToggleField we are refreshing
**/
function HGContentReplyRefreshCommand( content_type_id, content_id, reply_id, target ) { 

	this.content_type_id = content_type_id;
	this.content_id = content_id;
	this.reply_id = reply_id;
	this.target = target;

}

new HGContentReplyRefreshCommand( null, null, null, null );

HGContentReplyRefreshCommand.prototype.execute = function() { 

	//set up the HGRequest
	var nreq = new HGRequestObject( "HGContentReplyForContentPCommand" );
	nreq.addRequestElement( 'content_type_id', this.content_type_id );
	nreq.addRequestElement( 'content_id', this.content_id );
	nreq.addRequestElement( 'reply_id', this.reply_id );

	var af = AJAXRequestFunctionFactory( this.target, handleHGContentReplyRefreshRequest );
	XMLHTTPRequestCoordinator( "php/HGContentGetRequestC.php", af, "GET", nreq );

	//set status of the toggle field
	this.target.setStatusToWorking( "refreshing" );

}

function handleHGContentReplyRefreshRequest( x, t ) { 

	var hgr = x.getElementsByTagName( "HGResponse" )[0];

	var ref_content = RenderHGContentReplyForContentHelper( hgr, t );
	t.swapContent( ref_content );
	t.clearStatus();

}

/**
* HGContentReplySubmitCommand
*
* Submit edit to content reply
*
* @param content_type_id
* @param content_id
* @param reply_id
* @param toggle_field The EditableToggleField being submitted
**/
function HGContentReplySubmitCommand( content_type_id, content_id, reply_id, toggle_field ) {

	this.content_type_id = content_type_id;
	this.content_id = content_id;
	this.reply_id = reply_id
	this.toggle_field = toggle_field;

}

new HGContentReplySubmitCommand( null, null, null );

HGContentReplySubmitCommand.prototype.execute = function() { 

	//set up the HGRequest
	var nreq = new HGRequestObject( "HGUpdateContentReplyCommand" );
	nreq.addRequestElement( 'new_reply', this.toggle_field.activeEditor.getHTML() );
	nreq.addRequestElement( 'content_type_id', this.content_type_id );
	nreq.addRequestElement( 'content_id', this.content_id );
	nreq.addRequestElement( 'reply_id', this.reply_id );

	//create the AJAX Request Function
	var af = AJAXRequestFunctionFactory( this.toggle_field, handleHGContentReplySubmitRequest );
	
	//make the AJAX Request
	XMLHTTPRequestCoordinator( "php/HGContentPostRequestC.php", af, "POST", nreq );

	this.toggle_field.setStatusToWorking( "saving" );

}

function handleHGContentReplySubmitRequest( x, t ) {

	var success = HGPopupMessage( x );

	if ( success == 1 ) { 

		t.refresh_command.execute();

	}

	else { 

		t.setStatusFailed( "save unsuccessful" );

	}

}




/**
* insertContentReplyButtonFactory
* 
* A factory to create an insert reply button for the current content
*
* @param content_type_id
* @param content_id
* @param target The HGContent Object
* @param style_set
**/
function insertContentReplyButtonFactory( content_type_id, content_id, target, style_set ) { 

	var rbut = new SimpleButtonWidgetFactory( style_set, new HGInsertNewContentReplyPopupCommand ( style_set, content_type_id, content_id, target, style_set ) ).build();

	var nrepimg = document.createElement( 'img' );
	nrepimg.src = 'img/page/navigation/new_reply.gif';

	rbut.addContentNode( nrepimg );
	rbut.addContentNode( textToDOM( "span", " Reply" ) );

	return rbut;

}

/**
* HGInsertNewContentReplyPopupCommand
*
* Create a popup window with the proper editing capabilities for inserting a new content reply.  This includes a WYSIWYG editor, 
* a SUBMIT button, and a CANCEL button
*
* @param style_set
* @param content_type_id
* @param content_id
* @param target the HGContent object
**/
function HGInsertNewContentReplyPopupCommand( style_set, content_type_id, content_id, target ) { 

	this.style_set = style_set;
	this.content_type_id = content_type_id;
	this.content_id = content_id;
	this.target = target;

}

new HGInsertNewContentReplyPopupCommand();

HGInsertNewContentReplyPopupCommand.prototype.execute = function() { 

	//create the popup window
	var pw = new SimplePopupWindow( 
		"new_reply_popup_window" ,
		"New Reply Popup Window" ,
		"hg_popup" , 
		300 , 
		600 , 
		15 ,
		true , 
		false ,
		false
	); 

	//create the window title
	var win_title = textToDOM( "span", "New Reply" );

	//add the title to the popup window
	pw.header.addContentNode( win_title );

	//create the WYSIWYG editor
	var wysiwyg = new WYSIWYGObject(200, 550 );

	//render the WYSIWYG editor
	var rwysiwyg = wysiwyg.render();

	//add the WYSIWYG editor to the popup window
	pw.body.addContentNode( rwysiwyg );

	//create the submit button
	var sbut = submitNewContentReplyButtonFactory( this.style_set, this.content_type_id, this.content_id, pw, wysiwyg );

	//add the submit button to the control bar of the popup window
	pw.addControlButton( sbut );

	//add the popup window to the windowing engine
	document.body.windowing_environment.addPopupWindow( pw );

	//turn on the editing
	setTimeout( EditingSetupClosure( wysiwyg, "" ) , 50 );

	//set the popup from parameter so that we can refresh the replies upon submition
	pw.popup_from = this.target;


}

/**
* submitNewContentReplyButtonFactory
*
* Create a button used to submit a new content reply
*
* @param style_set
* @param content_type_id
* @param content_id
* @param target the new reply popup window
* @param wysiwyg The wysiwyg editor object containing the new reply
**/
function submitNewContentReplyButtonFactory( style_set, content_type_id, content_id, target, wysiwyg ) { 

	var rbut = new SimpleButtonWidget( style_set, new HGInsertNewContentReplyCommand ( content_type_id, content_id, target, wysiwyg ) );

	rbut.addContentNode( textToDOM( "span", "submit" ) );

	return rbut;

}

/**
* HGInsertNewContentReplyCommand
*
* A command to insert a new content reply
* 
* @param content_type_id
* @param content_id
* @param target the new reply popup window
* @param wysiwyg
**/
function HGInsertNewContentReplyCommand( content_type_id, content_id, target, wysiwyg ) { 

	this.content_type_id = content_type_id;
	this.content_id = content_id;
	this.target = target;
	this.wysiwyg = wysiwyg;

}

new HGInsertNewContentReplyCommand();

HGInsertNewContentReplyCommand.prototype.execute = function() { 

	//set up the HGRequest
	var nreq = new HGRequestObject( "HGInsertNewContentReplyCommand" );
	nreq.addRequestElement( 'content_type_id', this.content_type_id );
	nreq.addRequestElement( 'content_id', this.content_id );
	nreq.addRequestElement( 'new_reply', this.wysiwyg.getHTML() );

	var af = AJAXRequestFunctionFactory( this.target, handleHGInsertNewContentReplyRequest );
	XMLHTTPRequestCoordinator( "php/HGContentPostRequestC.php", af, "POST", nreq );

	//set status of the toggle field
	this.target.setStatusToWorking( "inserting reply" );

}

function handleHGInsertNewContentReplyRequest( x, t ) { 

	var success = HGPopupMessage( x );

	if ( success == 1 ) { 

		//refresh replies
		var lrc = new HGContentRepliesForContentPCommand( t.popup_from.content_type_id, t.popup_from.content_id, t.popup_from );
		lrc.execute();

		//close popup window
		var cpw = new ClosePopupWindowCommand( document.body.windowing_environment, t );
		cpw.execute();

	}

	else { 

		t.setStatusFailed( "insert unsuccessful" );

	}

}


