$(document).ready(function(){
	$('#nav li').each(function(){
		var _hold = $(this);
		var _btn = _hold.children('a');
		_btn.get(0)._f = true;
		var _box = _hold.children('div.block');
		if(_box.length){
			_btn.get(0)._f = false;
			var _h = _box.height();
			if(_hold.hasClass('opened')) _box.show();
			else _box.hide();
			_btn.click(function(){
				if(_hold.hasClass('opened')){
					_hold.removeClass('opened');
					_box.stop().animate({height:0}, 400, function(){$(this).css({display:'none', height:'auto'});});
					_hold.removeClass("active");
				}
				else{
					$('#nav > li').each(function(){
						$(this).removeClass("active");
						if ($(this).children('div.block').is(':visible')) {
							$(this).removeClass('opened');
							$(this).children('div.block').stop().animate({height:0}, 400, function(){$(this).css({display:'none', height:'auto'});});
						}
					});
					_hold.addClass('opened');
					if(_box.is(':hidden')){
						_box.show();
						_hold.addClass("active");
						_h = _box.height();
						_box.height(0);
					}
					_box.stop().animate({height: _h}, 400, function(){$(this).height('auto');});
				}
				return false;
			});
			_box.find('> ul > li > a').click(function(){
				if(this._f){
					_hold.removeClass('opened').removeClass("active");
					_box.stop().animate({height:0}, 400, function(){$(this).css({display:'none', height:'auto'});});
				}
			});
		}
	});
});
