// -------------------------------------------------------------------------- //
// ------------------------------ Button ------------------------------------ //
// -------------------------------------------------------------------------- //

Button = function( target, form, button ) {
	Button.superClass.apply( this, arguments );
	
	this.Id = 'Button';
	this.disabled = true;
	this.status = false;

	this.element = target;
	this.eForm = form || target.firstChild;
	this.eButton = button || this.eForm.lastChild;

	this.addHandler( this.eButton, 'click', function( params, ev ) { this.stopPropagation( ev ); } );
	this.addHandler( this.eForm, 'submit', this.onSubmit );
	this.addHandler( this.element, 'click', function( params, ev ) { this.eButton.click(); } );
	
	this.addHandler( this.element, 'mouseover', this.hoverIn );
	this.addHandler( this.element, 'mouseout', this.hoverOut );

	this.setValid( false );
}
Button.inheritsFrom( HTMLGlif );

// -------------------------------------------------------------------------- //

Button.prototype.hoverIn = function(){
	if ( !this.disabled ){
		this.setClassName( 'hoverButton', true );
	}
}

Button.prototype.hoverOut = function(){
	this.setClassName( 'hoverButton', false );
}

Button.prototype.fillForm = function( sUrl, oParams ) {
	sUrl = sUrl || false;
	oParams = oParams || false;
	if( typeof( oParams ) != 'object' ) return;
	if( sUrl ) this.setUrl( sUrl );
	for( var c in oParams ) {
		var cP = oParams[c];
		var cN = this.createNode( 'input', { type: 'hidden', name: c, value: cP } );
		this.eForm.appendChild( cN );
	}
	this.setValid( true );
}

Button.prototype.setUrl = function( url ) {
	this.setProperty( { action: url }, this.eForm );
}

Button.prototype.onSubmit = function( params, ev ) {
	this.preventDefault( ev );
	this.notify( 'buttonClicked' );
	if( this.disabled === false ) this.notify( 'buttonSubmited' );
}

Button.prototype.submit = function() {
	this.eForm.submit();
}

Button.prototype.setValid = function( flag ) {
	var oS = this.status;
	this.status = flag;
	this.setClassName( 'disabled', !flag, this.eForm );
	this.disabled = !flag;
	//this.eButton.disabled = !flag;
	//if( !( oS === this.status ) ) this.notify( 'buttonStatusChanged', this.status );
}