/*
Crea un cassetto a scomparsa con barra del titolo personalizzabile
OPTIONS ------------------------------
- state = 	'open' or 'close' (default close)
- title
*/
(function($){
    $.fn.extend({
        drawer : function (options){
            var defaults = {
                state :		'close',
                title:		'title'
            };
            var o = $.extend(defaults, options);
            var drawer = $(this);
            $(this).prepend("<div class='drawer-title-bar' style='min-width:"+$(this).width()+"px'><div class='drawer-title drawer-close' >"+o.title+"</div></div>");
            $(this).find('.drawer-title-bar').toggle(function() {
                $(drawer).drawerclose();
            }, function() {
                $(drawer).draweropen();
            });
            $(this).find('.drawer-title-bar').next().resize( function(){
                $(this).parent().css( 'min-width', $(this).width() );
            });
            if (o.state == 'close'){
                $(this).find('.drawer-title-bar').click();
            }else{
            	$(this).find('.drawer-title').removeClass('drawer-close').addClass('drawer-open');
            }
        },
        drawerclose : function(){
            $(this).children().next().animate({
                height: 'toggle',
                opacity: 0
            }, 200, function(){
                //$(this).parent().resize();
            });
            $(this).attr('state', 'close');
            $(this).find('.drawer-title').removeClass('drawer-open').addClass('drawer-close');
        },
        draweropen : function(){
            $(this).children().next().animate({
                height: 'toggle',
                opacity: 1
            }, 200, function(){
                $(this).parent().resize();
            });
            $(this).attr('state', 'open');
            $(this).find('.drawer-title').removeClass('drawer-close').addClass('drawer-open');
        }
    });
})(jQuery);

