$(document).ready(function () {

    // Show and hide login form
    $("#menu .user .login a").toggle(
        function(){
            $("#amicastlogin").show();
        },
        function(){
            $("#amicastlogin").hide();
        }
    );
    
    // Fix entries margin
    
    $("#p-frontpage .entries div:nth-child(6)").addClass("end");
    $("#p-frontpage .entries div:last-child").addClass("end");
    $("#p-browse .entries div:nth-child(4n)").addClass("end");
    $("#p-viewpost .entries div:nth-child(2n)").addClass("end");
    $("#p-profile .entries div:nth-child(3n)").addClass("end");
    $("#p-search .entries div:nth-child(5n)").addClass("end");
    
    // Hide ads
    
    $("#p-viewpost .thisentry .amifoo .close").click(function(event){
        $("#p-viewpost .thisentry .amifoo").hide();
        $("#p-viewpost .thisentry .trans").hide();
        event.preventDefault();
    });
    
    // Fix broken images
    
    $(".entry-img img").error(function () {
        $(this).remove();
    });
    
    // Make thumbs clickable
    
    $(".entry").each(function(){
        var link = $(this).find("h3 a").attr("href");
        $(this).find("img").wrap("<a href=\""+link+"\"></a>");
    });
    
    // Search
    
    $("#p-search #header .search span.inp").show();
    
    $("#searchBarForm .btn-search input").click( function(event) {
        if ($('#header .search span.inp').is(':hidden')) {
            $("#header .search span.inp").show();
            $("#header .search span.inp input").focus();
            $("#header .search span.inp input").attr("value","");
            event.preventDefault();
        } else {
            var keywords = $("#header .search span.inp input").attr("value");
            if (keywords == "") {
                event.preventDefault();
            } else {
                $("#searchBarForm").submit();    
            }
        }
    });
});

