(function($){
$.fn.extend({
	drawers : function(options){
		if(null === options){
			options = {};
		}
		options = $.extend({}, $.Drawers.defaults, options);
		
		return this.each(function(index){
			new $.Drawers(this, options);
		});
	}
});

$.Drawers = function(wrap, options){
	var $wrap = $(wrap);
	var menus = [];
	
	$wrap.find('.' + options.headerClass).each(function(){
		var $li = $(this);
		var a = $li.find('a:first');
		var ul = $li.find('ul:first');
		menus.push(new $.Drawers.Menu(a, ul, options));
	});
}

$.Drawers.defaults = {
	drawerClass : 'drawer'
	,headerClass : 'drawer-header'
	,toggleMethod : 'click'
	,preOpen : []
};

$.Drawers.Menu = function(a, ul, options){
	var $a = $(a);
	var $ul = $(ul);
	
	function init(){
		if(!$ul.hasClass('drawer-open')){
			$ul.hide();
		}
		$a.bind(options.toggleMethod, {}, toggle);
	};
	
	function toggle(e){
		$ul.slideToggle();
		e.preventDefault();
	};
	
	init();
	
	return {
		init : function(){
			init();
		}
		,toggle : function(){
			toggle();
		}
	}
}
})(jQuery);
