function OmegaOptionsSelector( valuesListId, selectedOptionsListId ) {
	this.valuesList = $('#' + valuesListId );
	this.selectedOptionsList = $( '#' + selectedOptionsListId );
	this.currentOption = null;
	$( 'body' ).bind( 'productChanged', $.proxy( this.productChanged, this ) );
	$( '#' + valuesListId + ' li' ).live( 'click', $.proxy( this.valueSelected, this ) );
	$( '#backLink' ).live( 'click', $.proxy( this.goBack, this ) );
	$( '#startOverLink' ).live( 'click', $.proxy( this.startOver, this ) );
}

OmegaOptionsSelector.prototype.productChanged = function( evt, product ) {
	// bolt on an OptionsCursor for easy traversal of the product options.
	this.product = $.extend( {}, product, { optionsStore:new OptionsCursor( product ) } );
	$('#productName').html( product.name );
	this.clearSelectedOptions();
	if( product.options.length > 0 ) {
		this.setCurrentOption( this.product.optionsStore.current() );
	}
}

OmegaOptionsSelector.prototype.clearSelectedOptions = function() {
	// the first child is the heading.
	this.selectedOptionsList.children( ':not(:first-child)' ).remove();
}

OmegaOptionsSelector.prototype.selectedSeatWidthId = function() {
	if( this.selectedOptions().length == 0 ) {
		return null;
	}
	return this.selectedOptions()[0].id;
}

OmegaOptionsSelector.prototype.setCurrentOption = function( productOption ) {
	this.valuesList.empty();
	this.currentOption = productOption;
//	console.log( productOption.name );
	$('#optionName').html( productOption.name );
	$('#selectorOptionValueTpl').tmpl( productOption ).appendTo( this.valuesList );
}

// given a dom element representing an option value, look up the corresponding object
// in the product options.
OmegaOptionsSelector.prototype.findValue = function( elem ) {
	// database id in the form of db_xxx
	var dbid = elem.id.split( '_' )[1];
	for( var i = 0; i < this.currentOption.values.length; i++ ) {
		if( this.currentOption.values[i].id == dbid ) {
			return this.currentOption.values[i];
		}
	}
	return null;
}

OmegaOptionsSelector.prototype.valueSelected = function( evt ) {

	this.addSelectedOption( evt.target );
	
	if( this.product.optionsStore.hasNext() ) {
		this.setCurrentOption( this.product.optionsStore.next() );
	}
	else {
		// we add a fake option here (as a bandaid) to prevent selection of multiple values
		// from the last available option, and to keep the back button in sync.
		this.setCurrentOption( { isFake:true } );
		$('#optionsSelector').trigger('lastOptionSelected');
	}
}

OmegaOptionsSelector.prototype.addSelectedOption = function( elem ) {
	// look up the details of the selected option.
	var option = this.findValue( elem );
	
	// pin those details to the dom element so we can grab them again later.
	$(elem).data( 'productOption', option );
	
	// add the dom element to the selected options list.
	this.selectedOptionsList.append( elem );
}

OmegaOptionsSelector.prototype.goBack = function( evt ) {
	if( this.product.optionsStore.hasPrevious() ) {
		this.selectedOptionsList.children( ":last-child" ).remove();
		if( ! this.currentOption.isFake ) {
			this.setCurrentOption( this.product.optionsStore.previous() );
		}
		else {
			this.setCurrentOption( this.product.optionsStore.last() );
		}
	}
}

OmegaOptionsSelector.prototype.selectedOptions = function() {
	var results = [];
	this.selectedOptionsList.children( ':not(:first-child)' ).each( function( index, elem ) {
		results.push( $(elem).data('productOption') );
	} );
	return results;
}

OmegaOptionsSelector.prototype.startOver = function() {
	this.product.optionsStore.reset();
	this.productChanged( null, this.product );
}

