How to enable/disable custom button on selection change with tinymce
===

I use this code to create a custom tinymce button that changes a class of image. It's in the setup block.

    ed.addButton('cust_setimgaspreview', {
            title : 'Set image as a preview image',
            image : 'ikony/previews.png',
            onclick : function() {
                if(ed.selection.getNode().tagName == 'IMG')
                {
                     ed.selection.getNode().className = 'preview';
                } else {
                    alert('Musíte vybrat obrázek.');
                }
            }
        });
As you can see, I use an "ugly approach" to disable the class change on other elements than image. How can I disable/enable the button in the same way as tinymce does with its default buttons (like edit image or edit link)? I guess I need to somehow catch selection change and then change the button state depending on the selection, but I have no ide how to do that.