/**
* HGComic
* 
* The HGComic object is responsible for the organization of the HGComic's displayed on the page
*
* @param style_set
*	-style_set_comic_container
*	-style_set_control_element
*	-style_set_comic_block
* @param comic_type_id
* @param comic_id
* @param user_id
* @param access_level
**/
function HGComic( 
	style_set,
	comic_block_factory,
	comic_container_factory, 
	comic_type_id, 
	comic_id, 
	user_id, 
	access_level 
) { 

	this.style_set = style_set;

	this.comic_block_factory = comic_block_factory;
	this.comic_container_factory = comic_container_factory;

	this.user_id = user_id;
	this.access_level = access_level;

	this.comic_type_id = comic_type_id;
	this.comic_id = comic_id;

	this.comic_element = new comic_container_factory( this.style_set + "_comic_container" ).build();
	this.comic_control_element = new TabularWidgetLayoutFactory( this.style_set + "_control_element", 5 ).build();
	this.hg_comic_block = new comic_block_factory( this.style_set + "_comic_block" ).build();
	
	//add the elements to the comic block
	this.hg_comic_block.addContentNode( this.comic_element.getWidgetDOM() );
	this.hg_comic_block.addContentNode( this.comic_control_element.getDOM() );
	
	//create the status widget
	this.status_widget = new StatusWidget( "status" );

	this.comic_control_element.addWidget( this.status_widget );

}

HGComic.prototype.setStatusToWorking = function ( msg ) { 

	this.status_widget.setStatus( "working", msg );
	alert( "here" );

}

HGComic.prototype.clearStatus = function() { 

	this.status_widget.hideWidget();

}

/**
* HGComicRenderCommand
*
* A command to render HGContent
*
* @param style_set
* @param comic_block_factory
* @param comic_container_factory
* @param user_id
* @param access_level
* @param x
**/
function HGComicRenderCommand( 
	style_set,
	comic_block_factory,
	comic_container_factory, 
	user_id, 
	access_level ,
	x
) { 

	this.style_set = style_set;

	this.comic_block_factory = comic_block_factory;
	this.comic_container_factory = comic_container_factory;

	this.x = x;
	this.user_id = user_id;
	this.access_level = access_level;

	return true;

}

new HGComicRenderCommand();

HGComicRenderCommand.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 comic_type_id = hgs.getElementsByTagName( "comic_type_id" )[0].childNodes[0].nodeValue;
	var comic_id = hgs.getElementsByTagName( "comic_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;

	//create the HGComic object
	var h = new HGComic( 
		"hg_comic", 		
		this.comic_block_factory ,
		this.comic_container_factory ,
		this.user_id, 
		this.access_level, 
		comic_type_id, 
		comic_id
	);

	//check to determine the editability of the comic
	var editability = ( this.access_level == 1 && this.user_id != 0 );

	var comic_toggle_field = RenderHGComicHelper( this.x, editability, this.style_set );
	comic_toggle_field.setRefreshCommand( new HGComicRefreshCommand( comic_type_id, comic_id, comic_toggle_field ) );
	comic_toggle_field.setSubmitCommand( new HGComicSubmitCommand( comic_type_id, comic_id, comic_toggle_field ) );

	var title_toggle_field = RenderHGComicTitleHelper( this.x, editability, this.style_set + "_title" );
	title_toggle_field.setRefreshCommand( new HGComicTitleRefreshCommand( comic_type_id, comic_id, title_toggle_field ) );
	title_toggle_field.setSubmitCommand( new HGComicTitleSubmitCommand( comic_type_id, comic_id, title_toggle_field ) );

	var description_div = RenderHGComicDescriptionHelper( this.x, editability, this.style_set );

	//create a comic_container
	var comic_container = new this.comic_container_factory( this.style_set + "_comic_container" ).build();

	comic_container.addContentNode( title_toggle_field.getDOM() );
	comic_container.addContentNode( description_div );
	comic_container.addContentNode( comic_toggle_field.getDOM() );

	//attach everything to the HGContent object
	h.comic_element.addContentNode( comic_container.getWidgetDOM() );

	return h;

}

/**
* RenderHGComicHelper
*
* A function to render HG Content From XML
**/
function RenderHGComicHelper( x, editability, style_set ) { 

	var hgr = x.getElementsByTagName( "HGResponse" )[0];

	//pull signature information from the XML
	var hgs = hgr.getElementsByTagName( "HGSignature" )[0];
	var comic_type_id = hgs.getElementsByTagName( "comic_type_id" )[0].childNodes[0].nodeValue;

	var hgd = hgr.getElementsByTagName( "HGDescription" )[0];

	var hgc = hgr.getElementsByTagName( "HGComic" )[0];
	var comic_src = hgc.getElementsByTagName( "file_nm" )[0].childNodes[0].nodeValue;

	var comic_div = RenderHGComicComicHelper( x, editability );

	var content_toggle_field = new EditableToggleField( 
		comic_div,
		null, //submit command
		"new_comic",
		null, //refresh command
		null , //width
		style_set , //style set
		SimpleWidgetFactory ,
		false ,
		editability ,
		"Text" ,
		new HGComicToggleFieldValuePopulationCommand( comic_src )
	);

	return content_toggle_field;

}

function RenderHGComicComicHelper( x, editability, style_set ) { 

	var hgr = x.getElementsByTagName( "HGResponse" )[0];
	var hgc = hgr.getElementsByTagName( "HGComic" )[0];
	var comic_src = hgc.getElementsByTagName( "file_nm" )[0].childNodes[0].nodeValue;
	var comic_img = document.createElement( 'img' );
	comic_img.src = "img/comic/" + comic_src;

	return comic_img;

}

/**
* HGComicToggleFieldValuePopulationCommand
* 
* Populates the text field of the HGComic Toggle Field
*
* @param c The comic_src
**/
function HGComicToggleFieldValuePopulationCommand( c ) {

	this.comic_src = c;

}

new HGComicToggleFieldValuePopulationCommand();

HGComicToggleFieldValuePopulationCommand.prototype.execute = function( t ) { 

	t.form_element.value = this.comic_src;

}

/**
* RenderHGComicDescriptionHelper
*
* A funtion to render HG Content Description from XML
**/
function RenderHGComicDescriptionHelper( x, editability, style_set ) { 

	var hgr = x.getElementsByTagName( "HGResponse" )[0];

	var hgd = hgr.getElementsByTagName( "HGDescription" )[0];

	//render the description elements
	var date_created = hgd.getElementsByTagName( "date_created" )[0].childNodes[0].nodeValue;
	var date_created_div = textToDOM( "span", date_created, style_set + "_comic_description_element" );

	var description_div = document.createElement( 'div' );
	description_div.appendChild( date_created_div );

	return description_div;

}


/**
* RenderHGComicTitleHelper
*
* A function to render HG Comic Title from XML
**/
function RenderHGComicTitleHelper( x, editability, style_set ) {

	var title_div = RenderHGComicTitleHelperHelper( 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;

}

/**
* RenderHGComicTitleHelperHelper
**/
function RenderHGComicTitleHelperHelper( 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( "comic_title" )[0].childNodes[0].nodeValue;
	var title_div = textToDOM( "div", title, style_set + "_comic_title" );

	return title_div;

}

/**
* HGComicTitleRefreshCommand
*
* A command to refresh a comic title already rendered within an editable toggle field
*
* @param comic_type_id
* @param comic_id
* @param target The EditableToggleField we are refreshing
**/
function HGComicTitleRefreshCommand( comic_type_id, comic_id, target ) { 

	this.comic_type_id = comic_type_id;
	this.comic_id = comic_id;
	this.target = target;

}

new HGComicTitleRefreshCommand();

HGComicTitleRefreshCommand.prototype.execute = function() { 

	//set up the HGRequest
	var nreq = new HGRequestObject( "HGComicPCommand" );
	nreq.addRequestElement( 'comic_type_id', this.comic_type_id );
	nreq.addRequestElement( 'comic_id', this.comic_id );

	var af = AJAXRequestFunctionFactory( this.target, handleHGComicTitleRefreshRequest );
	XMLHTTPRequestCoordinator( "php/HGContentGetRequestC.php", af, "GET", nreq );

	//set status of the toggle field
	this.target.setStatusToWorking( "refreshing" );

}

function handleHGComicTitleRefreshRequest( x, t ) { 

	var ref_comic = RenderHGComicTitleHelperHelper( x, null, t.style_set );
	t.swapContent( ref_comic );
	t.clearStatus();

}

/**
* HGComicTitleSubmitCommand
*
* Submit edit to comic title
*
* @param comic_type_id
* @param comic_id
* @param toggle_field The EditableToggleField being submitted
**/
function HGComicTitleSubmitCommand( comic_type_id, comic_id, toggle_field ) {

	this.comic_type_id = comic_type_id;
	this.comic_id = comic_id;
	this.toggle_field = toggle_field;

}

new HGComicTitleSubmitCommand();

HGComicTitleSubmitCommand.prototype.execute = function() { 

	//set up the HGRequest
	var nreq = new HGRequestObject( "HGUpdateComicTitleCommand" );
	nreq.addRequestElement( 'new_title', this.toggle_field.activeEditor.getHGFormElement("toggle_text_editor").getValue() );
	nreq.addRequestElement( 'comic_type_id', this.comic_type_id );
	nreq.addRequestElement( 'comic_id', this.comic_id );

	//create the AJAX Request Function
	var af = AJAXRequestFunctionFactory( this.toggle_field, handleHGComicTitleSubmitRequest );
	
	//make the AJAX Request
	XMLHTTPRequestCoordinator( "php/HGContentPostRequestC.php", af, "POST", nreq );

	this.toggle_field.setStatusToWorking( "saving" );

}

function handleHGComicTitleSubmitRequest( x, t ) {

	var success = HGPopupMessage( x );

	if ( success == 1 ) { 

		t.refresh_command.execute();

	}

	else { 

		t.setStatusFailed( "save unsuccessful" );

	}

}

/**
* HGComicRefreshCommand
*
* A command to refresh comic already rendered within an editable toggle field
*
* @param comic_type_id
* @param comic_id
* @param target The EditableToggleField we are refreshing
**/
function HGComicRefreshCommand( comic_type_id, comic_id, target ) { 

	this.comic_type_id = comic_type_id;
	this.comic_id = comic_id;
	this.target = target;

}

new HGComicRefreshCommand();

HGComicRefreshCommand.prototype.execute = function() { 

	//set up the HGRequest
	var nreq = new HGRequestObject( "HGComicPCommand" );
	nreq.addRequestElement( 'comic_type_id', this.comic_type_id );
	nreq.addRequestElement( 'comic_id', this.comic_id );

	var af = AJAXRequestFunctionFactory( this.target, handleHGComicRefreshRequest );
	XMLHTTPRequestCoordinator( "php/HGContentGetRequestC.php", af, "GET", nreq );

	//set status of the toggle field
	this.target.setStatusToWorking( "refreshing" );

}

function handleHGComicRefreshRequest( x, t ) { 

	var ref_comic = RenderHGComicComicHelper( x, null, t.style_set );
	t.swapContent( ref_comic );
	t.clearStatus();

}

/**
* HGComicSubmitCommand
*
* Submit edit to content
*
* @param comic_type_id
* @param comic_id
* @param toggle_field The EditableToggleField being submitted
**/
function HGComicSubmitCommand( comic_type_id, comic_id, toggle_field ) {

	this.comic_type_id = comic_type_id;
	this.comic_id = comic_id;
	this.toggle_field = toggle_field;

}

new HGComicSubmitCommand();

HGComicSubmitCommand.prototype.execute = function() { 

	//set up the HGRequest
	var nreq = new HGRequestObject( "HGUpdateComicCommand" );
	nreq.addRequestElement( 'new_file_nm', this.toggle_field.activeEditor.getHGFormElement("toggle_text_editor").getValue() );
	nreq.addRequestElement( 'comic_type_id', this.comic_type_id );
	nreq.addRequestElement( 'comic_id', this.comic_id );

	//create the AJAX Request Function
	var af = AJAXRequestFunctionFactory( this.toggle_field, handleHGComicSubmitRequest );
	
	//make the AJAX Request
	XMLHTTPRequestCoordinator( "php/HGContentPostRequestC.php", af, "POST", nreq );

	this.toggle_field.setStatusToWorking( "saving" );

}

function handleHGComicSubmitRequest( x, t ) {

	var success = HGPopupMessage( x );

	if ( success == 1 ) { 

		t.refresh_command.execute();

	}

	else { 

		t.setStatusFailed( "save unsuccessful" );

	}

}




