$(document).ready(function(){
    document.getElementById("warning-js").style.display = "none";
    $('input[type="text"]').focus(function() { $(this).addClass("active"); });
    $('input[type="text"]').blur(function() { $(this).removeClass("active"); });
    $('textarea').focus(function() { $(this).addClass("active"); });
    $('textarea').blur(function() { $(this).removeClass("active"); });

    /* admin login */

    $.get("/admin/isAuthorized", function(data) {
        if(data == "1") {
            $('#admin-login-show').html("Log out");
            $('#admin-login-show').click(function(e) {
                $.get("/admin/logout");
            });
            $('#front #admin-login-show').css("visibility","visible");

            // post form visible
            
            $('#front .post.new').css("display","inline-block");
            $('.post.new .title').width("97%");
            $('.post.new textarea').width("90%");

            // adding posts

            $('#new-post-form').submit(function() {
                var title = $(this).children('.title').val();
                var content = $(this).children('.excerpt').children('textarea').val();
                $.post("/admin/addPost", { title: title, content: content });
                $('#front .post.new').hide();
                $('<div class="post"><h2><a href="#">'+title+'</a></h2><div class="excerpt">'+content+'</div><p class="post-bottom"><a href="#">see more</a></p></div>').insertBefore('.post.new');
                return false;
            });

            // deleting posts
            
            $('<span> - <a href="#" class="admin-post-delete">delete</a></span>').insertAfter('.post-bottom a:first-child');
            $('.admin-post-delete').click(function() {
                var uri = $(this).parents().filter('.post').children('h2').children('a').attr("href").replace(/\/post\//i,"");
                $.post("/admin/deletePost", { uri: uri });
                $(this).parents().filter('.post').hide("slow");
            });
        }
        else {
            $('#admin-login-show').html("Log in");
            $('#admin-login-show').toggle(function() {
                $('#admin-login').show("slow");
            }, function() {
                $('#admin-login').hide("slow");
            });
            $('#front #admin-login-show').css("visibility","visible");
        }
    });
    
});