(function () {
	var resetInput = function () {
		var element = (typeof $(this).hasClassName === 'function') ? $(this) : $(arguments[0]);
		if (!element.hasClassName('prefilled') && element.getValue() === element.defaultValue) {
			element.setValue('');
		}
		return element;
	};
	var clearForms = function () {
		var form_elements = $$('input[type=text]', 'input[type=password]', 'textarea');
		form_elements.invoke('observe', 'focus', resetInput);
		form_elements.invoke('observe', 'blur', function () {
			if ($F(this).blank()) {
				this.setValue(this.defaultValue);
			}
		});
		$$('form').invoke('observe', 'submit', function () {
			this.select('input[type=text]', 'input[type=password]', 'textarea').map(resetInput);
		});
	};

	document.observe('dom:loaded', clearForms);
})();

// Element.Storage API
// each element contains an ID attribute, which is the key of the Element.Storage hash
// storing separate pseudo-attributes on each element can cause memory leaks in IE,
// but by only holding the element's ID with the element, and then a hash of all the
// element's pseudo-attributes with the ID as a key we can reduce the memory leaks
// and only have one unique point of access for all elements, even if they have been
// removed from the document.
var Storage = {
	create: function (element) {
		if (typeof window.Element.Storage === 'undefined') {
			Element.Storage = new Hash();
		}
 
		element = $(element);
		var storage = Element.Storage,
		id = element.identify();
 
		if (!storage.keys().include(id)) {
			storage.set(id, new Hash());
		}
 
		return element;
	}
};
 
Element.addMethods({
	store: function (element, rules, value) {
		element = Storage.create(element);
		var storage = Element.Storage.get(element.identify()),
			attributes = {};
 
		if (typeof rules === 'object') {
			attributes = rules;
		} else {
			attributes[rules] = value;
		}
 
		for (var key in attributes) {
			if (attributes.hasOwnProperty(key) && typeof attributes[key] !== 'undefined') {
				storage.set(key, attributes[key]);
			}
		}
 
		return element;
	},
 
	retrieve: function (element, property_name, property_default_value) {
		element = Storage.create(element);
		var storage = Element.Storage.get(element.identify());
 
		if (!storage.keys().include(property_name)) {
			element.store(property_name, property_default_value);
		}
 
		return storage.get(property_name);
	},
 
	eliminate: function (element, property_name) {
		element = Storage.create(element);
		var storage = Element.Storage.get(element.identify());
 
		if (storage.keys().include(property_name)) {
			storage.unset(property_name);
		}
 
		return element;
	}
});

Element.addMethods({
	reorder_list: function (element, column_count) {
		// this function fixes the display order of floated lists
		element = $(element);
		element.select('ul').invoke('remove');
		var old_list = $A(element.select('li')),
			new_list = $A(old_list);

		var limit = old_list.size();
		for (var i = 0, j = 0, k = 1; i < limit; i++) {
			if (j >= limit) {
				j = k;
				k++;
			}

			new_list[j] = $(old_list[i]).cloneNode(true);
			j = j + column_count;
		}

		element.update('');

		for (var i = 0; i < limit; i++) {
			element.insert({
				bottom: new_list[i]
			});
		}
		
		return element;
	},
	
	alphabetise: function (element) {
		element = $(element);
		
		if (element.retrieve('sorted_order')) {
			// if we have already sorted this column, use a shortcut
			element.innerHTML = element.retrieve('sorted_order');
		} else {
			// otherwise, store the original order
			element.store('original_order', element.innerHTML);
			
			// select all of the list items
			var list_items = element.select('li');
			var list = new Hash();
			
			// work out the surname for each person
			list_items.each(function (li) {
				// find the surname without Prof or Sir to confuse
				var surname = $w(li.innerHTML.stripTags().replace(/\sQC$/, '')).last() + '__' + li.innerHTML.stripTags();
				
				// return the element with surname
				list.set(surname, li.cloneNode(true));
			});
			
			// empty the list
			element.update('');
			
			// insert the ordered list items
			list.keys().sort().each(function (surname) {
				element.insert(list.get(surname));
			});
			
			// ensure we re-order the lists into columns
			element.reorder_list(3);
			
			// save the new order to speed up next time
			element.store('sorted_order', element.innerHTML);
		}
		
		return element;
	},
	
	seniority: function (element) {
		element = $(element);
		element.update(element.retrieve('original_order', element.innerHTML));
		return element;
	}
});

document.observe('dom:loaded', function () {
	var highlightColumn = function (cell) {
		if (cell.previous(cell.tagName.toLowerCase())) {
			cell.addClassName('highlight');
		}
	};
	
	$$('#content .content table tr th:nth-child(odd)',
	   '#content .content table tr td:nth-child(odd)').map(highlightColumn);
	
	if (document.getElementById('search-barrister_id')) {
		$('search-barrister_id').observe('change', function () {
			if (!this.getValue().empty()) {
				this.up('form').submit();
			}
		});
	}
	
	$$('.members-list ul').invoke('reorder_list', 3);
	
	if (document.getElementById('order-seniority')) {
		$('order-seniority').observe('click', function (event) {
			event.stop();
			$$('.members-list ul').invoke('seniority');
		});
	}
	
	if (document.getElementById('order-alpahbetical')) {
		$('order-alpahbetical').observe('click', function (event) {
			event.stop();
			$$('.members-list ul').invoke('alphabetise');
		});
	}
});
