$(document).ready(function(){
	initMenu();
});
/*--- menu accordion function ---*/
function initMenu(){
	$('.menu > ul').each(function(){
		var _list = $(this).children('li:has(".slide")');
		var _a = _list.index(_list.filter('.active:eq(0)'));
		_list.each(function(_i){
			this._btn = $(this).children('a').eq(0);
			this._box = $(this).children('.slide').eq(0);
			if(_i == _a) this._box.show();
			else this._box.hide();
			this._btn.click(function(){
				if(_a != -1){
					_list.get(_a)._box.stop().animate({height: 0}, 300, function(){
						$(this).css({display: 'none', height: 'auto'});
						$(this).parent().removeClass('active');
					});
				}
				if(_a != _i){
					_list.eq(_i).addClass('active');
					if(!_list.get(_i)._box.is(':animated')){
						_list.get(_i)._h = _list.get(_i)._box.height();
						_list.get(_i)._box.css({height: 0, display:'block'});
					}
					_list.get(_i)._box.stop().animate({ height: _list.get(_i)._h}, 300, function(){
						$(this).height('auto');
					});
					_a = _i;
				}
				else{
					_a = -1;
				}
				return false;
			});
		});
		$(this).children().not(_list).children('a').click(function(){
			if(_a != -1){
				_list.get(_a)._box.stop().animate({height: 0}, 300, function(){
					$(this).css({display: 'none', height: 'auto'});
					$(this).parent().removeClass('active');
				});
				_a = -1;
			}
		});
	});
}
