(function($) {
    try {
    /**
     *  The sidebar accordion
     */
    $('#content .individual.post .sidebar, #content-sidebar')
        .accordion({
            header: '.widget h3',
            autoHeight: false
        })

    /**
     *  Code block markups
     */
    var is_only_child = function(child_tag, parent) {
        return parent.text() == parent.find(child_tag).text();
    }

    $('#content .post p code:only-child')
        .parent('p')
        .each(function() {
            console.debug();
            if(is_only_child('code', $(this))) {
                $(this)
                    .find('br')
                        .remove()
                    .end()
                    .replaceWith(
                        $(document.createElement('pre'))
                            .append($(this).children())
                    )
            }
        });

    $('#content .post pre code:only-child')
        .parent('pre')
        .each(function() {
            if(is_only_child('code', $(this))) {
                $(this)
                    .text($.trim($(this).text()));
            }
        })
        .addClass('code ln-');

    /**
     *  Photos
     */
    $('#content .post p a:first-child:has(img) + br')
        .parent('p')
        .each(function() {
            // nodeType https://developer.mozilla.org/en/nodeType 
            if($(this).contents().get(0).nodeType != 3) {
                $(this)
                    .addClass('photo');
            }
        });

    $('#content .post p img:only-child')
        .parents('p')
        .each(function() {
            if($.trim($(this).text()) == '') {
                $(this).addClass('photo')
            }
        });

    /**
     *  Comment information
     */
    $(document.createElement('span'))
        .addClass('toggle')
        .addClass('toggle-inactive')
        .appendTo('#content-comments form .information')
            .siblings('p')
            .hide()
        .end()
        .text('Show information')
        .toggle(
            function() {
                $(this)
                    .addClass('toggle-active')
                    .removeClass('toggle-inactive')
                    .text('Close information')
                    .siblings('p')
                    .slideDown();
            },
            function() {
                $(this)
                    .text('Show information')
                    .removeClass('toggle-active')
                    .addClass('toggle-inactive')
                    .siblings('p')
                    .slideUp();
            }
        );

    /**
     *  Frontpage dashboard overload
     */
    $('#content-dashboard .widget')
        .slice(3)
        .addClass('collapsible collapsed')
        .find('.body')
        .hide()
        .end()
        .find('h3')
        .toggle(
            function() {
                $('#content-dashboard .collapsible')
                    .removeClass('collapsed')
                    .addClass('expanded')
                    .find('.body')
                    .slideDown();
            },
            function() {
                $('#content-dashboard .collapsible')
                    .addClass('collapsed')
                    .removeClass('expanded')
                    .find('.body')
                    .slideUp();
            }
        )

    /**
     *  Featured slideshow
     */
    $('#content-dashboard .widget_links:has(.blogroll li img:only-child)')
        .addClass('slideshow')
        .each(function() {
            var index = 0, list = $(this).find('li');

            $(list.hide().get(index)).fadeIn();

            setInterval(
                function() {
                    $(list.get(index))
                        .fadeOut();

                    index = (index + 1) % list.length;

                    $(list.get(index))
                        .fadeIn();
                },
                7500
            );
        })
    } catch(e) {
        console.debug(e);
    }
})(jQuery);
