﻿SmieldChoice = Class.create();
Object.extend( SmieldChoice.prototype, {
	initialize: function( members, seperator )
	{
		this.seperator = seperator || " ";
		this.hasFields = false;
		var fields = [];
		if( members )
		{
			this.hasFields = true;
			if( typeof members == "string" )
				fields.push( $( members ) );
			else if( members.length )
			{
				for( var i = 0, field = null; null != ( field = $( members[ i ] ) ); i++ )
					fields.push( field );
			}
		}
		this.fields = $A( fields );
	},
	getValue: function()
	{
		var self = this;
		var buffer = "";
		this.fields.each( function( field )
		{
			if( !field ) return;
			var value = field.value.strip();
			if( value.length > 0 )
			{
				if( buffer.length > 0 ) buffer += self.seperator;
				buffer += value;
			}
		} );
		return buffer;
	}
} );

Smield = Class.create();
Object.extend( Smield.prototype, {
	initialize: function( element, group, choices, options )
	{
		var self = this;
		this.input = $( element );
		this.options = Object.extend( { 
			useId: false, 
			disabledClass: "disabled", 
			filter: function( value ){ return value; } 
			}, options || {} );
		
		this.event_swap = this.swap.bindAsEventListener( this );
		this.event_update = this.update.bind( this );
		
		// triggers (or, erm, radio buttons that trigger a change in data source)
		this.triggers = $A( document.getElementsByName( group ) );
		this.triggers.each( function( trigger ) { Event.observe( trigger, "click", self.event_swap, false ); } );
		
		// choices (or sources of data i.e. the input fields that provide the actual values, name, company etc...)
		this.choices = choices;
		for( var key in this.choices )
		{
			var item = this.choices[ key ];
			if( !this.current ) this.current = item;
			item.fields.each( function( field )
			{
				Event.observe( field, "blur", self.event_update, false );
				Event.observe( field, "keyup", self.event_update, false );
			} );
		}
		
		this.selectInitialChoice();
		if( this.input.form )
			Event.observe( this.input.form, "submit", function(){ self.input.disabled = false; }, false );
	},
	swap: function( e )
	{
		var el = Event.element( e );
		this.current = this.choices[ el.value ];
		this.update();
	},
	update: function()
	{
		var item = this.current, el = this.input;
		this.setValue( this.options.filter( item.getValue() ) );
		el.disabled = item.hasFields;
		if( item.hasFields ) el.addClassName( this.options.disabledClass );
		else el.removeClassName( this.options.disabledClass );
	},
	setValue: function( value )
	{
		if( value.strip().length > 0 )
			this.input.value = value;
	},
	getValue: function()
	{
		return this.input.value.strip();
	},
	selectInitialChoice: function()
	{
		var value = this.getValue();
		if( value.length > 0 )
		{
			var self = this;
			for( var key in this.choices )
			{
				var item = this.choices[ key ];
				if( item.hasFields && value == this.options.filter( item.getValue() ) )
				{
					this.current = item;
					this.triggers.each( function( trigger )
					{
						// maps the key of the choices array to the value of 
						// a partipating radio button value
						if( key == trigger.value )
						{
							trigger.checked = true;
							throw $break;
						}
					} );
					break;
				}
			}
			
			// if there are 'other' fields participating in the current item, 
			// then make sure it's out-of-reach for direct editing.
			if( this.current.hasFields )
			{
				this.input.disabled = true;
				this.input.addClassName( this.options.disabledClass );
			}
		}
	}
} );

