/**s
 * Skinned radio buttons. 
 * Custom objects: ControlRadioGroup, ControlRadioConfig, ControlRadio
 * @author Gytis Galvanauskas <gytis@gaumina.lt>
 * @version 0.1
 */


/**
 * ControlRadioGroup custom object
 * @access public
 */


ControlRadioGroup = function( name, form_element_name ) {
	
	this.name = name;
	this.value = '';
	this.form_element_name = form_element_name;
	this.oControlRadioActive = null;
	
}

ControlRadioGroup.prototype.getHTMLID = function( name ) {

	return( '__HTMLID__ControlRadioGroup_' + this.name + ( name ? ( '_' + name ) : '' ) );

}

ControlRadioGroup.prototype.getHTML = function() {
	
	var ret_val = '';
	
	ret_val = '<input type="hidden" value="' + this.value + '" name="' + this.form_element_name + '" id="' + this.getHTMLID( 'value' ) + '" />';
	
	return( ret_val );

}

ControlRadioGroup.prototype.redraw = function() {

	var value_ref = getElement( this.getHTMLID( 'value' ) );
	
	if( value_ref ) {
		
		value_ref.value = this.value;
	}

}

ControlRadioGroup.prototype.changeActiveRadio = function( oControlRadio ) {

	if( ( ! this.oControlRadioActive ) || ( this.oControlRadioActive.name != oControlRadio.name ) ) {
		
		if( this.oControlRadioActive ) {
			this.oControlRadioActive._setChecked( false );
		}
		
		oControlRadio._setChecked( true );
		
		this.oControlRadioActive = oControlRadio;
		
		this.value = oControlRadio.value;
		
		this.redraw();
		
	}

}

ControlRadioGroup.prototype.toString = function() {

	return( this.getHTML() );

}


/**
 * ControlRadioConfig custom object
 * @access public
 */


ControlRadioConfig = function() {
	
	this.images = { 'on' : '', 'off' : '', 'over' : '' };
	
	this.image_width 	= '0px';
	this.image_height 	= '0px';
	
	this.use_over = false;

}


/**
 * ControlRadio custom object
 * @access public
 */

ControlRadio = function( name, value, oControlRadioConfig, oControlRadioGroup ) {
	
	this.name = name;
	this.checked = false;
	this.over = false;
	this.value = value;
	
	this.oControlRadioGroup 	= oControlRadioGroup;
	this.oConfig 				= oControlRadioConfig;
	
	for( var idx in this.oConfig.images ) {
		getImage( this.oConfig.images[idx] );
	}

}

ControlRadio.prototype.getHTMLID = function( name ) {
	
	return( '__HTMLID__ControlRadio_' + this.name + ( name ? ( '_' + name ) : '' ) );

}


ControlRadio.prototype.onMouseOver = function( evt ) {
	
	if( this.oConfig.use_over ) {
		this.over = true;
		this.redraw();
	}

}

ControlRadio.prototype.onClick = function( evt ) {

	this.oControlRadioGroup.changeActiveRadio( this );
	
}


ControlRadio.prototype._setChecked = function( value ) {
	
	this.checked = value ? true : false;
	this.redraw();

}

ControlRadio.prototype.setChecked = function() {
	
	this.oControlRadioGroup.changeActiveRadio( this );

}

ControlRadio.prototype.onMouseOut = function( evt ) {

	if( this.oConfig.use_over ) {
		this.over = false;
		this.redraw();
	}

}

ControlRadio.prototype.redraw = function() {

	var container_ref = getElement( this.getHTMLID( 'container' ) );
	
	if( container_ref && container_ref.innerHTML ) {
		container_ref.innerHTML = this.getImageHTML();
	}

}


ControlRadio.prototype.getImageHTML = function() {
	
	var ret_val = '';
	
	var image_src = this.oConfig.images[( this.oConfig.use_over && this.over ) ? 'over' : ( this.checked ? 'on' : 'off' )];
	var width = this.oConfig.image_width;
	var height = this.oConfig.image_height;
	
	ret_val =	'<span style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader( src=\'' + image_src + '\' ); width: ' + width + '; height: ' + height + ';">' +
					'<img style="filter:progid:DXImageTransform.Microsoft.Alpha( opacity=0 ); width: ' + width + '; height: ' + height + ';" src="' + image_src + '" alt="" />' +
				'</span>';
				
	return( ret_val );
	
}

ControlRadio.prototype.getHTML = function() {
	
	var ret_val = '';
	
	var width = this.oConfig.image_width;
	var height = this.oConfig.image_height;

	
	ret_val = 	'<div style="width: ' + width + '; height: ' + height + ';" onmousedown="javascript:' + this.name + '.onClick( event );" onmouseover="javascript:' + this.name + '.onMouseOver( event );" onmouseout="javascript:' + this.name + '.onMouseOut( event );" id="' + this.getHTMLID( 'container' ) + '">' + 
					this.getImageHTML() + 
				'</div>';

	return( ret_val );
	
}

ControlRadio.prototype.toString = function() {
	
	return( this.getHTML() );
	
}