$(document).ready(function() {
    // remove stuff the static version doesn't need
    $("img.serendipity_tweetback_check").remove();
    $("#blog-search").remove();
    $("ul#nav>li:nth-child(4)").remove();
    $("ul#nav>li:nth-child(3)").remove();
    $("ul#nav>li:nth-child(2)").remove();
    $("#trackback-url").remove();
    $("#comment-feed").remove();
    $("#feedback").remove();
    
    // Mark external links with an arrow
    $("body a:not(:has(img))").filter(function() {
        return this.hostname && this.hostname !== location.hostname;
    }).append('&#8599;');

    // Hide and expand comment directions
    // adapted from http://jqueryfordesigners.com/slidedown-animation-jump-revisited/
    var $dir = $('#directions');
    var heightDirections = $dir.height();
    $dir.hide().css({ height : 0 });

    $('#toggle-directions').click(function (eventSearch) {
        eventSearch.preventDefault();
        if ( $dir.is(':visible') ) {
             $dir.animate({ height: 0 }, { duration: 500, complete: function () {
                 $dir.hide();
             } 
        });
        } else {
            $dir.show().animate({ height : heightDirections }, { duration: 500 });
        }
        return false;
    });

    // Show/hide info on hovering the logo
    $('#info').hide();
    $('#logo').hover(function(){
        $('#info').fadeIn('slow');
    }, function(){
        $('#info').fadeOut('slow');
    });
    // same for focus
    $('#logo a').focus(function(){
        $('#info').fadeIn('slow');
    });
    $('#logo a').blur(function(){
        $('#info').fadeOut('slow');
    });

    // Enhance the search form by displaying/hiding a default search term
    var searchDefault = "Suchbegriff(e)";
    $('#searchterm').val(searchDefault)
        .focus(function() {
            if (this.value == searchDefault) {
                $(this).val('');
            };
        })
        .blur(function() {
            if (this.value == '') {
                $(this).val(searchDefault);
            };
        });
    // If there's no user input, but the form is submitted anyway, remove
    // the default search term
    $('#search').submit(function() {
        if ($('#searchterm').val() == searchDefault) {
            $('#searchterm').val('');
        }
    });

    // Replaces the default inline s9y alert box for trackback url
    $('#trackback-url').click(function() {
        alert('Dieser Link ist nicht aktiv. Er enthält die Trackback-URI zu diesem Eintrag. Sie können diese URI benutzen, um Ping- und Trackbacks von Ihrem eigenen Blog zu diesem Eintrag zu schicken. Um den Link zu kopieren, klicken Sie ihn mit der rechten Maustaste an und wählen "Verknüpfung kopieren" im Internet Explorer oder "Linkadresse kopieren" in Mozilla/Firefox.');
        return false;
    });

    // Some DOM manipulation 
    // Manipulate DOM for freetag plugin
    $('.freetag_cloud h2').each(function() {
        $(this).replaceWith("<h1>" + $(this).text() + "</h1>");
    });

    $('.serendipity_freetag_taglist_related').each(function() {
        $(this).replaceWith("<h2>" + $(this).text() + "</h2>");
    });

    // Hide and expand related articles
    $('#related-articles-desc').wrapInner('<a id="toggle-related" href=\"#related-articles\"></a>');
    var $rel = $('#related-articles');
    var heightRelated = $rel.height();
    $rel.hide().css({ height : 0 });

    $('#toggle-related').click(function (eventRelated) {
        eventRelated.preventDefault();
        if ( $rel.is(':visible') ) {
             $rel.animate({ height: 0 }, { duration: 500, complete: function () {
                 $rel.hide();
             } 
        });
        } else {
            $rel.show().animate({ height : heightRelated }, { duration: 500 });
        }
        return false;
    });

    // Replace epicon with proper arrows
    $('.epicon').remove();
    $('.serendipity_entrypaging_left a').prepend('&larr; ');
    $('.serendipity_entrypaging_right a').append(' &rarr;');

    // Fancybox
    // http://fancybox.net/
    $('.serendipity_image_link').fancybox();
});

