/*---------------------------------------------------------------------------
 * @Plugin Name: aceBlogExtender
 * @Plugin Id: aceblogextender
 * @Plugin URI: 
 * @Description: Blog Extender for LiveStreet/ACE
 * @Version: 1.4.541
 * @Author: Vadim Shemarov (aka aVadim)
 * @Author URI: 
 * @LiveStreet Version: 0.4.2
 * @File Name: mtopics.js
 * @License: 
 *----------------------------------------------------------------------------
 */

document.addEvent('domready', function() {
    var mtopicAttacFiles = $("mtopic_attach_files");
    if (!mtopicAttacFiles) return;
    var nAttachNum = 1;

    var addButton = function(n) {
        if (n>1) {
            var div = new Element("div", {id: "mtopic_attach_file_" + n});
            var button = new Element("input", {
                id: "mtopic_attach" + n,
                name: "mtopic_attach" + n,
                type: "file"
            });
            var span = new Element("span", {
                id: "mtopic_attach_filename_" + n + "_cmd",
                styles: {display: "none"}
            });
            span.set("html", "\n" +
                "<img src=\"" + DIR_STATIC_SKIN + "/images/delete.gif\" alt=\"delete\" onclick=\"mtopic.newDelete(" + n + ")\" />\n " +
                "<img src=\"" + DIR_STATIC_SKIN + "/images/edit.gif\" alt=\"edit\" onclick=\"mtopic.newEdit(" + n + ")\" />\n " +
                " &nbsp;\n ");
            /*
            span.grab(new Element("img", {
                src: DIR_STATIC_SKIN + "/images/delete.gif",
                alt: "delete",
                onclick: "mtopic.newDelete(" + n + ")"
            }));
            span.grab(new Element("img", {
                src: DIR_STATIC_SKIN + "/images/edit.gif",
                alt: "edit",
                onclick: "mtopic.newEdit(" + n + ")"
            }));
            */
            div.grab(button);
            div.grab(span);
            div.grab(new Element("input", {
                type: "text",
                id: "mtopic_attach_filename_" + n + "_txt",
                name: "mtopic_attach_filename_" + n + "_txt",
                styles: {display: "none"}
            }));
            div.grab(new Element("span", {id: "mtopic_attach_filename_" + n}));
            //div.grab(new Element("br"));
            mtopicAttacFiles.grab(div);
        }
        $("mtopic_attach" + n).addEvent('change', function() {changeAttach(""+n)});
    }

    var addAttach = function(n) {
        $("mtopic_attach"+n).fireEvent("click");
    }

    var changeAttach = function(n) {
        var file_name = $("mtopic_attach" + n).get("value");
        $("mtopic_attach_filename_" + n).set("text", file_name);
        if (file_name) {
            $("mtopic_attach_filename_" + n + "_cmd").set("styles", {display: ""});
            addButton(nAttachNum++);
        }
    }

    addButton(nAttachNum++);
});

var mtopic = {
    newDelete: function(nFile) {
        $("mtopic_attach_file_" + nFile).destroy();
    },
    newEdit: function(nFile) {
        $("mtopic_attach_filename_" + nFile + "_txt").set("styles", {display: ""});
    }
};
