CAjs={

	prioritySelects:[],

	init:function() {
		$A([1,2,3]).each(function(index) {
			if($('courseApplication[courseApplicationCourse]['+index+'][priority]') != null) {
				CAjs.prioritySelects.push($('courseApplication[courseApplicationCourse]['+index+'][priority]'));
			}
		});

		if(CAjs.prioritySelects.length > 1) {
			CAjs.memorize();
			CAjs.events();
		}
	},

	events:function() {
		$A(CAjs.prioritySelects).each(function(select) {
			select.observe('change', function() {
				var newValue = $F(this);
				$A(CAjs.prioritySelects).each(function(select) {
					if(select.lastValue == newValue) {
						$A(select.getElementsByTagName("option")).each(function(opt) {
							opt.selected = (opt.value == this.lastValue);
						}.bind(this));
					}
				}.bind(this));
				CAjs.memorize();
			}.bindAsEventListener(select));
		});
	},

	memorize:function() {
		$A(CAjs.prioritySelects).each(function(select) {
			select.lastValue = $F(select);
		});
	}
}

Event.observe(window, 'load', CAjs.init);
