/**
* contentTypeListButtonFactory
*
* Create a button which will call up and generate a HGContentType list
*
* @param t The hg_page to generate the list within
**/
function contentTypeListButtonFactory( t ) { 

	var rbut = new SimpleButtonWidget( t.style_set + "_posts_nav_button", new HGContentTypeListGenerateCommand( t ) );

	return rbut;

}

function HGContentTypeListGenerateCommand( t ) { 

	this.target = t;

}

new HGContentTypeListGenerateCommand();

HGContentTypeListGenerateCommand.prototype.execute = function() { 

	//clear out the current context
	this.target.list_context.clearList();
	
	//show the list
	this.target.list_context.showContext();

	//set the status to working
	this.target.list_context.setStatusToWorking( "loading content type list" );

	//set up the HGRequest
	var nreq = new HGRequestObject( "HGContentTypeListPCommand" );

	var af = AJAXRequestFunctionFactory( this.target, handleHGContentTypeListPRequest );
	XMLHTTPRequestCoordinator( "php/HGContentGetRequestC.php", af, "GET", nreq );

}

function handleHGContentTypeListPRequest( x, t ) { 

	//make doubly sure the list is clear
	t.list_context.clearList();

	t.list_context.populateList( x, contentTypeListContextButtonFactory, t );

	t.list_context.clearStatus();

}

/**
* contentTypeListContextButtonFactory
*
* A factory for creating list buttons to load lists of content for a given type
*
* @param x The xml link
* @param context_style_set The style set to use when rendering the button
* @param p The hg_page we are generating the buttons for
**/
function contentTypeListContextButtonFactory( x, style_set, p ) { 

	//Grab the signature
	var hgs = x.getElementsByTagName( "HGSignature" )[0];

	//pull the content_type_id from the link
	var content_type_id = hgs.getElementsByTagName( "content_type_id" )[0].childNodes[0].nodeValue;

	
	//grab the button name
	var button_name = x.getElementsByTagName( "HGLinkText" )[0].childNodes[0].nodeValue;

	var rbut = new SimpleButtonWidget( style_set, new CloseListHGContentPCommand( content_type_id, 0, p ) );

	rbut.addContentNode( textToDOM( "span", button_name ) );

	return rbut;

}

/**
* CloseListHGContentPCommand
*
* Closes the list context and executes an HGContentPCommand
**/
function CloseListHGContentPCommand( content_type_id, content_id, hg_page ) { 

	this.content_type_id = content_type_id;
	this.content_id = content_id;
	this.hg_page = hg_page;

}

CloseListHGContentPCommand.prototype.execute = function() { 

	var command = new HGContentPCommand( this.content_type_id, this.content_id, this.hg_page.content_context );
	command.execute();
	this.hg_page.list_context.hideContext();
	this.hg_page.comic_context.hideContext();

	this.hg_page.scrollTo( parseInt( this.hg_page.getContentContextPosition().top ) - 50 );

	
}

/**
* HGContentListGenerateCommand
*
* Generate a content list withing the list context
*
* @param p The hg_page to generate the list within
* @param content_context The content_context we are generating the list from
**/
function HGContentListGenerateCommand( p, content_context ) { 

	this.target = p;
	this.content_context = content_context;

}

new HGContentListGenerateCommand();

HGContentListGenerateCommand.prototype.execute = function() { 

	var content_type_id = this.content_context.content_type_id;

	//clear out the current context
	//this.target.list_context.clearList();
	
	//show the list
	this.target.list_context.showContext();

	//set the status to working
	this.target.list_context.setStatusToWorking( "loading content list" );

	//set up the HGRequest
	var nreq = new HGRequestObject( "HGContentListPCommand" );
	nreq.addRequestElement( 'content_type_id', content_type_id );

	var af = AJAXRequestFunctionFactory( this.target, handleHGContentListPRequest );
	XMLHTTPRequestCoordinator( "php/HGContentGetRequestC.php", af, "GET", nreq );

	this.target.scrollTo( parseInt( this.target.getListContextPosition().top ) - 50 );

}

function handleHGContentListPRequest( x, t ) { 

	//make doubly sure the list is clear
	t.list_context.clearList();

	t.list_context.populateList( x, contentListContextButtonFactory, t );

	t.list_context.clearStatus();

}

/**
* contentListButtonFactory
*
* A factory for creating list buttons to load lists of content for a given type
*
* @param x The xml link
* @param context_style_set The style set to use when rendering the button
* @param p The hg_page we are generating the buttons for
**/
function contentListContextButtonFactory( x, style_set, p ) { 

	//Grab the signature
	var hgs = x.getElementsByTagName( "HGSignature" )[0];

	//pull the content_type_id from the link
	var content_type_id = hgs.getElementsByTagName( "content_type_id" )[0].childNodes[0].nodeValue;
	var content_id = hgs.getElementsByTagName( "content_id" )[0].childNodes[0].nodeValue;

	//grab the button name
	var button_name = x.getElementsByTagName( "HGLinkText" )[0].childNodes[0].nodeValue;

	var rbut = new SimpleButtonWidget( style_set, new CloseListHGContentPCommand( content_type_id, content_id, p ) );

	rbut.addContentNode( textToDOM( "span", button_name ) );

	return rbut;

}

/**
* comicTypeListButtonFactory
*
* Create a button which will call up and generate a HGComicType list
*
* @param t The hg_page to generate the list within
**/
function comicTypeListButtonFactory( t ) { 

	var rbut = new SimpleButtonWidget( t.style_set + "_comics_nav_button", new HGComicTypeListGenerateCommand( t ) );

	return rbut;

}

function HGComicTypeListGenerateCommand( t ) { 

	this.target = t;

}

new HGComicTypeListGenerateCommand();

HGComicTypeListGenerateCommand.prototype.execute = function() { 

	//clear out the current context
	this.target.list_context.clearList();
	
	//show the list
	this.target.list_context.showContext();

	//set the status to working
	this.target.list_context.setStatusToWorking( "loading comic type list" );

	//set up the HGRequest
	var nreq = new HGRequestObject( "HGComicTypeListPCommand" );

	var af = AJAXRequestFunctionFactory( this.target, handleHGComicTypeListPRequest );
	XMLHTTPRequestCoordinator( "php/HGContentGetRequestC.php", af, "GET", nreq );

}

function handleHGComicTypeListPRequest( x, t ) { 

	//make doubly sure the list is clear
	t.list_context.clearList();

	t.list_context.populateList( x, comicTypeListContextButtonFactory, t );

	t.list_context.clearStatus();

}

/**
* comicTypeListContextButtonFactory
*
* A factory for creating list buttons to load lists of comic for a given type
*
* @param x The xml link
* @param context_style_set The style set to use when rendering the button
* @param p The hg_page we are generating the buttons for
**/
function comicTypeListContextButtonFactory( x, style_set, p ) { 

	//Grab the signature
	var hgs = x.getElementsByTagName( "HGSignature" )[0];

	//pull the comic_type_id from the link
	var comic_type_id = hgs.getElementsByTagName( "comic_type_id" )[0].childNodes[0].nodeValue;

	
	//grab the button name
	var button_name = x.getElementsByTagName( "HGLinkText" )[0].childNodes[0].nodeValue;

	var rbut = new SimpleButtonWidget( style_set, new CloseListHGComicPCommand( comic_type_id, 0, p ) );

	var button_name_span = textToDOM( "span", button_name );
	button_name_span.style.fontWeight = "bold";

	rbut.addContentNode( button_name_span );

	//add a description
	if ( x.getElementsByTagName( "HGLinkDescription" )[0] ) { 

		var link_description = x.getElementsByTagName( "HGLinkDescription" )[0].childNodes[0].nodeValue;
	
		var description_area = document.createElement( 'div' );
		
		if ( x.getElementsByTagName( "HGLinkThumbnail" )[0] ) { 
			var link_thumbnail = document.createElement( 'img' );
			link_thumbnail.src = "img/comic/thumbnails/" + x.getElementsByTagName( "HGLinkThumbnail" )[0].childNodes[0].nodeValue;

			//link_thumbnail.style.position = "static";
			link_thumbnail.style.cssFloat = "right";
			link_thumbnail.style.styleFloat = "right";
		
			description_area.appendChild( link_thumbnail );

		}

		var description_text = textToDOM( "span", link_description );
		//description_text.style.cssFloat = "left";
		//description_text.style.position = "static";

		description_area.appendChild( description_text );

		rbut.addContentNode( description_area );
	}

	return rbut;

}

/**
* CloseListHGComicPCommand
*
* Closes the list context and executes an HGComicPCommand
**/
function CloseListHGComicPCommand( comic_type_id, comic_id, hg_page ) { 

	this.comic_type_id = comic_type_id;
	this.comic_id = comic_id;
	this.hg_page = hg_page;

}

CloseListHGComicPCommand.prototype.execute = function() { 

	var command = new HGComicPCommand( this.comic_type_id, this.comic_id, this.hg_page.comic_context );
	command.execute();
	this.hg_page.list_context.hideContext();
	this.hg_page.content_context.hideContext();

	this.hg_page.scrollTo( parseInt( this.hg_page.getComicContextPosition().top ) - 50 );

	
}

/**
* HGComicListGenerateCommand
*
* Generate a comic list withing the list context
*
* @param p The hg_page to generate the list within
* @param comic_context
**/
function HGComicListGenerateCommand( p, comic_context ) { 

	this.target = p;
	this.comic_context = comic_context;

}

new HGComicListGenerateCommand();

HGComicListGenerateCommand.prototype.execute = function() { 

	var comic_type_id = this.comic_context.comic_type_id;
	
	//show the list
	this.target.list_context.showContext();

	//set the status to working
	this.target.list_context.setStatusToWorking( "loading comic list" );

	//set up the HGRequest
	var nreq = new HGRequestObject( "HGComicListPCommand" );
	nreq.addRequestElement( 'comic_type_id', comic_type_id );

	var af = AJAXRequestFunctionFactory( this.target, handleHGComicListPRequest );
	XMLHTTPRequestCoordinator( "php/HGContentGetRequestC.php", af, "GET", nreq );

	this.target.scrollTo( parseInt( this.target.getListContextPosition().top ) - 50 );

}

function handleHGComicListPRequest( x, t ) { 

	//make doubly sure the list is clear
	t.list_context.clearList();

	t.list_context.populateList( x, comicListContextButtonFactory, t );

	t.list_context.clearStatus();

}

/**
* comicListButtonFactory
*
* A factory for creating list buttons to load lists of comic for a given type
*
* @param x The xml link
* @param context_style_set The style set to use when rendering the button
* @param p The hg_page we are generating the buttons for
**/
function comicListContextButtonFactory( x, style_set, p ) { 

	//Grab the signature
	var hgs = x.getElementsByTagName( "HGSignature" )[0];

	//pull the comic_type_id from the link
	var comic_type_id = hgs.getElementsByTagName( "comic_type_id" )[0].childNodes[0].nodeValue;
	var comic_id = hgs.getElementsByTagName( "comic_id" )[0].childNodes[0].nodeValue;

	//grab the button name
	var button_name = x.getElementsByTagName( "HGLinkText" )[0].childNodes[0].nodeValue;

	var rbut = new SimpleButtonWidget( style_set, new CloseListHGComicPCommand( comic_type_id, comic_id, p ) );

	rbut.addContentNode( textToDOM( "span", button_name ) );

	return rbut;

}


