var ARIArticleSorting = new Class({
	CLASS_FIELD: 'sort_field',
	
	CLASS_DIR: 'sort_dir',
	
	CLASS_ITEM: 'sort_item',

	options: {
		containerId: null,
		hiddenId: null
	},

	initialize: function(options) {
		this.setOptions(options);
		
		var self = this;
		$(this.options.containerId).getElements('.' + this.CLASS_FIELD).each(function(el) {
			if (typeof(jQuery) != 'undefined' && typeof(jQuery.fn.chosen) != 'undefined')
				jQuery(el).chosen().change(function(e, data) {
					self.saveSettings();
				});
			
			$(el).addEvent('change', function(event) {
				self.saveSettings();
			});
		});
		
		$(this.options.containerId).getElements('.' + this.CLASS_DIR).each(function(el) {
			if (typeof(jQuery) != 'undefined' && typeof(jQuery.fn.chosen) != 'undefined')
				jQuery(el).chosen().change(function(e, data) {
					self.saveSettings();
				});
			
			$(el).addEvent('change', function(event) {
				self.saveSettings();
			});
		});
	},
	
	saveSettings: function() {
		var settings = [],
			self = this;

		$(this.options.containerId).getElements('.' + this.CLASS_ITEM).each(function(el) {
			var field = $(el).getElement('.' + self.CLASS_FIELD).value, 
				dir = $(el).getElement('.' + self.CLASS_DIR).value;

			if (field && dir)
				settings.push(field + ',' + dir);
		});

		$(this.options.hiddenId).value = settings.join(';');
	}
});
ARIArticleSorting.implement(new Options);