(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]', '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]', '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;
	}
});

// requires Prototype.js version 1.6 or greater
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()).without('Prof').without('Sir')[1] + '__' + 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
			if (element.up('.two_column_list')) {
				element.reorder_list(2);
			}
			
			if (element.up('.three_column_list')) {
				element.reorder_list(3);
			}
			
			// save the new order to speed up next time
			element.store('sorted_order', element.innerHTML);
		}
		
		return element;
	}
});

document.observe('dom:loaded', function () {

	$$('#tools_content a.bookmark').invoke('observe', 'click', function (event) {
		event.stop();
		Try.these(
			function () { window.external.AddFavorite(window.location.href, document.title); },
			function () { window.sidebar.addPanel(document.title, window.location.href, ''); },
			function () { alert('Please add this page to your favourites.'); }
		);
	});
	
	$$('#tools_content a.print').invoke('observe', 'click', function (event) {
		event.stop();
		window.print();
	});

	if (typeof Vx === 'undefined') {
		var options = {
			duration: 0.6,
			afterFinish: function () {
				animating = false;
			}
		}
		var stretcher = $$('.stretcher'), stretch = $$('.stretch'), animating = false;
		stretch.map(function (el) {
			el.writeAttribute('_height', (el.getHeight()+10) + 'px');
		});
		stretch.invoke('setStyle', { overflow: 'hidden', height: '1px' });
		stretcher.invoke('observe', 'click', function (event) {
			event.stop();
			if (!animating) {
				animating = true;
				var content = this.next('.stretch');
				if (content.getHeight() === 1) {
					content.morph('height:' + content.readAttribute('_height'), options);
				} else {
					content.morph('height:1px', options);
				}
			}
		});
	}
	
	$$('.breadcrumbs').each(function (bc) {
		if (bc.down('ul')) {
			bc.down('ul').insert('<li>' + document.title.split(' | ').slice(1).join(' | ') + '</li>');
		}
	});
	
	if (typeof Vx === 'undefined') {
		$$('.four_column ul').invoke('reorder_list', 4);
	}
											
if (typeof Vx === 'undefined') {
		try {
			var location_sub = /([^\/]+?)\/([^\/]+?)\.aspx?$/;
			var folder_name, file_name, window_location;
			window.location.href.gsub(location_sub, function (match) {
				window_location = match[0];
				folder_name = match[1];
				file_name = match[2];
			});

			$$('.vx_menu a', '.submenu a').each(function (element) {
				$(element).readAttribute('href').gsub(location_sub, function (match) {
					if (match[0] === window_location) {
						var active_elements = $(element).ancestors().select(
							function (el) { return (el.tagName.toUpperCase() === 'LI') }
						);

						active_elements.invoke('addClassName', 'selected');
					}
				});
			});
		} catch (address_error) {}
	}
	
	$$('#alphabetise .people_list ul').invoke('alphabetise');
	$$('.barr_col_2.people_list ul').invoke('reorder_list', 3);
	
});
