/**
* SimpleMouseDownMouseUpButtonWidget
*
* A simple box shaped button widget with a class set applied to it. 
*
* Simple widget supports the following style_set elements
*   -style_set_smdmubw_out : The overall widget style
*   -style_set_smdmubw_out_container : The content container style
*   -style_set_smdmubw_over : The overall widget style when the mouse is over the widget
*   -style_set_smdmubw_over_container : The content container style when the mouse is over the widget
* 
* @param style_set
* @param mouse_down_command
* @param mouse_up_command
**/
function SimpleMouseDownMouseUpButtonWidget( style_set, mouse_down_command, mouse_up_command ) { 

	this.style_set = style_set;
	this.mouse_down_command = mouse_down_command;
	this.mouse_up_command = mouse_up_command;

	this.content_array = new Array();
	
	this.container_dom = document.createElement( 'div' );
	this.content_container = document.createElement( 'div' );

	//associate the SimpleMouseDownMouseUpButtonWidget with the container_dom
	this.container_dom.associated_button = this;

	this.container_dom.className = this.style_set + "_smdmubw_out";
	this.content_container.className = this.style_set + "_smdmubw_out_container";

	this.container_dom.appendChild( this.content_container );

	//add over and out event listeners 
	CBAddEventListener( 
		this.container_dom, 
		"mouseover", 
		function( e ) { 
			var my_target = getTarget( e );
			//we have to look through my target and the parent nodes of my target to find the true target
			while ( ! ( my_target.associated_button ) && my_target.parentNode ) 
				my_target = my_target.parentNode;
			my_target.style.cursor = "pointer"; 
			my_target.className = my_target.associated_button.style_set + "_smdmubw_over";
			my_target.associated_button.content_container.className = my_target.associated_button.style_set + "_smdmubw_over_container"; 
		} 
	);

	CBAddEventListener( 
		this.container_dom, 
		"mouseout", 
		function( e ) { 
			var my_target = getTarget( e );
			//we have to look through my target and the parent nodes of my target to find the true target
			while ( ! ( my_target.associated_button ) && my_target.parentNode ) 
				my_target = my_target.parentNode;
			my_target.style.cursor = "default"; 
			my_target.className = my_target.associated_button.style_set + "_smdmubw_out";
			my_target.associated_button.content_container.className = my_target.associated_button.style_set + "_smdmubw_out_container"; 
		} 
	);

	if ( mouse_down_command && mouse_down_command != null ) {
		CBAddEventListener( 
			this.container_dom, 
			"mousedown", 
			function( e ) { 
				var my_target = getTarget( e );
				//we have to look through my target and the parent nodes of my target to find the true target
				while ( ! ( my_target.associated_button ) && my_target.parentNode ) 
					my_target = my_target.parentNode;
				my_target.associated_button.mouse_down_command.execute( e );
			} 
		);
	}

	if ( mouse_up_command && mouse_up_command != null ) {
		CBAddEventListener( 
			this.container_dom, 
			"mouseup", 
			function( e ) { 
				var my_target = getTarget( e );
				//we have to look through my target and the parent nodes of my target to find the true target
				while ( ! ( my_target.associated_button ) && my_target.parentNode ) 
					my_target = my_target.parentNode;
				my_target.associated_button.mouse_up_command.execute( e );
			} 
		);
	}
	

}

SimpleMouseDownMouseUpButtonWidget.prototype = new AbstractWidget();

/**
* SimpleMouseDownMouseUpButtonWidget.setMouseDownCommand
*
* Sets the command which the button calls execute on
*
* @param c The command object
*
**/
SimpleMouseDownMouseUpButtonWidget.prototype.setMouseDownCommand = function( c ) { 

	this.mouse_down_command = c;

	CBRemoveEventListener( this.container_dom, "mousedown" );

	CBAddEventListener( 
		this.container_dom, 
		"mousedown", 
		function( e ) { 
			var my_target = getTarget( e );
			//we have to look through my target and the parent nodes of my target to find the true target
			while ( ! ( my_target.associated_button ) && my_target.parentNode ) 
				my_target = my_target.parentNode;
			my_target.associated_button.mouse_down_command.execute( e );
		} 
	);

}

/**
* SimpleMouseDownMouseUpButtonWidget.setMouseUpCommand
*
* Sets the command which the button calls execute on
*
* @param c The command object
*
**/
SimpleMouseDownMouseUpButtonWidget.prototype.setMouseUpCommand = function( c ) { 

	this.mouse_up_command = c;

	CBRemoveEventListener( this.container_dom, "mouseup" );

	CBAddEventListener( 
		this.container_dom, 
		"mouseup", 
		function( e ) { 
			var my_target = getTarget( e );
			//we have to look through my target and the parent nodes of my target to find the true target
			while ( ! ( my_target.associated_button ) && my_target.parentNode ) 
				my_target = my_target.parentNode;
			my_target.associated_button.mouse_up_command.execute( e );
		} 
	);

}

/**
* SimpleMouseDownMouseUpButtonWidget.hideWidget
*
* Change the visibility of a rendered widget and also decrease the height and width to 0 so that it does not impact surrounding widgets
* Also remove listeners so that class name does not revert upon resizing of the widget
*
**/
SimpleMouseDownMouseUpButtonWidget.prototype.hideWidget = function() { 

	//remove mouse out and mouse over listening
	CBRemoveEventListener( this.container_dom, "mouseout" );
	CBRemoveEventListener( this.container_dom, "mouseover" );

	this.container_dom.className = "hidden_button";

}

/**
* SimpleMouseDownMouseUpButtonWidget.showWidget
*
* Change the visibility of a rendered widget and also return the width and height to their original values
**/
SimpleMouseDownMouseUpButtonWidget.prototype.showWidget = function() { 

	//reinit mouse out and mouse over listeners
	CBReAddEventListener( this.container_dom, "mouseout" );
	CBReAddEventListener( this.container_dom, "mouseover" );

	this.container_dom.className = this.style_set + "_sbw_out";

}

/**
* SimpleMouseDownMouseUpButtonWidgetFactory
*
* A factory for simple button widgets
*
* @param style_set
**/
function SimpleMouseDownMouseUpButtonWidgetFactory( style_set, mouse_down_command, mouse_up_command ) { 

	this.style_set = style_set;
	this.mouse_down_command = mouse_down_command;
	this.mouse_up_command = mouse_up_command;

}

new SimpleMouseDownMouseUpButtonWidgetFactory();

SimpleMouseDownMouseUpButtonWidgetFactory.prototype.build = function() { 

	return new SimpleMouseDownMouseUpButtonWidget( this.style_set, this.mouse_down_command, this.mouse_up_command );

}

