/**
 * $Id: dynamicOptions.js 4875 2008-08-06 14:47:24Z jons $
 * 
 * DynamicOptions creates a filter on a select box based on the class names of the options.
 * 
 * Required 2 elements:
 * 
 * 1. selector -> the filter element
 * 2. selection -> the filtered element.
 */


var DynamicOptions = Class.create();

DynamicOptions.prototype = {
	
	initialize: function(selector, selection) {
		
		this.selector = $(selector);
		this.selection = $(selection);
		this.all = $A(this.selection.options);
		
		this.selector.observe("change" , this.change.bind(this));
		this.change();
		
	},
	
	change: function() {
		this.clear();
		
		if(this.selector.options[this.selector.selectedIndex].className == "clear") {
			this.clear();
		} else if(this.selector.options[this.selector.selectedIndex].className) {
			this.all.each(function(s){
				
				if(s.className == this.selector.options[this.selector.selectedIndex].className || s.className === "all" ) {
					this.selection.options[this.selection.options.length] = s;
				}
			}.bind(this));
		} else {
			this.reset();
		}
	},
	clear: function() {
		this.selection.length = 0;
	},
	reset: function() {
		this.all.each(function(s){
			this.selection.options[this.selection.options.length] = s;
		}.bind(this));
	}
} 

