
var ProductSelector = Class.create(
{
	categoryURI: "/product/list/",
	
	productURI: "/product/detail/",
	
	initialize: function( element )
	{
		this.element = $( element );
		this.initEvents();
	},
	
	initEvents: function()
	{
		this.element.observe( "change", this.handleChange.bind( this ) );
	},
	
	handleChange: function( e )
	{
		Event.stop( e );
		var selected = $( this.element.options[ this.element.selectedIndex ] );
		
		var group = selected.up( "optgroup" );
		
		if( !group )
			return false;
		
		var type = group.getAttribute( "id" );
		
		if( type !== "by-category" && type !== "by-name" )
			return false;
		
		window.location = ( type === "by-name" ? this.productURI : this.categoryURI ) + $F( e.element() );
		return true;
	}			
});

document.observe( "dom:loaded", function(){ new ProductSelector( "product-selector" ) } );