var fix_utf8 = function (string) {
	var _string = string;
	
	_string = _string.replace(/%E0/g, String.fromCharCode(224)); // à
	_string = _string.replace(/%E4/g, String.fromCharCode(228)); // ä
	_string = _string.replace(/%E8/g, String.fromCharCode(232)); // è
	_string = _string.replace(/%E9/g, String.fromCharCode(233)); // é
	_string = _string.replace(/%F6/g, String.fromCharCode(246)); // ö
	_string = _string.replace(/%FC/g, String.fromCharCode(252)); // ü
	
	_string = _string.replace(/%C3%A0/g, String.fromCharCode(224)); // à
	_string = _string.replace(/%C3%A4/g, String.fromCharCode(228)); // ä
	_string = _string.replace(/%C3%A8/g, String.fromCharCode(232)); // è
	_string = _string.replace(/%C3%A9/g, String.fromCharCode(233)); // é
	_string = _string.replace(/%C3%B6/g, String.fromCharCode(246)); // ö
	_string = _string.replace(/%C3%BC/g, String.fromCharCode(252)); // ü
	
	return _string || string;
}

document.observe('dom:loaded', function () {
	if ($$('#column1 .menu').first()) {
		var areas = $$('#column1 .menu').first(),
			menu = areas.down('ul'),
			items = menu.select('li > a'),
			subsubmenu = menu.select('ul > li > ul');
		
		subsubmenu.invoke('remove');
		var submenu = menu.select('ul');
		
	
		submenu.invoke('wrap', 'div', { className: 'submenu' }).invoke('hide');
	
		items.invoke('observe', 'click', function (event) {
			if (this.down('div.submenu') && this.down('div.submenu').visible()) {
				return this;
			}
			
			if (this.next('div.submenu')) {
				// this means the link we have clicked on
				submenu.invoke('up', 'div.submenu').each(function (s) {
					if (s !== this.next('div.submenu') && s.visible()) {
						Effect.BlindUp(s, { duration: 0.5 });
					}
				}, this);
		
				if (this.next('div.submenu')) {
					event.stop();
					if (!this.next('div.submenu').visible()) {
						Effect.BlindDown(this.next('div.submenu'), { duration: 0.5 });
					}
				}
			}
		});
		
		if (typeof Vx === 'undefined') {
			var location_regex = /([^\/]+?)\/([^\/]+?)\.([A-Za-z0-9]+?)(?:#[a-z0-9]+?)?$/;
			var window_location, folder_name, file_name;
			
			fix_utf8(window.location.href).replace('/latest-', '/').gsub(location_regex, function (match) {
				window_location = match[0];
				folder_name = match[1];
				file_name = match[2];
			});
			
			var active_submenu = items.inject($A(), function (accumulator, element) {
				element.readAttribute('href').gsub(location_regex, function (match) {
					if (window_location === match[0]) {
						element.addClassName('selected');
						accumulator.push(element.up('div.submenu') || element);
					} else {
						if (match[2] === folder_name && match[1] !== folder_name) {
							if (element.up('div.submenu')) {
								element.addClassName('selected');
							}
							accumulator.push(element.up('div.submenu') || element);
						}
					}
				});
				
				return $A(accumulator);
			});
						
			active_submenu.invoke('show');
		}
	}
});