<div class="crumb">
    <ul>
        <li><a href="update/index/">top</a></li>
    </ul>
</div>

<table id="updates" class="data">
    <tr>
        <th>Version</th>
        <th>Title</th>
        <th>Date</th>
        <th>&nbsp;</th>
    </tr>
    <?php foreach ($package as $p): ?>
        <tr class="<?php echo ($p->version > $current) ? 'highlighted' : 'disabled'; ?>" rel="<?php echo htmlspecialchars($p->version); ?>">
            <td><?php echo htmlspecialchars($p->version); ?></td>
            <td><?php echo htmlspecialchars($p->title); ?></td>
            <td><?php echo htmlspecialchars($p->created); ?></td>
            <td>
                <?php if ($p->version > $current) : ?>
                    <a href="javascript:" class="install" title="<?php echo htmlspecialchars($p->version); ?>">Install</a>
                    <script type="text/javascript">
                        $('a.install[title="<?php echo htmlspecialchars($p->version); ?>"]')
                            .data('version', '<?php echo htmlspecialchars($p->version); ?>')
                            .data('title', '<?php echo htmlspecialchars($p->title); ?>')
                            .data('description', '<?php echo htmlspecialchars($p->description); ?>');
                    </script>
                <?php else : ?>
                    <span class="done">Done</span>
                <?php endif; ?>
            </td>
        </tr>
    <?php endforeach; ?>
</table>

<div id="install-template" style="display:none;">
    <div class="install-prompt">
        <h1>Installing version <span class="version"></span></h1>
        <h2></h2>
        <textarea></textarea>
        <ul>
            <li><a href="javascript:" class="confirm">Install</a></li>
            <li><a href="javascript:" class="cancel">Cancel</a></li>
        </ul>
    </div>
</div>

<script type="text/javascript">
    $(document).ready(function() {
        $('table#updates a.install').click(function() {
            var install = $($('#install-template').html());
            install.find('span.version').text($(this).data('version'));
            install.find('h2').text($(this).data('title'));
            install.find('textarea').text($(this).data('description'));
            install.find('a.confirm').attr('rel', $(this).data('version'));
            $.facebox(install);
            return false;
        });
        
        $('.install-prompt a.confirm').live('click', function() {
            var log = $(this).closest('.install-prompt').find('textarea');
            
            $.ajax({
                url: 'update/install',
                type: 'post',
                data: { version: $(this).attr('rel') },
                dataType: 'json',
                success: function(response){
                    if (typeof response.error != 'undefined') {
                        log.text(response.error);
                    } else {
                        log.text(response.log);
                        document.location.reload(true);
                    }
                }
            });
            
            return false;
        });
        
        $('.install-prompt a.cancel').live('click', function() {
            $.facebox.close();
            return false;
        });
    });
</script>