Event.observe(window, "load", function() { Comments.init(); });

var Comments = {

    init: function() {
        Actions.attach("btnAddComment", "onclick", "PopupLogin.show", "'Comments.showForm'");
        Comments.showList();
        if($$(".editable_text").length > 0) Comments.setAdmin();
    },
    
    onSignalClick: function(commentId) {
        var raisonText = prompt('Entrer la raison du signalement :');
        if (raisonText != null) {
            new Ajax.Request('/signal-comment', {
                method: 'post',
                onSuccess: function() {
                    alert('Le signalement a \xE9t\xE9 envoy\xE9 aux mod\xE9rateurs.');
                },
                parameters: {
                    raisonSignal: raisonText,
                    commentId: commentId
                }
            });
        }
    },

    showList: function(href, hideCommentForm) {
        var href = href || "/commentslist/" + mediaId + "/page-1";
        var hideCommentForm = hideCommentForm || false;

        new Ajax.Updater(
            "commentsListContainer",
            href,
            {
                encoding: "ISO-8859-1",
                method: "get",
                onComplete: function() {
                    $$("#comments_paginator a").each(function(e) {
                        Actions.attach(e, "onclick", "Comments.showList", "'" + e.href + "'");
                        e.href = "#";
                    });
                    if(hideCommentForm) Comments.hideForm();
                    Comments.setAdmin();
                }
            }
        );
        return false;
    },

    showForm: function() {
        $("btnAddComment").hide();
        new Ajax.Updater(
            "addCommentZone",
            "/media-addcommentform/" + mediaId,
            {
                method: "post",
                parameters: {"from": window.location},
                onComplete: function() {

                    if($("btnCloseNoticeComment")) Actions.attach("btnCloseNoticeComment", "onclick", "Comments.hideForm");

                    $("addCommentZone").hide();
                    new Effect.BlindDown("addCommentZone");
                    Actions.detach("btnAddComment", "onclick");
                    Actions.attach("btnAddComment", "onclick", "Comments.hideForm");

                    if ($("mediaId")) $("commentMediaId").value = $F("mediaId");

                    Actions.attach("videoCommentText", "onkeyup", "Comments.limitLength", "'videoCommentText', 1000");
                    Actions.attach("btnSubmitComment", "onclick", "Comments.send");

                }
            }
        );
        return false;
    },

    hideForm: function() {
        $("btnAddComment").show();
        new Effect.BlindUp("addCommentZone");
        Actions.detach("btnAddComment", "onclick");
        Actions.attach("btnAddComment", "onclick", "Comments.showForm");
        return false;
    },

    send: function() {
        if($F("videoCommentText") != "") {
            new Ajax.Request(
                "/media-procaddcomment/",
                {
                    method: "post",
                    parameters: $("frmAddComment").serialize(true), // penser a appliquer 'utf8_decode' sur les valeurs concernees dans le code php
                    asynchronous: false,
                    onComplete: function(t) {
                        if(t.responseText != '') window.alert(t.responseText);
                        Comments.showList(false, true);
                    }
                }
            );
        } else {
            window.alert("Vous devez saisir un commentaire.");
        }
        return false;
    },

    setAdmin: function() {
        $$(".editable_text").each(function(e) { Actions.attach(e, "onclick", "Comments.edit", "this"); });
        $$(".deletecomment").each(function(e) { Actions.attach(e, "onclick", "Comments.del", "this"); });
    },

    edit: function(e) {
        var text = e.childNodes[0].nodeValue.strip();
        e.setAttribute("onclick", null);
        e.innerHTML = null;
        e.innerHTML = '<textarea cols="50" rows="5" onBlur="Comments.sendEdit(this)">' + text + '</textarea>';
    },

    sendEdit: function(textarea) {
        text = textarea.value;
        container = textarea.parentNode;
        container.innerHTML = Tools.htmlEncode(text);
        container.setAttribute("onclick", "Comments.edit(this)");
        var id = container.id;
        var expression = /change_comment_(\d+)/;
        expression.exec(id);
        id = RegExp.$1;
        new Ajax.Request(
            "/ajax/editcomment/" + id,
            {
                method: "post",
                parameters: { "text": text },
                onComplete: function() { window.alert("Modification effectu\xE9e"); }
            }
        );
    },

    del: function(e) {
        if(window.confirm("Voulez vous effacer ce commentaire ?")) {
            var id    = e.id;
            var expression = /del_comment_(\d+)/;
            expression.exec(id);
            id = RegExp.$1;
            e.parentNode.remove();
            new Ajax.Request(
                "/ajax/hidecomment/" + id,
                {
                    method: "get",
                    onComplete: function() { window.alert("Modification effectu\xE9e"); }
                }
            );
        }
    },

    limitLength: function(textarea, limit) {
        var value  = $(textarea).value;
        if(value.length > limit) $(textarea).value = value.substring(0, limit);
        var strlen = limit - value.length;
        $("commentCharLeft").innerHTML = Math.max(0, strlen) + " caract&egrave;res restant";
    }

};
