var addProfileSelectorEventHandler, available_selections;

addProfileSelectorEventHandler = function () {
	$('profile_selector').observe('change', function (e) {
		var box, sf;
		box = Event.element(e);
		sf = box.up('form');
		sf.insert('<input type="hidden" name="' + $F(box) + '" value="' + $F(box) + '" />');
		sf.submit();
	});
};
available_selections = [
	{'value': 'lucky', 'text': 'en tilfeldig profil'},
	{'value': 'students', 'text': 'studenter'},
	{'value': 'employees', 'text': 'ansatte'},
	{'value': 'finishedStudents', 'text': 'siste fullførte'},
	{'value': 'updatedStudents', 'text': 'siste oppdaterte'}
];
Event.observe(window, 'load', function (e) {
	var lab, el, insertOption;
	lab = new Element('label', {'for': 'profile_selector'}).update('eller velg gruppe:');
	el = new Element('select', { 'id': 'profile_selector', 'name': 'profile_selector'});
	insertOption = 
		function (n) {
			var selected, showing, opt;
			selected = $F($('current_selection')) === n.value;
			showing = (selected) ? 'Viser' : 'Vis';
			opt = new Element('option',	{'value': n.value}).update(showing + ' ' + n.text);
			if (selected) {
				opt.writeAttribute('selected', 'selected');
			}
			el.insert({	bottom: opt });
		};
	available_selections.each(insertOption);
	if ($F($('current_selection')) === "search") {
		insertOption({'value': 'search', 'text': 's&oslash;keresultat'});
	}
	$('current_selection').insert({after: el});
	$('current_selection').insert({after: lab});
	addProfileSelectorEventHandler();
});
