/* General */
function log(str) {
        $("#log").append(str + "<br/>");
}

/* Collapsibles */
function expand(elem) {
        var p = $(elem).closest(".wrapping");
        $(".collapsible", p).slideDown();
        $("a.collapse", p).show();
        $(elem).hide();
}

function collapse(elem) {
        var p = $(elem).closest(".wrapping");
        $(".collapsible", p).slideUp();
        $("a.expand", p).show();
        $(elem).hide();
}

function do_collapsibles() {
        $(".wrapping").each(
                function() {
                        var w = $(this);
                        var exp = $("a.expand", w);
                        var col = $("a.collapse", w);
                        exp.click(
                                function () {
                                        expand(this); 
                                        return false; 
                                }
                        );
                        col.click(
                                function () {
                                        collapse(this);
                                        return false; 
                                }
                        );
                        if (w.is(".expanded")) {
                                exp.hide();
                                col.show();
                        } else {
                                $(".collapsible", w).hide();
                                exp.show();
                                col.hide();
                        }
                }
        );

        $("[rel='hide_all']").show().click(
                function () {
                        $("a.collapse").each( 
                                function () { 
                                        collapse(this);
                                }
                        );
                        $("[rel='hide_all']").hide();
                        $("[rel='show_all']").show();
                        return false;
                }
        );

        $("[rel='show_all']").hide().click(
                function () {
                        $("a.expand").each(
                                function () { 
                                        expand(this);
                                }
                        );

                        $("[rel='hide_all']").show();
                        $("[rel='show_all']").hide();
                        return false;
                }
        );
}

/* Article handling */
function do_articles() {
        function after() {
                /* Correct link back to original address */
                var article = $(this).closest("div.article");
                var link = $("[rel=more]", article);
                var alias = link.attr("id").split("_", 2)[1];
                link.attr("href", "/article/" + alias);

        }

        function handler() {
                var article = $(this).closest("div.article");
                var alias = $(this).attr("id").split("_", 2)[1];
                var body = $(".art_body", article);

                body.html('Loading...<img src="/media/asciispinner.gif" alt="spinner" />');
                body.slideDown();


                /* Hide all articles except for this one */
                $("a.collapse", $("div.article").not(article)).each(
                        function () { collapse(this); }
                );
                
                body.load("/get", {article: alias});
                $(this).html("Go directly to the article page");

                return false;
        }

        $("[rel=more]").one("click", handler);        
}


function do_comment_form() {
        /* List of validated fields */
        var elements = $('#comments input[type="text"], #comments textarea');
        var touched = {};
        var changed = false;

        function get_respond(data, status, request) {
                for (key in touched) {
                        var field = $("[name=" + key + "]").closest(".field");
                        
                        if (data[key])
                                $(".errors", field).html(data[key]);
                        else
                                $(".errors", field).html("<span>Correct</span>");
                }
        }
        
        /* Mark non-empty as touched already! */
        elements.each(
                function () {
                        if ($(this).val().length > 0) {
                                touched[$(this).attr("name")] = 1;
                                changed = true;
                        }
                }
        );

        function validate() {
                var to_validation = {};
                elements.each(
                        function () {
                                var val = $(this).val();
                                var name = $(this).attr("name");
                                to_validation[name] = val; 
                        }
                );
                
                var data = {
                        check_comment_form: JSON.stringify(to_validation)
                };
                $.post('/get', data , get_respond, 'json');
        }

        elements.focusout(
                function () {
                        /* Add current field to list of validated */
                        touched[$(this).attr("name")] = 1;
                        validate();
                }
        );

        if (changed == true) {
                validate();
        }

        /* Help users with javascript fill the form. ;d */
        $("#gender input").attr("checked", false);
}

/* Register actions */
$(document).ready(
        function () {
                do_collapsibles();
                do_articles();
                do_comment_form();
        }
);