<?php
/**
 * @package     FILEman
 * @copyright   Copyright (C) 2011 Timble CVBA. (http://www.timble.net)
 * @license     GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 * @link        http://www.joomlatools.com
 */
defined('KOOWA') or die;
$query = url()->getQuery(true);
$row   = $query['row'];
?>

<?= helper('ui.load') ?>

<?= import('com:files.attachments.manage.html', array('debug' => (bool) JFactory::getApplication()->getCfg('debug'))) ?>

<script>
    kQuery(function($)
    {
        var container = $('.mce-in.mce-panel', window.parent.document);
        if (container.length) {
            container.addClass('k-joomla-modal-override-j3');
        }

        container = $('.modal-dialog .modal-content', window.parent.document);
        if (container.length) {
            container.addClass('k-joomla-modal-override-j4');
        }

        Files.app.options.router.defaults.option = 'com_fileman';

        Files.app.grid.addEvent('afterAttachAttachment', function(data)
        {
            $.each(['form[name="adminForm"]', 'form.k-js-form-controller'], function(idx, form) {
                form = $(form, parent.document);

                if (form.length && !form.find('input[name="fileman_attachment_row"]').length)
                {
                    // Append the the row value to update the row columns when attaching files to new items.
                    form.append($('<input type="hidden" name="fileman_attachment_row" value=<?= json_encode($row) ?> />'));
                    return false;
                }
            });
        });

        Files.app.grid.addEvent('onClickAttachment', function(e)
        {
            var selected = that.grid.selected;

            if (selected && typeof that.grid.nodes[selected] !== 'undefined')
            {
                var file = that.grid.nodes[selected].file;

                if (file.type == 'image' && !file.thumbnail) {
                    this.preview.getElement('img').set('src', this.createRoute(
                        {
                            view: 'file',
                            format: 'html',
                            name: file.name,
                            routed: 1
                        }));
                }
            }
        }.bind(Files.app));
    });
</script>
