// Dropdown boxes
// =========================================
//
// Written by Ben Schultz # 14/7/2010

var Dropdown = new Class({
	
	initialize: function(element) {
		this.element 			= element;
		this.display 			= element.getElement('p');
		this.default_display	= this.display.get('text');
		this.selected 			= new Array();
		
		this.element.addEvent('click', function() {
			this.element.addClass('active');
			
			var cancel_event = function() {
				this.element.removeClass('active');
				this.element.removeEvent('mouseleave', cancel_event);
			}.bind(this);
			
			this.element.addEvent('mouseleave', cancel_event);

		}.bind(this));
		
		$$('#' + this.element.id + ' input').each(function(input) {
			input.addEvent('click', function(e) {
				this.set_display();							  
			}.bind(this));
		}.bind(this));
		
		this.set_display();

	},
	
	set_display: function() {
		this.selected.empty();
		
		$$('#' + this.element.id + ' input').each(function(input) {
			if (input.checked == true) {
				if (input.get('rel') == null) {
					this.selected.push(input.value);	
				} else {
					this.selected.push(input.get('rel'));	
				}					
			}
		}.bind(this));
		
		if (this.selected.length == 0) {
			this.display.set('text', this.default_display);
		} else if (this.selected.length == 1) {
			this.display.set('text', this.selected[0].substring(0, 15));	
		} else {
			this.display.set('text', 'Multiple');	
		}
	}
	
});
