$(document).ready(
    function() {
        $('div.agallery > a').each(
            function() {
                var url=$(this).attr('href');
                $(this)
                    .removeAttr('href')
                    .css('cursor', 'pointer')
                    .click(
                        function() {
                            $('<div></div>')
                                .append($('<img src="'+url+'">')
                                    .load(
                                        function() {
                                            var parent =$(this).parent();
                                            parent.dialog({
                                                width: this.width+14,
                                                height: this.height+14,
                                                open: function() {
                                                    parent.click(
                                                        function() {
                                                            parent.remove();
                                                        }
                                                    );
                                                    $('div.ui-widget-overlay').click(
                                                        function() {
                                                            parent.remove();
                                                        }
                                                    );
                                                },
                                                close: function() {
                                                    parent.remove();
                                                },
                                                resizable: false,
                                                draggable: false,
                                                modal: true
                                            });
                                        }
                                    )
                                ).appendTo(document.body);
                        }
                    );
            }
        );
    }
);
