/**
 * Select input manipulations
 */
var Select = Class.create();
Select.prototype = {
  	initialize: function(obj) {
		this.obj = $(obj);
		this.serialize = null;
	},
	
	// listen to selection in order to move selected to another select
	createMoveListener: function(obj) {
		// initialize the target select object
		this.target = obj;
		
		this.moveReference = null;
		if (arguments.length >= 2)
			this.moveReference = $(arguments[1]);
		
		this.obj.observe('change', this.moveOptions.bindAsEventListener(this));
	},
	
	// move selected elements to another select
	moveOptions: function() {
		for (var i = this.obj.options.length - 1; i >= 0; i--)
			if (this.obj.options[i].selected) {
				this.obj.options[i].selected = false;
				if (this.moveReference == null || !this.moveReference.value || this.moveReference.value == this.obj.options[i].value.replace(/-\d+/, ''))
					this.target.obj.options[this.target.obj.options.length] = new Option(this.obj.options[i].text, this.obj.options[i].value);
				this.obj.options[i] = null;
			}
		this.target.orderOptions();
		
		this.checkSerializer();
		this.target.checkSerializer();
	},
	
	// order options in select
	orderOptions: function() {
		var opts = new Array();
		var j = 0;
		for (var i = this.obj.options.length - 1; i >= 0; i--) {
			opts[j++] = new Option(this.obj.options[i].text, this.obj.options[i].value);
			this.obj.options[i] = null;
		}
		opts.sort(function(a, b) {
			if (a.text < b.text)
				return -1;
		   	if (a.text > b.text)
				return 1;
		   return 0;
		});
		for (i = 0; i < opts.length; i++)
			this.obj.options[i] = opts[i];
	},
	
	// serialize values
	serializeValues: function() {
		values = '';
		for (var i = 0; i < this.obj.options.length; i++) {
			if (values != '') values += ',';
			values += this.obj.options[i].value;
		}
		
		return values;
	},
	
	// populate from array
	populate: function(arr, empty) {
		if (empty) this.empty();
		for (var i = 0; i < arr.length; i++)
			this.obj.options[i] = new Option(arr[i].label, arr[i].id);
	},
	
	// empty select
	empty: function() {
		for (var i = this.obj.options.length - 1; i >= 0; i--)
			this.obj.options[i] = null;
	},
	
	// add an item
	addItem: function(obj) {
		this.obj.options[this.obj.options.length] = new Option(obj.label, obj.id);
		
		this.checkSerializer();
	},
	
	// remove an item
	removeItem: function(id) {
		for (var i = this.obj.options.length - 1; i >= 0; i--)
			if (this.obj.options[i].value == id) {
				this.obj.options[i] = null;
				break;
			}
			
		this.checkSerializer();
	},
	
	// set serialize handlers (target obj, serialize action, tc)
	setSerializer: function(opt) {
		this.serialize = opt;
	},
	
	// check if serialize is needed and perform it
	checkSerializer: function() {
		if (this.serialize != null) {
			if (this.serialize.obj != null)
				this.serialize.obj.value = this.serializeValues();
			if (this.serialize.action != null)
				this.serialize.action();
		}
	}
}