jQuery().ready(function() { $('.part.collapsable').each(function() { var height = $(this).height(); var paddingBottom = parseInt($(this).css('padding-bottom')); var marginBottom = parseInt($(this).children('.title').css('margin-bottom')); $(this).children('.title').css({ cursor: 'pointer', 'background-image': 'url("/images/arrow_method.png")' }).click(function() { if($(this).parent().hasClass('closed')) { $(this).animate({ 'margin-bottom': marginBottom }, height).parent().removeClass('closed').addClass('opened').animate({ height: height, 'padding-bottom': paddingBottom }, height); } else if($(this).parent().hasClass('opened')) { $(this).animate({ 'margin-bottom': 0 }, height).parent().removeClass('opened').addClass('closed').animate({ height: $(this).height() + 1, 'padding-bottom': 0 }, height); } }); if($(this).hasClass('closed')) { $(this).css({ 'padding-bottom': '0', height: $(this).children('.title').height() + 1 }).children('.title').css({ margin: 0 }); } else if(!$(this).hasClass('closed') && !$(this).hasClass('opened')) { $(this).addClass('opened'); } }); });