/**
* HGContentContext
*
* Organization for the HGContentContext.  The HGContentContext is a composite of three widgets:
*  -Context Level Control Widget
*  -HGContent Widget
*  -Context Level Navigation Control Widget
*
* @param style_set
* @param user_id
* @param access_level
**/
function HGContentContext( style_set, user_id, access_level, hg_page ) { 

	this.style_set = style_set;
	this.user_id = parseInt( user_id );
	this.access_level = parseInt( access_level );
	this.content_type_id = 1;
	this.content_id = 0;

	this.assoc_hg_page = hg_page;

	//create the context layout
	this.context_layout = new SimpleWidgetFactory( this.style_set + "_context_layout" ).build();

	//create the three widgets
	this.context_controls = new TabularWidgetLayoutFactory( this.style_set + "_context_controls", 1 ).build();
	this.context_controls_container = new SimpleWidgetFactory( this.style_set + "_context_controls" ).build();
	this.context_navigation = new TabularWidgetLayoutFactory( this.style_set + "_context_navigation", 1 ).build();
	this.context_navigation_container = new SimpleWidgetFactory( this.style_set + "_context_controls" ).build();

	this.content = new SimpleWidgetFactory( this.style_set + "_content" ).build();
	this.context_container = new SimpleWidgetFactory( this.style_set + "_context_container" ).build();

	//create the status field

	//create context controls buttons
	this.show_context_button = showContentContextButtonFactory( this );
	this.hide_context_button = hideContentContextButtonFactory( this );

	this.associated_comic_button = associatedComicButtonFactory( this );
	this.associated_comic_button.hideWidget();

	this.content_list_button = contentListButtonFactory( this );

	this.insert_content_button = HGInsertNewContentPopupButtonFactory( this );
	this.create_assoc_button = HGContentComicAssociationPopupButtonFactory( this );

	if ( user_id <= 0 || access_level != 1 ) {
		this.insert_content_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_comic_button );
	this.context_controls.addWidget( this.content_list_button );
	this.context_controls.addWidget( this.insert_content_button );
	this.context_controls.addWidget( this.create_assoc_button );

	//add status field to the context controls
	this.status_widget = new StatusWidget( "status" );

	//add widgets to the container
	this.context_controls_container.addContentNode( this.context_controls.getDOM() );
	this.context_navigation_container.addContentNode( this.context_navigation.getDOM() );
	this.context_controls_container.addContentNode( this.context_navigation_container.getWidgetDOM() );

	this.context_controls_container.addContentNode( this.status_widget.getWidgetDOM() );

	this.context_layout.addContentNode( this.content.getWidgetDOM() );
	this.context_layout.addContentNode( this.context_controls_container.getWidgetDOM() );

	this.context_container.addContentNode( this.context_layout.getWidgetDOM() );

	//add a breaker
	var breaker = document.createElement('div');
	breaker.style.height = "0px";
	breaker.style.clear = "both";

	this.context_container.addContentNode( breaker );

}

new HGContentContext();

/**
* HGContentContext.loadContent
*
* Make and execute a HGContentPCommand for the passed in content_type_id and content_id
*
* @param content_type_id
* @param content_id
**/
HGContentContext.prototype.loadContent = function( content_type_id, content_id ) { 

	this.setControlConfigToShown();

	var hcpc = new HGContentPCommand( content_type_id, content_id, this );
	hcpc.execute();

}

/**
* HGContentContext.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 content within the 
* context
*
* @param user_id
* @param access_level
**/
HGContentContext.prototype.resetUserInfo = function( user_id, access_level ) { 

	this.user_id = parseInt( user_id );
	this.access_level = parseInt( access_level );

	this.resetContentAccess();

	this.loadContent( this.content_type_id, this.content_id );

}

/**
* HGContentContext.resetContentAccess
**/
HGContentContext.prototype.resetContentAccess = function () { 

	if ( this.user_id <= 0 || this.access_level > this.content_access || this.access_level <= 0 ) {
		this.insert_content_button.hideWidget();
		this.create_assoc_button.hideWidget();
	}
	else {
		this.insert_content_button.showWidget();
		if ( this.access_level == 1 )
			this.create_assoc_button.showWidget();
		else
			this.create_assoc_button.hideWidget();
	}

}

/**
* HGContentContext.addNavigationButton
*
* Add a navigation button the the navigation widget of the content context
*
* @param b The button to add
**/
HGContentContext.prototype.addNavigationButton = function( b ) { 

	this.context_navigation.addWidget( b );

}

/**
* HGContentContext.setStatusToWorking
**/
HGContentContext.prototype.setStatusToWorking = function( msg ) { 

	this.status_widget.setStatus( "working", msg );

}

/**
* HGContentContext.clearStatus
**/
HGContentContext.prototype.clearStatus = function() { 

	this.status_widget.hideWidget();

}

/**
* HGContentContext.hideContext
*
* Hide everything in the context except for the controls
**/
HGContentContext.prototype.hideContext = function() { 

	this.content.hideWidget();
	this.context_navigation_container.hideWidget();

	this.setControlConfigToHidden();

	//set cookies
	createCookie( "content_context_shown", 0, 0 );

}

/**
* HGContentContext.showContext
*
* Hide everything in the context except for the controls
**/
HGContentContext.prototype.showContext = function() { 

	this.content.showWidget();
	this.context_navigation_container.showWidget();

	this.setControlConfigToShown();


	//set cookies
	createCookie( "content_context_shown", 1, 0 );

}

/**
* HGContentContext.setControlConfigToShown
**/
HGContentContext.prototype.setControlConfigToShown = function() { 
	this.show_context_button.hideWidget();
	this.hide_context_button.showWidget();
}

/**
* HGContentContext.setControlConfigToHidden
**/
HGContentContext.prototype.setControlConfigToHidden = function() { 
	this.show_context_button.showWidget();
	this.hide_context_button.hideWidget();
}


/**
* HGContentContext.updateAssociatedComicButton
*
* Creates the load comic command which the load associated comic button will call and shows the button
*
* @param assoc_comic_type_id
* @param assoc_comic_id
**/
HGContentContext.prototype.updateAssociatedComicButton = function( assoc_comic_type_id, assoc_comic_id ) {

	this.associated_comic_button.setCommand( new LoadAssociatedComicCommand( this.assoc_hg_page, assoc_comic_type_id, assoc_comic_id ) );
	this.showAssociatedComicButton();

}

/**
* HGContentContext.hideAssociatedComicButton
**/
HGContentContext.prototype.hideAssociatedComicButton = function() { 

	this.associated_comic_button.hideWidget();

}

/**
* HGContentContext.showAssociatedComicButton
**/
HGContentContext.prototype.showAssociatedComicButton = function() { 

	this.associated_comic_button.showWidget();

}	

/**
* HGContentPCommand
*
* A command to make a HGContentPCommand request to the server
*
* @param content_type_id
* @param content_id
* @param target the HGContentContextModule object from which we are making the request
**/
function HGContentPCommand( content_type_id, content_id, target ) { 

	this.content_type_id = content_type_id;
	this.content_id = content_id;
	this.target = target;

}

new HGContentPCommand();

HGContentPCommand.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, handleHGContentPCommand );
	XMLHTTPRequestCoordinator( "php/HGContentGetRequestC.php", af, "GET", nreq );

	//set status of the toggle field
	this.target.setStatusToWorking( "Loading..." );

}

function handleHGContentPCommand( x, t ) {

	t.clearStatus();

	//dump the current content
	t.content.clearContents();
	t.context_navigation.clearWidgets();

	var hgr = x.getElementsByTagName( "HGResponse" )[0];
	var hgs = hgr.getElementsByTagName( "HGSignature" )[0];

	//pull content_type_id and content_id and set the appropriate fields in the target
	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 content_access = hgs.getElementsByTagName( "content_access" )[0].childNodes[0].nodeValue;

	//create the load associated comic button if apporpriate
	if ( hgs.getElementsByTagName( "assoc_comic_type_id" )[0] ) {
		var assoc_comic_type_id = hgs.getElementsByTagName( "assoc_comic_type_id" )[0].childNodes[0].nodeValue;
		var assoc_comic_id = hgs.getElementsByTagName( "assoc_comic_id" )[0].childNodes[0].nodeValue;
		t.updateAssociatedComicButton( assoc_comic_type_id, assoc_comic_id );
	}
	else {
		t.hideAssociatedComicButton();
	}

	t.content_type_id = parseInt( content_type_id );
	t.content_id = parseInt( content_id );
	t.content_access = parseInt( content_access );

	t.resetContentAccess();

	//create the HGContent
	var hgcrc = new HGContentRenderCommand( 
		"hg_content", 
		SimpleWidgetFactory, 
		SimpleWidgetFactory, 
		RoundedWidgetFactory, 
		SimpleWidgetFactory, 
		RoundedWidgetFactory, 
		x, t.user_id, t.access_level 
	);

	var hgc = hgcrc.execute();

	//attach the content to the HGContentContext
	t.content.addContentNode( hgc.hg_content_block.getWidgetDOM() );

	//generate the context level controls

	//generate the navigation
	contentContextNavigationRenderHelper( x, t );

	//set context to shown
	t.showContext();

	//set cookies
	createCookie( "last_content_type_id", content_type_id, 0 );
	createCookie( "last_content_id", content_id, 0 );

}

/**
* contentContextNavigationRenderHelper
*
* Render the buttons necessary for the HGContentContext
*
* @param x
* @param t The HGContentContext to add the navigation buttons to
**/
function contentContextNavigationRenderHelper( x, t ) { 

	var hgr = x.getElementsByTagName( "HGResponse" )[0];
	var hgn = hgr.getElementsByTagName( "HGNavigation" )[0];
	var hgnpb = hgn.getElementsByTagName( "hgContent_previousButton" )[0];
	var hgnnb = hgn.getElementsByTagName( "hgContent_nextButton" )[0];

	if ( hgnpb ) {
		var hgnpb_but = contentContextNavigationButtonFactory( hgnpb, t, " previous",  'img/page/navigation/back_post.gif');
		t.addNavigationButton( hgnpb_but );
	}
	if ( hgnnb ) {
		var hgnnb_but = contentContextNavigationButtonFactory( hgnnb, t, " next", 'img/page/navigation/next_post.gif' );
		t.addNavigationButton( hgnnb_but );
	}

}

/**
* contentContextNavigationButtonFactory
*
* A factory for building buttons to use in the content 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 contentContextNavigationButtonFactory( xsig, target, button_content, button_img ) {

	var hgs = xsig.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 rbut = new SimpleButtonWidget( target.style_set, new HGContentPCommand( content_type_id, content_id, target ) );


	var ncimg = document.createElement( 'img' );
	ncimg.src = button_img;
	ncimg.alt = button_content;

	rbut.addContentNode( ncimg );

	rbut.addContentNode( textToDOM( "span", button_content ) );

	return rbut;

}

/**
* hideContentContextButtonFactory
*
* @param c The context we are hiding
**/
function hideContentContextButtonFactory( c ) { 

	var rbut = new SimpleButtonWidget( c.style_set, new HideContentContextCommand( c ) );

	var ncimg = document.createElement( 'img' );
	ncimg.src = 'img/page/navigation/hide_post.gif';
	ncimg.alt = "Hide Posts";

	rbut.addContentNode( ncimg );

	rbut.addContentNode( textToDOM( "span", " hide posts" ) );

	return rbut;

}

/**
* HideContentContextCommand
*
* @param t The context to hide
**/
function HideContentContextCommand( t ) { 

	this.target = t;

}

new HideContentContextCommand();

HideContentContextCommand.prototype.execute = function() { 

	this.target.hideContext();

}

/**
* showContentContextButtonFactory
*
* @param c The context we are showing
**/
function showContentContextButtonFactory( c ) { 

	var rbut = new SimpleButtonWidget( c.style_set, new ShowContentContextCommand( c ) );

	var ncimg = document.createElement( 'img' );
	ncimg.src = 'img/page/navigation/show_post.gif';
	ncimg.alt = "Hide Posts";

	rbut.addContentNode( ncimg );

	rbut.addContentNode( textToDOM( "span", " show posts" ) );

	return rbut;

}

/**
* ShowContentContextCommand
*
* @param t The context to show
**/
function ShowContentContextCommand( t ) { 

	this.target = t;

}

new ShowContentContextCommand();

ShowContentContextCommand.prototype.execute = function() { 

	this.target.showContext();

}

/**
* contentListButtonFactory
*
* @param t The context we are building the button from
*
* @returns A button which will load a content list into the list context of the associated hg page of the current context
**/
function contentListButtonFactory( t ) {

	var rbut = new SimpleButtonWidget( t.style_set, new HGContentListGenerateCommand( t.assoc_hg_page, t ) );

	var ncimg = document.createElement( 'img' );
	ncimg.src = 'img/page/navigation/get_post_list.gif';
	ncimg.alt = "Insert Content";

	rbut.addContentNode( ncimg );

	rbut.addContentNode( textToDOM( "span", " list post threads" ) );

	return rbut;

}

/**
* HGInsertNewContentPopupButtonFactory
*
* @param t The HGContentContext object we are inserting the content into
*/
function HGInsertNewContentPopupButtonFactory( t ) { 

	var rbut = new SimpleButtonWidget( t.style_set, new HGInsertNewContentPopupCommand( t ) );
	
	var ncimg = document.createElement( 'img' );
	ncimg.src = 'img/page/navigation/new_post.gif';
	ncimg.alt = "Insert Content";

	rbut.addContentNode( ncimg );
	rbut.addContentNode( textToDOM( "span", " insert content" ) );

	return rbut;

}

function HGInsertNewContentPopupCommand( t ) { 

	this.content_context = t;

}

new HGInsertNewContentPopupCommand();

HGInsertNewContentPopupCommand.prototype.execute = function() {

	//create the popup window
	var pw = new SimplePopupWindow( 
		"insert_content_popup_window" ,
		"Insert Content Popup Window" ,
		"hg_popup" , 
		600 , 
		700 , 
		15 ,
		true , 
		false ,
		false
	);

 

	//create the window title
	var win_title = textToDOM( "span", "Insert New Content" );

	//add the title to the popup window
	pw.header.addContentNode( win_title );

	//create the content title form
	var new_content_title_form = new HGForm();

	var content_title = new HGFormElement ( 
		"text" ,
		"" , 
		"new_title" ,
		"20" ,
		"1" ,
		"100" ,
		null ,
		null ,
		"New Content Title"
	);

	new_content_title_form.addHGFormElement( content_title );

	pw.body.addContentNode( new_content_title_form.form );

	//create the content WYSIWYG editor
	var wysiwyg = new WYSIWYGObject(500, 650 );

	//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 = submitHGInsertNewContentButtonFactory( 
		this.content_context.style_set, 
		pw, 
		this.content_context.content_type_id, 
		new_content_title_form, 
		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
	pw.popup_from = this.content_context;

}

/**
* submitHGInsertNewContentButtonFactory
**/
function submitHGInsertNewContentButtonFactory( style_set, pw, content_type_id, new_content_title_form, new_content_wysiwyg ) { 

	var rbut = new SimpleButtonWidget( 
		style_set ,
		new HGInsertNewContentCommand ( 
			pw, 
			new_content_title_form.getHGFormElement( "new_title" ) , 
			new_content_wysiwyg ,
			content_type_id
		) 
	);

	rbut.addContentNode( textToDOM( "span", "submit" ) );

	return rbut;

}

/**
* HGInsertNewContentCommand
*
* Generates the server request to add a new content to the HG_Page
*
* @param pw The popup window generating the request
* @param new_title_field
* @param new_content_wysiwyg
* @param content_type_id
**/
function HGInsertNewContentCommand( pw, new_title_field, new_content_wysiwyg, content_type_id ) { 

	this.popup_window = pw;
	this.new_title_field = new_title_field;
	this.new_content_wysiwyg = new_content_wysiwyg;
	this.content_type_id = content_type_id;

}

new HGInsertNewContentCommand();

HGInsertNewContentCommand.prototype.execute = function() { 

	//set up the HGRequest
	var nreq = new HGRequestObject( "HGInsertNewContentCommand" );
	nreq.addRequestElement( 'content_type_id', this.content_type_id );
	nreq.addRequestElement( 'new_title', this.new_title_field.getValue() );
	nreq.addRequestElement( 'new_content', this.new_content_wysiwyg.getHTML() );

	var af = AJAXRequestFunctionFactory( this.popup_window, handleHGInsertNewContentRequest );
	XMLHTTPRequestCoordinator( "php/HGContentPostRequestC.php", af, "POST", nreq );

	//set status of the toggle field
	this.popup_window.setStatusToWorking( "inserting content" );

}

function handleHGInsertNewContentRequest( x, t ) { 

	var success = HGPopupMessage( x );

	if ( success == 1 ) { 

		//refresh content
		var lcc = new HGContentPCommand( t.popup_from.content_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" );

	}

}

/**
* associatedComicButtonFactory
**/
function associatedComicButtonFactory( hg_page ) { 

	var rbut = new SimpleButtonWidget( 
		hg_page.style_set ,
		null 
	);

	rbut.addContentNode( textToDOM( "span", "load associated comic" ) );

	return rbut;

}

/**
* LoadAssociatedComicCommand
*
* A command to load a comic in the comic context.  This is an inner context command
*
* @param hg_page The HG_Page that the comic context is in
* @param comic_type_id
* @param comic_id
**/
function LoadAssociatedComicCommand( hg_page, comic_type_id, comic_id ) { 

	this.hg_page = hg_page;
	this.comic_type_id = comic_type_id;
	this.comic_id = comic_id;

}

new LoadAssociatedComicCommand();

LoadAssociatedComicCommand.prototype.execute = function() { 

	this.hg_page.comic_context.loadComic( this.comic_type_id, this.comic_id );

}

/**
* HGContentComicAssociationPopupButtonFactory
*
* Create a popup window with selections available for comic_type_id and comic_id to user as the associated comic
*
* @param hg_page_content_context
**/
function HGContentComicAssociationPopupButtonFactory( hg_page_content_context ) { 

	var rbut = new SimpleButtonWidget( hg_page_content_context.style_set, new HGContentComicAssociationPopupCommand( hg_page_content_context ) );

	rbut.addContentNode( textToDOM( "span", "create comic association" ) );

	return rbut;

}

function HGContentComicAssociationPopupCommand( t ) { 

	this.hg_page_content_context = t;

}

new HGContentComicAssociationPopupCommand();

HGContentComicAssociationPopupCommand.prototype.execute = function() {

	//create the popup window
	var pw = new SimplePopupWindow( 
	"content_comic_association_creation_window" ,
	"Create Content Comic Association" ,
	"hg_popup" , 
	200 , 
	300 , 
	15 ,
	true , 
	false ,
	false
	); 

	//create the window title
	var win_title = textToDOM( "span", "Create Content Comic Association" );

	//add the title to the popup window
	pw.header.addContentNode( win_title );

	//create the content title form
	var content_comic_association = new HGForm();

	var comic_type_id_form_element = new HGFormElement ( 
		"selection" ,
		null , 
		"comic_type_id" ,
		null ,
		null ,
		null ,
		PopulateComicIdFormElementCommand , //<-- state change command 
		new HGComicTypeListPCommand_F()
	);

	var comic_id_form_element = new HGFormElement ( 
		"selection" ,
		null ,	
		"comic_id" ,
		null , 
		null ,
		null ,
		null ,
		null ,
		null
	);

	content_comic_association.addHGFormElement( comic_type_id_form_element );
	content_comic_association.addHGFormElement( comic_id_form_element );

	pw.body.addContentNode( content_comic_association.form );

	//create the submit button
	var sbut = submitHGContentComicAssociationButtonFactory( pw, this.hg_page_content_context, content_comic_association, this.hg_page_content_context.content_type_id, this.hg_page_content_context.content_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_content_context;

}


/**
* HGComicTypeListPCommand_F
*
* Command to pull a HGComicTypeList and render it in the form necessary for an HGFormElement Selection box
*
**/
function HGComicTypeListPCommand_F() { }

new HGComicTypeListPCommand_F();

HGComicTypeListPCommand_F.prototype.execute = function( target ) { 

	//set up the HGRequest
	var nreq = new HGRequestObject( "HGComicTypeListPCommand" );

	//create the AJAX Request Function
	var af = AJAXRequestFunctionFactory( target, handleHGComicTypeListPCommand_F );
	
	//make the AJAX Request
	XMLHTTPRequestCoordinator( "php/HGContentGetRequestC.php", af, "GET", nreq );

}

function handleHGComicTypeListPCommand_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( "-Comic 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( 'comic_type_id' )[0].childNodes[0].nodeValue;

		t.form_element.options.add( new Option( opt_desc, opt_value ) );

	}

	return true;

}

/**
* PopulateComicIdFormElementCommand
*
* Creates a HGComicListPCommand_F command based on the comic_type_id value of the comic_type_id field of the associatedHGForm attached
* to the calling field
**/
function PopulateComicIdFormElementCommand( form_element ) {

	this.form_element = form_element;

}

new PopulateComicIdFormElementCommand();

PopulateComicIdFormElementCommand.prototype.execute = function() { 

	var form = this.form_element.associatedHGForm;

	var comic_type_id = this.form_element.getValue();

	var comic_id_field = form.getHGFormElement( "comic_id" );

	comic_id_field.repopulate( new HGComicListPCommand_F ( comic_type_id ) );

}

/**
* HGComicListPCommand_F
*
* Command to pull a HGComicList and render it in the form necessary for an HGFormElement Selection box
*
**/
function HGComicListPCommand_F( comic_type_id ) { 

	this.comic_type_id = comic_type_id;

}

new HGComicListPCommand_F();

HGComicListPCommand_F.prototype.execute = function( target ) { 

	//set up the HGRequest
	var nreq = new HGRequestObject( "HGComicListPCommand" );
	nreq.addRequestElement( 'comic_type_id', this.comic_type_id );

	//create the AJAX Request Function
	var af = AJAXRequestFunctionFactory( target, handleHGComicListPCommand_F );
	
	//make the AJAX Request
	XMLHTTPRequestCoordinator( "php/HGContentGetRequestC.php", af, "GET", nreq );

	alert( "here" );
	this.assoc_hg_page.scrollTo( this.assoc_hg_page.getListContextPosition().top );

}

function handleHGComicListPCommand_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( "-Comic-", 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( 'comic_id' )[0].childNodes[0].nodeValue;

		t.form_element.options.add( new Option( opt_desc, opt_value ) );

	}

	return true;

}

/**
* submitHGContentComicAssociationButtonFactory
**/
function submitHGContentComicAssociationButtonFactory( pw, hg_page_content_context, content_comic_association_form, content_type_id, content_id ) { 

	var rbut = new SimpleButtonWidget( hg_page_content_context.style_set, new HGInsertNewContentComicAssocCommand( pw, hg_page_content_context, content_comic_association_form, content_type_id, content_id ) );

	rbut.addContentNode( textToDOM( "span", "submit" ) );

	return rbut;

}

/**
* HGInsertNewContentComicAssocCommand
*
* Generates the server request to add a new content comic association
*
* @param pw The popup window generating the request
* @param hg_page_content_context
* @param content_comic_association_form
* @param content_type_Id
* @param content_id
**/
function HGInsertNewContentComicAssocCommand( pw, hg_page_content_context, content_comic_association_form, content_type_id, content_id ) { 

	this.popup_window = pw;
	this.hg_page_content_context = hg_page_content_context;
	this.content_comic_association_form = content_comic_association_form;
	this.content_type_id = content_type_id;
	this.content_id = content_id;

}

new HGInsertNewContentComicAssocCommand();

HGInsertNewContentComicAssocCommand.prototype.execute = function() { 

	//set up the HGRequest
	var nreq = new HGRequestObject( "HGInsertNewComicContentAssocCommand" );
	nreq.addRequestElement( 'content_type_id', this.content_type_id );
	nreq.addRequestElement( 'content_id', this.content_id );
	nreq.addRequestElement( 'comic_type_id', this.content_comic_association_form.getHGFormElement( "comic_type_id" ).getValue() );
	nreq.addRequestElement( 'comic_id', this.content_comic_association_form.getHGFormElement( "comic_id" ).getValue() ); 

	var af = AJAXRequestFunctionFactory( this.popup_window, handleHGInsertNewContentComicAssociation );
	XMLHTTPRequestCoordinator( "php/HGContentPostRequestC.php", af, "POST", nreq );

	//set status of the toggle field
	this.popup_window.setStatusToWorking( "creating association" );

}

function handleHGInsertNewContentComicAssociation( x, t ) { 

	var success = HGPopupMessage( x );

	if ( success == 1 ) { 

		//refresh content
		var lcc = new HGContentPCommand( t.popup_from.content_type_id, t.popup_from.content_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" );

	}

}

