/**
* HGComicContext
*
* Organization for the HGComicContext.  The HGComicContext is a composite of three widgets:
*  -Context Level Control Widget
*  -HGComic Widget
*  -Context Level Navigation Control Widget
*
* @param context_style_set
* @param user_id
* @param access_level
**/
function HGComicContext( style_set, user_id, access_level, hg_page ) { 

	this.style_set = style_set;
	this.user_id = user_id;
	this.access_level = access_level;
	this.comic_type_id = 1;
	this.comic_id = 0;

	this.assoc_hg_page = hg_page;

	//create the three widgets
	this.context_controls = new TabularWidgetLayoutFactory( this.style_set + "_context_controls", 10 ).build();
	this.comic = new SimpleWidgetFactory( this.style_set + "_comic" ).build();
	this.context_navigation = new TabularWidgetLayoutFactory( this.style_set + "_context_navigation", 5 ).build();
	this.context_navigation_container = new SimpleWidgetFactory( this.style_set + "_context_navigation" ).build();
	this.context_container = new SimpleWidgetFactory( this.style_set + "_context_container" ).build();

	//create the status field

	//create context controls buttons
	this.show_context_button = showComicContextButtonFactory( this );
	this.hide_context_button = hideComicContextButtonFactory( this );

	this.associated_content_button = associatedContentButtonFactory( this );
	this.associated_content_button.hideWidget();

	this.comic_list_button = comicListButtonFactory( this );

	this.insert_comic_button = HGInsertNewComicPopupButtonFactory( this );
	this.create_assoc_button = HGComicContentAssociationPopupButtonFactory( this );

	if ( user_id <= 0 || access_level != 1 ) {
		this.insert_comic_button.hideWidget();
		this.create_assoc_button.hideWidget();
	}

	//add buttons to the context controls
	this.context_controls.addWidget( this.show_context_button );
	this.context_controls.addWidget( this.hide_context_button );
	this.context_controls.addWidget( this.associated_content_button );

	this.context_controls.addWidget( this.comic_list_button );
	this.context_controls.addWidget( this.insert_comic_button );
	this.context_controls.addWidget( this.create_assoc_button );

	
	//add status field to the context controls
	this.status_widget = new StatusWidget( "status" );
	this.context_controls.addWidget( this.status_widget );

	//add widgets to the container
	this.context_container.addContentNode( this.context_controls.getDOM() );
	this.context_container.addContentNode( this.comic.getWidgetDOM() );
	this.context_navigation_container.addContentNode( this.context_navigation.getDOM() );
	this.context_container.addContentNode( this.context_navigation_container.getWidgetDOM() );

}

new HGComicContext();

/**
* HGComicContext.loadComic
*
* Make and execute a HGComicPCommand for the passed in comic_type_id and comic_id
*
* @param comic_type_id
* @param comic_id
**/
HGComicContext.prototype.loadComic = function( comic_type_id, comic_id ) { 

	this.setControlConfigToShown();

	var hcpc = new HGComicPCommand( comic_type_id, comic_id, this );
	hcpc.execute();

}

/**
* HGComicContext.resetUserInfo
*
* Used when a user logs on or off.  Changes the user_id and the access_level associated with the context and then reloads the comic within the 
* context
*
* @param user_id
* @param access_level
**/
HGComicContext.prototype.resetUserInfo = function( user_id, access_level ) { 

	this.user_id = user_id;
	this.access_level = access_level;

	this.resetComicAccess();

	this.loadComic( this.comic_type_id, this.comic_id );

}

/**
* HGComicContext.resetComicAccess
**/
HGComicContext.prototype.resetComicAccess = function () { 

	if ( this.user_id <= 0 || this.access_level > this.comic_access || this.access_level <= 0 ) {
		this.insert_comic_button.hideWidget();
		this.create_assoc_button.hideWidget();
	}
	else {
		this.insert_comic_button.showWidget();
		this.create_assoc_button.showWidget();
	}

}

/**
* HGComicContext.addNavigationButton
*
* Add a navigation button the the navigation widget of the comic context
*
* @param b The button to add
**/
HGComicContext.prototype.addNavigationButton = function( b ) { 

	this.context_navigation.addWidget( b );

}

/**
* HGComicContext.setStatusToWorking
**/
HGComicContext.prototype.setStatusToWorking = function( msg ) { 

	this.status_widget.setStatus( "working", msg );

}

/**
* HGComicContext.clearStatus
**/
HGComicContext.prototype.clearStatus = function() { 

	this.status_widget.hideWidget();

}

/**
* HGComicContext.hideContext
*
* Hide everything in the context except for the controls
**/
HGComicContext.prototype.hideContext = function() { 

	this.comic.hideWidget();
	this.context_navigation_container.hideWidget();

	this.setControlConfigToHidden();

}

/**
* HGComicContext.showContext
*
* Hide everything in the context except for the controls
**/
HGComicContext.prototype.showContext = function() { 

	this.comic.showWidget();
	this.context_navigation_container.showWidget();

	this.setControlConfigToShown();

}

/**
* HGComicContext.setControlConfigToShown
**/
HGComicContext.prototype.setControlConfigToShown = function() { 

	this.show_context_button.hideWidget();
	this.hide_context_button.showWidget();

}

/**
* HGComicContext.setControlConfigToHidden
**/
HGComicContext.prototype.setControlConfigToHidden = function() { 

	this.show_context_button.showWidget();
	this.hide_context_button.hideWidget();

}

/**
* HGComicContext.updateAssociatedContentButton
*
* Creates the load content command which the load associated content button will call and shows the button
*
* @param assoc_content_type_id
* @param assoc_content_id
**/
HGComicContext.prototype.updateAssociatedContentButton = function( assoc_content_type_id, assoc_content_id ) {

	this.associated_content_button.setCommand( new LoadAssociatedContentCommand( this.assoc_hg_page, assoc_content_type_id, assoc_content_id ) );
	this.showAssociatedContentButton();

}

/**
* HGComicContext.hideAssociatedContentButton
**/
HGComicContext.prototype.hideAssociatedContentButton = function() { 

	this.associated_content_button.hideWidget();

}

/**
* HGComicContext.showAssociatedContentButton
**/
HGComicContext.prototype.showAssociatedContentButton = function() { 

	this.associated_content_button.showWidget();

}	

/**
* HGComicPCommand
*
* A command to make a HGComicPCommand request to the server
*
* @param comic_type_id
* @param comic_id
* @param target the HGComicContextModule object from which we are making the request
**/
function HGComicPCommand( comic_type_id, comic_id, target ) { 

	this.comic_type_id = comic_type_id;
	this.comic_id = comic_id;
	this.target = target;

}

new HGComicPCommand();

HGComicPCommand.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, handleHGComicPCommand );
	XMLHTTPRequestCoordinator( "php/HGContentGetRequestC.php", af, "GET", nreq );

	//set status of the toggle field
	this.target.setStatusToWorking( "Loading..." );

}

function handleHGComicPCommand( x, t ) {

	t.clearStatus();

	//dump the current comic
	t.comic.clearContents();
	t.context_navigation.clearWidgets();

	var hgr = x.getElementsByTagName( "HGResponse" )[0];
	var hgs = hgr.getElementsByTagName( "HGSignature" )[0];

	//pull comic_type_id and comic_id and set the appropriate fields in the target
	var comic_type_id = hgs.getElementsByTagName( "comic_type_id" )[0].childNodes[0].nodeValue;
	var comic_id = hgs.getElementsByTagName( "comic_id" )[0].childNodes[0].nodeValue;
	var comic_access = hgs.getElementsByTagName( "comic_access" )[0].childNodes[0].nodeValue;

	//create the load associated content button if apporpriate
	if ( hgs.getElementsByTagName( "assoc_content_type_id" )[0] ) {
		var assoc_content_type_id = hgs.getElementsByTagName( "assoc_content_type_id" )[0].childNodes[0].nodeValue;
		var assoc_content_id = hgs.getElementsByTagName( "assoc_content_id" )[0].childNodes[0].nodeValue;
		t.updateAssociatedContentButton( assoc_content_type_id, assoc_content_id );
	}
	else {
		t.hideAssociatedContentButton();
	}

	t.comic_type_id = comic_type_id;
	t.comic_id = comic_id;
	t.comic_access = comic_access;

	/**
	t.resetComicAccess();
	**/

	//create the HGComic
	var hgcrc = new HGComicRenderCommand( 
		"hg_comic", 
		SimpleWidgetFactory, 
		SimpleWidgetFactory, 
		t.user_id,
		t.access_level,
		x
	);

	var hgc = hgcrc.execute();

	//attach the comic to the HGComicContext
	t.comic.addContentNode( hgc.hg_comic_block.getWidgetDOM() );

	//generate the context level controls

	//generate the navigation
	comicContextNavigationRenderHelper( x, t );

	//set context to shown
	t.showContext();

	//set cookies
	createCookie( "last_comic_type_id", comic_type_id, 0 );
	createCookie( "last_comic_id", comic_id, 0 );

}

/**
* comicContextNavigationRenderHelper
*
* Render the buttons necessary for the HGComicContext
*
* @param x
* @param t The HGComicContext to add the navigation buttons to
**/
function comicContextNavigationRenderHelper( x, t ) { 

	var hgr = x.getElementsByTagName( "HGResponse" )[0];
	var hgn = hgr.getElementsByTagName( "HGNavigation" )[0];
	var hgnpb = hgn.getElementsByTagName( "hgComic_previousButton" )[0];
	var hgnnb = hgn.getElementsByTagName( "hgComic_nextButton" )[0];

	if ( hgnpb ) {
		var hgnpb_but = comicContextNavigationButtonFactory( hgnpb, t, "prev" );
		t.addNavigationButton( hgnpb_but );
	}
	if ( hgnnb ) {
		var hgnnb_but = comicContextNavigationButtonFactory( hgnnb, t, "next" );
		t.addNavigationButton( hgnnb_but );
	}

}

/**
* comicContextNavigationButtonFactory
*
* A factory for building buttons to use in the comic context navigation.  
*
* @param xsig the xml object creating the HGSignature from which the button should be built
* @param target the Target of the buttons command
* @param button_content The string to use as the buttons DOM contents
**/
function comicContextNavigationButtonFactory( xsig, target, style_set_appender ) {

	var hgs = xsig.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;

	var rbut = new SimpleButtonWidget( target.style_set + "_navigation_" + style_set_appender, new HGComicPCommand( comic_type_id, comic_id, target ) );

	//rbut.addContentNode( textToDOM( "span", button_content ) );

	return rbut;

}

/**
* hideComicContextButtonFactory
*
* @param c The context we are hiding
**/
function hideComicContextButtonFactory( c ) { 

	var rbut = new SimpleButtonWidget( c.style_set, new HideComicContextCommand( c ) );

	var ncimg = document.createElement( 'img' );
	ncimg.src = 'img/page/navigation/hide_comic.gif';
	ncimg.alt = "Hide Comic";

	rbut.addContentNode( ncimg );

	rbut.addContentNode( textToDOM( "span", " hide comic" ) );

	return rbut;

}

/**
* HideComicContextCommand
*
* @param t The context to hide
**/
function HideComicContextCommand( t ) { 

	this.target = t;

}

new HideComicContextCommand();

HideComicContextCommand.prototype.execute = function() { 

	//set the cookie for element shown
	createCookie( "comic_context_shown", 0, 0 );

	this.target.hideContext();

}

/**
* showComicContextButtonFactory
*
* @param c The context we are showing
**/
function showComicContextButtonFactory( c ) { 

	var rbut = new SimpleButtonWidget( c.style_set, new ShowComicContextCommand( c ) );

	var ncimg = document.createElement( 'img' );
	ncimg.src = 'img/page/navigation/show_comic.gif';
	ncimg.alt = "Show Comic";

	rbut.addContentNode( ncimg );

	rbut.addContentNode( textToDOM( "span", " show comic" ) );

	return rbut;

}

/**
* ShowComicContextCommand
*
* @param t The context to show
**/
function ShowComicContextCommand( t ) { 

	this.target = t;

}

new ShowComicContextCommand();

ShowComicContextCommand.prototype.execute = function() { 

	//set the cookie for element shown
	createCookie( "comic_context_shown", 1, 0 );

	this.target.showContext();

}

/**
* comicListButtonFactory
*
* @param t The context we are building the button from
*
* @returns A button which will load a comic list into the list context of the associated hg page of the current context
**/
function comicListButtonFactory( t ) {

	var rbut = new SimpleButtonWidget( t.style_set, new HGComicListGenerateCommand( t.assoc_hg_page, t ) );

	var ncimg = document.createElement( 'img' );
	ncimg.src = 'img/page/navigation/get_comic_list.gif';
	ncimg.alt = "Load Comic List";

	rbut.addContentNode( ncimg );

	rbut.addContentNode( textToDOM( "span", " load comic list" ) );

	return rbut;

}

/**
* HGInsertNewComicPopupButtonFactory
*
* @param t The HG_Page we are inserting the comic into
*/
function HGInsertNewComicPopupButtonFactory( t ) { 

	var rbut = new SimpleButtonWidget( t.style_set, new HGInsertNewComicPopupCommand( t ) );

	var ncimg = document.createElement( 'img' );
	ncimg.src = 'img/page/navigation/new_comic.gif';
	ncimg.alt = "Insert Comic";

	rbut.addContentNode( ncimg );

	rbut.addContentNode( textToDOM( "span", " new comic" ) );

	return rbut;

}

function HGInsertNewComicPopupCommand( t ) { 

	this.comic_context = t;

}

new HGInsertNewComicPopupCommand();

HGInsertNewComicPopupCommand.prototype.execute = function() {

	//create the popup window
	var pw = new SimplePopupWindow( 
		"insert_comic_popup_window" ,
		"Insert Comic Popup Window" ,
		"hg_popup" , 
		300 , 
		600 , 
		15 ,
		true , 
		false ,
		false
	);



	//create the window title
	var win_title = textToDOM( "span", "Insert New Comic" );

	//add the title to the popup window
	pw.header.addContentNode( win_title );

	//create the logon form
	var new_comic_form = new HGForm();

	var comic_title = new HGFormElement ( 
		"text" ,
		"" , 
		"new_title" ,
		"20" ,
		"1" ,
		"20" ,
		null ,
		null
	);

	var comic_name = new HGFormElement ( 
		"text" ,
		"" , 
		"new_file_nm" ,
		"20" ,
		"1" ,
		"20" ,
		null ,
		null
	);

	new_comic_form.addHGFormElement( comic_title );
	new_comic_form.addHGFormElement( comic_name );

	pw.body.addContentNode( new_comic_form.form );

	//create the submit button
	var sbut = submitHGInsertNewComicButtonFactory( 
		this.comic_context.style_set, 
		pw, 
		this.comic_context.comic_type_id, 
		new_comic_form
	);

	//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 );

	//set the popup from parameter
	pw.popup_from = this.comic_context;

}

/**
* submitHGInsertNewComicButtonFactory
**/
function submitHGInsertNewComicButtonFactory( style_set, pw, comic_type_id, new_comic_form ) { 

	var rbut = new SimpleButtonWidget( 
		style_set ,
		new HGInsertNewComicCommand ( 
			pw, 
			new_comic_form.getHGFormElement( "new_title" ) , 
			new_comic_form.getHGFormElement( "new_file_nm" ) ,
			comic_type_id
		) 
	);

	rbut.addContentNode( textToDOM( "span", "submit" ) );

	return rbut;

}

/**
* HGInsertNewComicCommand
*
* Generates the server request to add a new comic to the HG_Page
*
* @param pw The popup window generating the request
* @param new_title_field
* @param new_file_nm_field
* @param comic_type_id
**/
function HGInsertNewComicCommand( pw, new_title_field, new_file_nm_field, comic_type_id ) { 

	this.popup_window = pw;
	this.new_title_field = new_title_field;
	this.new_file_nm_field = new_file_nm_field;
	this.comic_type_id = comic_type_id;

}

new HGInsertNewComicCommand();

HGInsertNewComicCommand.prototype.execute = function() { 

	//set up the HGRequest
	var nreq = new HGRequestObject( "HGInsertNewComicCommand" );
	nreq.addRequestElement( 'comic_type_id', this.comic_type_id );
	nreq.addRequestElement( 'new_title', this.new_title_field.getValue() );
	nreq.addRequestElement( 'new_file_nm', this.new_file_nm_field.getValue() );

	var af = AJAXRequestFunctionFactory( this.popup_window, handleHGInsertNewComicRequest );
	XMLHTTPRequestCoordinator( "php/HGContentPostRequestC.php", af, "POST", nreq );

	//set status of the toggle field
	this.popup_window.setStatusToWorking( "inserting comic" );

}

function handleHGInsertNewComicRequest( x, t ) { 

	var success = HGPopupMessage( x );

	if ( success == 1 ) { 

		//refresh comic
		var lcc = new HGComicPCommand( t.popup_from.comic_type_id, 0, t.popup_from );
		lcc.execute();

		//close popup window
		var cpw = new ClosePopupWindowCommand( document.body.windowing_environment, t );
		cpw.execute();

	}

	else { 

		t.setStatusFailed( "insert unsuccessful" );

	}

}

/**
* associatedContentButtonFactory
**/
function associatedContentButtonFactory( hg_page ) { 

	var rbut = new SimpleButtonWidget( 
		hg_page.style_set ,
		null 
	);

	var ncimg = document.createElement( 'img' );
	ncimg.src = 'img/page/navigation/get_post.gif';
	ncimg.alt = "Get Associated Post";

	rbut.addContentNode( ncimg );

	rbut.addContentNode( textToDOM( "span", " get the post" ) );

	return rbut;

}

/**
* LoadAssociatedContentCommand
*
* A command to load content in the content context.  This is an inner context command
*
* @param hg_page The HG_Page that the content context is in
* @param content_type_id
* @param content_id
**/
function LoadAssociatedContentCommand( hg_page, content_type_id, content_id ) { 

	this.hg_page = hg_page;
	this.content_type_id = content_type_id;
	this.content_id = content_id;

}

new LoadAssociatedContentCommand();

LoadAssociatedContentCommand.prototype.execute = function() { 

	this.hg_page.content_context.loadContent( this.content_type_id, this.content_id );

}

/**
* HGComicContentAssociationPopupButtonFactory
*
* Create a popup window with selections available for content_type_id and content_id to user as the associated content
*
* @param hg_page_comic_context
**/
function HGComicContentAssociationPopupButtonFactory( hg_page_comic_context ) { 

	var rbut = new SimpleButtonWidget( hg_page_comic_context.style_set, new HGComicContentAssociationPopupCommand( hg_page_comic_context ) );

	rbut.addContentNode( textToDOM( "span", "create content association" ) );

	return rbut;

}

function HGComicContentAssociationPopupCommand( t ) { 

	this.hg_page_comic_context = t;

}

new HGComicContentAssociationPopupCommand();

HGComicContentAssociationPopupCommand.prototype.execute = function() {

	//create the popup window
	var pw = new SimplePopupWindow( 
	"comic_content_association_creation_window" ,
	"Create Comic Content Association" ,
	"hg_popup" , 
	200 , 
	300 , 
	15 ,
	true , 
	false ,
	false
	); 

	//create the window title
	var win_title = textToDOM( "span", "Create Comic Content Association" );

	//add the title to the popup window
	pw.header.addContentNode( win_title );

	//create the content title form
	var comic_content_association = new HGForm();

	var content_type_id_form_element = new HGFormElement ( 
		"selection" ,
		null , 
		"content_type_id" ,
		null ,
		null ,
		null ,
		PopulateContentIdFormElementCommand , //<-- state change command 
		new HGContentTypeListPCommand_F()
	);

	var content_id_form_element = new HGFormElement ( 
		"selection" ,
		null ,	
		"content_id" ,
		null , 
		null ,
		null ,
		null ,
		null ,
		null
	);

	comic_content_association.addHGFormElement( content_type_id_form_element );
	comic_content_association.addHGFormElement( content_id_form_element );

	pw.body.addContentNode( comic_content_association.form );

	//create the submit button
	var sbut = submitHGComicContentAssociationButtonFactory( pw, this.hg_page_comic_context, comic_content_association, this.hg_page_comic_context.comic_type_id, this.hg_page_comic_context.comic_id );

	//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 );

	//set the popup from parameter
	pw.popup_from = this.hg_page_comic_context;

}


/**
* HGContentTypeListPCommand_F
*
* Command to pull a HGContentTypeList and render it in the form necessary for an HGFormElement Selection box
*
**/
function HGContentTypeListPCommand_F() { }

new HGContentTypeListPCommand_F();

HGContentTypeListPCommand_F.prototype.execute = function( target ) { 

	//set up the HGRequest
	var nreq = new HGRequestObject( "HGContentTypeListPCommand" );

	//create the AJAX Request Function
	var af = AJAXRequestFunctionFactory( target, handleHGContentTypeListPCommand_F );
	
	//make the AJAX Request
	XMLHTTPRequestCoordinator( "php/HGContentGetRequestC.php", af, "GET", nreq );

}

function handleHGContentTypeListPCommand_F( x, t ) { 

	//grab the list of links from the XML
	var ll = x.getElementsByTagName( 'HGResponse' )[0].getElementsByTagName( 'HGLinkList' )[0].getElementsByTagName( 'HGLink' );

	//create a default selection
	t.form_element.options.add( new Option( "-Content Type-", 0, true ) );

	//generate the remaining selections from the XML
	for( var i=0; i<ll.length; i++ ) { 
		
		var opt_desc = ll[i].getElementsByTagName( 'HGLinkText' )[0].childNodes[0].nodeValue;
		var opt_value = ll[i].getElementsByTagName( 'HGSignature' )[0].getElementsByTagName( 'content_type_id' )[0].childNodes[0].nodeValue;

		t.form_element.options.add( new Option( opt_desc, opt_value ) );

	}

	return true;

}

/**
* PopulateContentIdFormElementCommand
*
* Creates a HGContentListPCommand_F command based on the content_type_id value of the content_type_id field of the associatedHGForm attached
* to the calling field
**/
function PopulateContentIdFormElementCommand( form_element ) {

	this.form_element = form_element;

}

new PopulateContentIdFormElementCommand();

PopulateContentIdFormElementCommand.prototype.execute = function() { 

	var form = this.form_element.associatedHGForm;

	var content_type_id = this.form_element.getValue();

	var content_id_field = form.getHGFormElement( "content_id" );

	content_id_field.repopulate( new HGContentListPCommand_F ( content_type_id ) );

}

/**
* HGContentListPCommand_F
*
* Command to pull a HGContentList and render it in the form necessary for an HGFormElement Selection box
*
**/
function HGContentListPCommand_F( content_type_id ) { 

	this.content_type_id = content_type_id;

}

new HGContentListPCommand_F();

HGContentListPCommand_F.prototype.execute = function( target ) { 

	//set up the HGRequest
	var nreq = new HGRequestObject( "HGContentListPCommand" );
	nreq.addRequestElement( 'content_type_id', this.content_type_id );

	//create the AJAX Request Function
	var af = AJAXRequestFunctionFactory( target, handleHGContentListPCommand_F );
	
	//make the AJAX Request
	XMLHTTPRequestCoordinator( "php/HGContentGetRequestC.php", af, "GET", nreq );

}

function handleHGContentListPCommand_F( x, t ) { 

	//grab the list of links from the XML
	var ll = x.getElementsByTagName( 'HGResponse' )[0].getElementsByTagName( 'HGLinkList' )[0].getElementsByTagName( 'HGLink' );

	//create a default selection
	t.form_element.options.add( new Option( "-Content-", 0, true ) );

	//generate the remaining selections from the XML
	for( var i=0; i<ll.length; i++ ) { 
		
		var opt_desc = ll[i].getElementsByTagName( 'HGLinkText' )[0].childNodes[0].nodeValue;
		var opt_value = ll[i].getElementsByTagName( 'HGSignature' )[0].getElementsByTagName( 'content_id' )[0].childNodes[0].nodeValue;

		t.form_element.options.add( new Option( opt_desc, opt_value ) );

	}

	return true;

}

/**
* submitHGComicContentAssociationButtonFactory
**/
function submitHGComicContentAssociationButtonFactory( pw, hg_page_comic_context, comic_content_association_form, comic_type_id, comic_id ) { 

	var rbut = new SimpleButtonWidget( hg_page_comic_context.style_set, new HGInsertNewComicContentAssocCommand( pw, hg_page_comic_context, comic_content_association_form, comic_type_id, comic_id ) );

	rbut.addContentNode( textToDOM( "span", "submit" ) );

	return rbut;

}

/**
* HGInsertNewComicContentAssocCommand
*
* Generates the server request to add a new comic content association
*
* @param pw The popup window generating the request
* @param hg_page_comic_context
* @param comic_content_association_form
* @param comic_type_Id
* @param comic_id
**/
function HGInsertNewComicContentAssocCommand( pw, hg_page_comic_context, comic_content_association_form, comic_type_id, comic_id ) { 

	this.popup_window = pw;
	this.hg_page_comic_context = hg_page_comic_context;
	this.comic_content_association_form = comic_content_association_form;
	this.comic_type_id = comic_type_id;
	this.comic_id = comic_id;

}

new HGInsertNewComicContentAssocCommand();

HGInsertNewComicContentAssocCommand.prototype.execute = function() { 

	//set up the HGRequest
	var nreq = new HGRequestObject( "HGInsertNewComicContentAssocCommand" );
	nreq.addRequestElement( 'comic_type_id', this.comic_type_id );
	nreq.addRequestElement( 'comic_id', this.comic_id );
	nreq.addRequestElement( 'content_type_id', this.comic_content_association_form.getHGFormElement( "content_type_id" ).getValue() );
	nreq.addRequestElement( 'content_id', this.comic_content_association_form.getHGFormElement( "content_id" ).getValue() ); 

	var af = AJAXRequestFunctionFactory( this.popup_window, handleHGInsertNewComicContentAssociation );
	XMLHTTPRequestCoordinator( "php/HGContentPostRequestC.php", af, "POST", nreq );

	//set status of the toggle field
	this.popup_window.setStatusToWorking( "creating association" );

}

function handleHGInsertNewComicContentAssociation( x, t ) { 

	var success = HGPopupMessage( x );

	if ( success == 1 ) { 

		//refresh content
		var lcc = new HGComicPCommand( t.popup_from.comic_type_id, t.popup_from.comic_id, t.popup_from );
		lcc.execute();

		//close popup window
		var cpw = new ClosePopupWindowCommand( document.body.windowing_environment, t );
		cpw.execute();

	}

	else { 

		t.setStatusFailed( "association unsuccessful" );

	}

}

