﻿var Contact = {
	who: {},
	interests: {},
	whoHasInterests: {},
	initialInterest: '',

	init: function() {
		var selects = document.getElementById('git').getElementsByTagName('select');
		this.who = selects[0];
		this.interests = selects[1];
		this.initialInterest = this.interests.value;
		for (var item, people, person, len = this.interests.options.length, i = 0; i < len; i++) {
			item = this.interests.options[i];
			people = item.className.split(' ');
			for (var len2 = people.length, j = 0; j < len2; j++) {
				person = people[j];
				this.whoHasInterests[person] = this.whoHasInterests[person] ? this.whoHasInterests[person] : [];
				this.whoHasInterests[person][this.whoHasInterests[person].length] = [item.text, item.value];
			}
		}
		this.updateInterests();
		Event.add(this.who, 'mouseup', this.updateInterests.bind(this));
		Event.add(this.who, 'keypress', this.updateInterests.bind(this));
	},
	
	updateInterests: function() {
		var person = this.who.value;
		var interest = this.interests.value || this.initialInterest;
		var index = 0;
		this.interests.options.length = 0;
		for (var item, len = this.whoHasInterests[person].length, i = 0; i < len; i++) {
			item = this.whoHasInterests[person][i];
			this.interests.options[this.interests.options.length] = new Option(item[0], item[1]);
			if (item[1] == this.interests.value) index = i;
		}
		this.interests.options[index].selected = true;
	}
};

Event.add(window, 'load', Contact.init.bind(Contact));