window.addEvent('domready', function(){

	// new format box
	if ($('container').getElement('.sectionchooser')){	
  
  		// something
  		$('select-section')
  		.addEvent('change', function(ev){
  		
  			// save selected section
			var section = this.get('value');
			
			// loop over all categories
			$('container').getElements('.cat')
			.each(function(cat){
						
				// hide by default
				cat.addClass('hidden');
				
				// if we have the right class
				if (cat.hasClass('section' + section)){
					cat.removeClass('hidden');
				}
			
			});

			// hide all subcategories
			$('container').getElements('.sectionchooser .subcat').addClass('hidden');
				  			
  		})
  		.fireEvent('change');
  		
  		// add events to the submenus
  		$('container').getElements('.sectionchooser .cat.haschildren')
  		.addEvent('click', function(ev) {
  				
  			// stop default event		
			if (ev) ev.stop();
			
			// if selected already then go to me
			if (this.hasClass('selected')){
			
				location.href = (this.href) ? this.href : this.getElement('a').href;
				
			} else {
			
				// get my category id
				category_id = -1; //submenu.className.match(/cat/);
				split = this.className.split(' ');
				split.each(function(key) {
					
					if (key.replace('cat','')!=key && key.replace('cat','')!='') {
						category_id = key.replace('cat', '');	// find something matching cat24 where 24 is the target.
					}
				});
				
					
				// remove any other selections
				this.getSiblings().removeClass('selected');

				// search thru .subcat, hide them all
				$('container').getElements('.sectionchooser .subcat')
				.each(function(sc){
				
					// hide by default
					sc.addClass('hidden');

					// select applicable rows
					if (sc.hasClass('childof' + category_id)){
						sc.removeClass('hidden');
					}

				});
					
				// selected only shows the arrow change, the children need to be hidden separately
				this.addClass('selected');
				
			}  	
								
		});
		
		// prevent clicks on a elements
		$('container').getElements('.sectionchooser .cat.haschildren a').addEvent('click', function(ev){
			if (ev) ev.preventDefault();
		});

  }

});
