<?php
/**
 * Artio MTurbo
 *
 * @category    design
 * @package     default_default
 * @copyright   Copyright (c) 2013 Artio s.r.o (http://www.artio.net/)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>

<div class="entry-edit">
    <?php echo $this->getFormHtml() ?>

    <div class="entry-edit-head">
        <h4 class="icon-head head-edit-form fieldset-legend"><?php echo $this->__("Blocks") ?></h4>
    </div>
    <fieldset id="dynamic_fieldset" class="grid">
        <legend><?php echo $this->__("Blocks") ?></legend>
        <table class="data" cellspacing="0">
            <thead>
                <tr class="headings">
                    <th><?php echo $this->__("Block") ?></th>
                    <th style="width:100px"><?php echo $this->__("Type") ?></th>
                    <th style="width:100px"><?php echo $this->__("Action") ?></th>
                </tr>
            </thead>
            <tbody id="blocks_container">
<?php function showContentsTemplate($self, $_i=0) { ?>
					 <?php $block = $self->getFormUserBlock($_i); ?>
					 <?php $name  = ($block && isset($block['name'])) ? $block['name'] : '' ?>
					 <?php $type  = ($block && isset($block['type'])) ? $block['type'] : 'ajax' ?>
                     <td style="margin-bottom:5px;">
                        <select name="blocks[name][]"">
                        	<?php foreach ($self->getBlocks() as $handle => $blocks): ?>
                        		<optgroup label="<?php echo $handle ?>">
                        			<?php foreach ($blocks as $key => $value): ?>
                            			<option value="<?php echo $key ?>" <?php if ($name==$key) echo 'selected="selected"' ?> ><?php echo $self->escapeHtml($value) ?></option>
                            		<?php endforeach ?>
                        		</optgroup>
                        	<?php endforeach ?>
                        </select>
                     </td>
                     <td style="margin-bottom:5px;">
                        <select name="blocks[type][]" style="width:100px">
                            <?php foreach ($self->getBlockTypes() as $key => $value): ?>
                            	<option value="<?php echo $key ?>" <?php if ($type==$key) echo 'selected="selected"' ?> ><?php echo $self->escapeHtml($value) ?></option>
                            <?php endforeach ?>
                        </select>
                     </td>
                     <td style="margin-bottom:5px;width:100px">
                        <?php echo $self->getRemoveRowButtonHtml('tr') ?>
                     </td>
<?php } ?>
				<tr id="blocks_template">
					<?php showContentsTemplate($this, 0) ?>
				</tr>
				<?php for ($i=1; $i<$this->getFormUserBlockCount(); $i++): ?>
					<tr>
						<?php showContentsTemplate($this, $i) ?>
					</tr>
				<?php endfor ?>
            </tbody>
            <tfoot>
                <tr>
                    <td class="a-left" colspan="6"><?php echo $this->getAddRowButtonHtml('blocks_container', 'blocks_template', $this->__('Add Block')) ?></td>
                </tr>
            </tfoot>
        </table>
    </fieldset>
</div>
<script type="text/javascript">
function addRow(container, template)
{
    Element.insert($(container), {bottom: '<tr>' + $(template).innerHTML + '</tr>'});
}

function removeRow(button, selector)
{
    Element.remove($(button).up(selector));
}
</script>