// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults


function add_close_icon(node) {
    var box = remix_get_box(node);
    var img = remix_load_image("cross.png");
    img.style.cssFloat = "right";
    img.style.cursor = "pointer";
    img.style.border = "1px black outset";
    img.style.backgroundColor = "#fafafa";
    img.onclick = function() {
        new Effect.Fade(node, {durantion: 0.5});
        /* Element.remove(node); */
    };
    node.insertBefore(img, node.firstChild);
}

Event.observe(window, "load", function() {
    /* Buscamos los elementos con la clase can_be_closed. A estos
     * le aņadiremos una marca para que se puedan borrar
     */
    $$(".can_be_closed").each(function(node) { add_close_icon(node); });

    /* Buscamos los mensajes sobre separar con comas para avisar
       cuando se llegue a widget de etiquetas */
    setTimeout(function() {
        var div_notice_tag_separator = $("notice_tag_separator");
        if(div_notice_tag_separator) {
            var re = /^\w+_etiquetas$/;
            var proc = function() { new Effect.Pulsate(this.node, {duration: 1}) }.bind({node: div_notice_tag_separator});

            for(var widget in RWidgets) {
                if(widget.match(re)) {
                    RWidgets[widget].add_event("focus", proc);
                }
            }
        }
    }, 1000);

});


/* Ajustar una imagen al ancho de su contenedor */
function resize_img_to_fit(img, container)  {
    var dims = Element.getDimensions(container);

    if(img.width > dims.width)
        img.width = dims.width;
}
