$(document).ready (function() {

	function dHide(e){
		var target = e.srcElement || e.target;
		if (!$('ul.main-menu')[0].contains(target)){
			$('ul.main-menu > li').removeAttr('class');
			$('ul.main-menu > li').children('ul').hide('fast');
			$(document).unbind('click', dHide);
		}
	}

	$('.search-button').bind('click', function(){$('#search-form').submit();});
	$('ul.main-menu > li > ul.active').parent().addClass('active');

	$('ul.main-menu > li > a').bind('click', function(){
		if ($(this).parent().children('ul').children('li').length > 0){
			if ($(this).parent().hasClass('active')){
				$(this).parent().children('ul').hide('fast');
				$(this).parent().removeAttr('class');				
            } else {
				$('ul.main-menu > li').removeAttr('class');
				$('ul.main-menu > li').children('ul').hide('fast');
				$(this).parent().addClass('active');
				$(this).parent().children('ul').show('fast');
			}

			$(document).bind('click', dHide)
			return false;
		} else {			
			return true;
        }
	});

	$('ul.menu-level-1 > li > a').bind('click',	function(){
		if ($(this).parent().children('ul').children('li').length > 0){
			if($(this).parent().hasClass('active')){
				$(this).parent().parent().children('li').children('ul').hide('fast');
				$(this).parent().parent().children('li').removeAttr('class');				
			} else {
				$(this).parent().parent().children('li').removeAttr('class');
				$(this).parent().parent().children('li').children('ul').hide('fast');
				$(this).parent().addClass('active');
				$(this).parent().children('ul').show('fast');
			}
			return false;
		} else {
			return true;
        }
	});

});

