try {
	document.execCommand("BackgroundImageCache", false, true);
} catch (not_ie) {}

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

document.observe('dom:loaded', function () {
	if (typeof Vx === 'undefined') {
		Cufon.replace('div.content h1');
		
		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];
			});

			$$('div.vx_menu 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 (err) {}
	}
	
	$$('div.expandable_block').each(function (block, i) {
		var height = block.getHeight() + 'px';
		
		block.select('div.title').invoke('observe', 'click', function (event) {
			if (block.hasClassName('closed')) {
				block.morph('height: ' + height, { afterFinish: function () { block.removeClassName('closed'); } });
			} else {
				block.morph('height:55px', { afterFinish: function () { block.addClassName('closed'); } });
			}
		});
		
		if (i === 0) return;
		block.addClassName('closed');
	});
	
	if (typeof Vx === 'undefined') {
		$$('div.bulletin_download').each(function (download) {
			var link = download.select('a[href]').pluck('href');
			download.observe('click', function (event) {
				event.stop();
				if (link.first()) { window.location.href = link.first(); }
			});
		});
		
		$$('#brochure_options a.ajax').invoke('observe', 'click', function (event) {
			event.stop();
			new Ajax.Updater(this, this.readAttribute('href'), {
				method: 'get',
				onSuccess: function () {
					this.setOpacity(0.5);
				}.bindAsEventListener(this)
			});
		});
		
		$('view_brochure').observe('click', function (event) {
			event.stop();
			window.open(this.readAttribute('href'));
			
			return;
			
			var document_title = prompt('Please title your document', '25 Bedford Row');
			if (document_title) {
				window.open(this.readAttribute('href') + '?title=' + escape(document_title));
			} else {
				window.open(this.readAttribute('href'));
			}
		});
		
		$$('.barrister_search input').each(function (search) {
			var results = search.up('p').next('.barrister_search_results');
			var list = new Hash();
			
			search.observe('focus', function () {
				if (list.keys().size() === 0) {
					new Ajax.Request('../_search/barrister.asp?list=full', {
						method: 'get',
						evalJSON: 'force',
						onSuccess: function (transport, json) {
							list = $H(json);
						}
					});
				}
			});
			
			var current_match = -1;
			
			search.observe('keyup', function (event) {
				if (event.keyCode === Event.KEY_DOWN) {
					var matches = results.select('li');
					
					if (matches[current_match]) { matches[current_match].removeClassName('selected'); }
					current_match = current_match + 1;
					if (matches[current_match]) {
						matches[current_match].addClassName('selected');
					} else {
						current_match = current_match - 1;
					}
					
				} else if (event.keyCode === Event.KEY_UP) {
					var matches = results.select('li');

					if (matches[current_match]) { matches[current_match].removeClassName('selected'); }
					current_match = current_match - 1;
					if (matches[current_match]) {
						matches[current_match].addClassName('selected');
					} else {
						current_match = current_match + 1;
					}
					
				} else {
					if (!this.getValue().blank()) {
						var term = new RegExp(this.getValue(), 'gi');
						var results_list = list.keys().inject($A(), function (acc, i) {
							if (i.match(term)) {
								acc.push('<li><a href="' + list.get(i) + '">' + i + '</a></li>');
							}
					
							return $A(acc);
						});
				
						results.update(results_list.join('\n'));
					} else {
						results.update('');
					}
				}
			});
		});
		
		$$('.barrister_search form').invoke('observe', 'submit', function (event) {
			event.stop();
			if (this.down('li.selected a')) {
				window.location.href = this.down('li.selected a').readAttribute('href');
			}
		});
	}
});
