/**
* HGListContext
*
* Organization of a list of links
*
* @param style_set
**/
function HGListContext( style_set ) { 

	this.style_set = style_set;

	//generate the context container
	this.context_container = new SimpleWidget( this.style_set + "_list_container" );

	//generate the list context
	this.list_context = new SimpleWidget( this.style_set + "_list_context" );

	//generate the widget engine which will contain the buttons
	this.button_engine = new TabularWidgetLayoutFactory( this.style_set + "_list_buttons", 3 ).build();
	
	//generate the command widget engine
	this.command_engine = new TabularWidgetLayoutFactory( this.style_set + "_list_command", 5 ).build();

	//generate the close list button and add it to the command engine
	this.close_list_button = closeListButtonFactory( this );
	this.command_engine.addWidget( this.close_list_button );

	//add the status widget
	this.status_widget = new StatusWidget( "status" );
	this.command_engine.addWidget( this.status_widget );	

	//attach everything to the context container
	this.list_context.addContentNode( this.button_engine.getDOM() );
	this.list_context.addContentNode( this.command_engine.getDOM() );

	this.context_container.addContentNode( this.list_context.getWidgetDOM() );

}

new HGListContext();

/**
* HGListContext.hideContext
*
* Hide the context container
**/
HGListContext.prototype.hideContext = function() { 

	this.context_container.hideWidget();

}

/**
* HGListContext.showContext
*
* Show the context container
**/
HGListContext.prototype.showContext = function() { 

	this.context_container.showWidget();

}

/**
* HGListContext.clearList
*
* Clear out the buttons
**/
HGListContext.prototype.clearList = function() { 

	this.button_engine.clearWidgets();

}

/**
* HGListContext.populateList
*
* Populates the HGList with link buttons based on the link list xml passed to the function.
*
* The buttons are build by a link button factory which is also passed in.  For each link present in the xml, the link xml is sent to the 
* factory which returns an appropriate button.  
*
* @param x The passed in xml
* @param button_factory The button factory to be used in creating the buttons
* @param p The hg_page we are generating the buttons for
**/
HGListContext.prototype.populateList = function( x, button_factory, p ) { 

	var hgr = x.getElementsByTagName( "HGResponse" )[0];
	var llist = hgr.getElementsByTagName( "HGLinkList" )[0];
	var links = llist.getElementsByTagName( "HGLink" );

	for( var i=0; i<links.length; i++ ) { 

		this.button_engine.addWidget( button_factory( links[i], this.style_set + "_list_context", p ) );
		
	}

}

/**
* HGListContext.setStatusToWorking
**/
HGListContext.prototype.setStatusToWorking = function( msg ) { 

	this.status_widget.setStatus( "working", msg );

}

/**
* HGListContext.clearStatus
**/
HGListContext.prototype.clearStatus = function() { 

	this.status_widget.hideWidget();

}

/**
* closeListButtonFactory
*
* Create a button to close the list context by hiding it
*
* @param t The list context to close
**/
function closeListButtonFactory( t ) { 

	var rbut = new SimpleButtonWidget( t.style_set + "_list_context" , new CloseListCommand( t ) );

	rbut.addContentNode( textToDOM( "span", "Close List" ) );

	return rbut;


}

function CloseListCommand( t ) { 

	this.target = t;

}

new CloseListCommand();

CloseListCommand.prototype.execute = function() { 

	this.target.hideContext();

}


