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

        /**
         *  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();
                }
            );

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

        $('.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())
                        )
                }
            });

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

        /**
         *  Photos
         */
        $('.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');
                }
            });

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

        /**
         *  Featured slideshow
         */
        $('.widget_links .blogroll:has(li img:only-child)')
            .addClass('slideshow')
            .each(function() {
                var index = 0,
                    list = $(this)
                        .find('li')
                        .each((function() {
                            var container = $(this);
                            // the actual each function
                            return function() {
                                if($(this).outerHeight() > container.innerHeight()) {
                                    container.height($(this).outerHeight());
                                }
                            }
                        }).call(this));

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

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

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

                        $(list.get(index))
                            .fadeIn();
                    },
                    7500
                );
            });

        $(window).load(function() {
            $('.dashboard-grid')
                .masonry({
                    singleMode: true,
                    resizable: true
                });

            /**
             *  Fix anchor
             */
            if ( window.location.hash ) {
                var destination = $( window.location.hash ).offset().top;
                $('html:not(:animated),body:not(:animated)').scrollTop( destination );
            }
        });

    } catch(e) {
        console.debug(e);
    }
})(jQuery);

