<?php
    $this->headScript()->captureStart();
?>
$(function() {

    var urls = {
        "move": "<?php echo $this->url(array('saving' => null, 'action' => 'move', 'controller' => $this->controllerName, 'module' => $this->moduleName, 'id' => '{{id}}'), 'default', false, false); ?>",
        "edit": "<?php echo $this->url(array('saving' => null, 'action' => 'edit', 'controller' => $this->controllerName, 'module' => $this->moduleName, 'id' => '{{id}}'), 'default', false, false); ?>",
        "create": "<?php echo $this->url(array('saving' => null, 'action' => 'create', 'controller' => $this->controllerName, 'module' => $this->moduleName), 'default', false); ?>",
        "delete": "<?php echo $this->url(array('saving' => null, 'action' => 'delete', 'controller' => $this->controllerName, 'module' => $this->moduleName, 'id' => '{{id}}'), 'default', false, false); ?>"
    }

    var page_id_re = new RegExp("page\-(\\d+)", "");

    function get_id(node) {
        return page_id_re.exec($(node).attr('id'))[1];
    }
    $("#tree").CUI('tree', {
        "plugins" : ["html_data","ui","crrm","cookies","dnd"],
        "html_data" : {
            "data" : $('#tree').html(),
            "ajax" : { 
                "url" : "<?php echo $this->url(array('saving' => null, 'type' => 'json'));?>",
                "data" : function (n) {
                        return {
                        
                         id : n.attr ? n.attr("id") : 0 };
                    },
                "success" : function() {}
                }
        }
    });
    
    $("#tree").bind("move_node.jstree", function (e, data) {
            data.rslt.o.each(function (i) {
                var type = (data.rslt.cr === -1 ? '' : data.rslt.p);
                
                $.ajax({
                    url: Mustache.to_html(urls.move, {'id': $(this).attr("id").replace("page-","")}),
                    type: 'post',
                    data: {'type': type, 'referer': data.rslt.r.attr("id").replace("page-","")}
                    })
                });
            })
    
    $("#tree").bind('load_node.jstree', function(){
        $('.field-switcher:not(.field-switcher-converted)').CUI("switcher", {url:"<?php echo $this->url(array('action' => 'switch', 'controller' => $this->controllerName, 'module' => $this->moduleName));?>"});
        
        var u = navigator.userAgent.toLowerCase();
        if(/msie/.test(u)) {
            $("#tree a.help .ui-icon").removeClass("ui-icon");
            
        }
    });
    
    $("#tree").bind("open_node.jstree close_node.jstree", function() {
        var u = navigator.userAgent.toLowerCase();
        if(/msie/.test(u)) {
            $("#tree").animate({width: "99%"});
            $("#tree").animate({width: "100%"});
        }
    });
    
    inContextLanguage();
    
    $(".field-switcher:visible").CUI("switcher", {url:"<?php echo $this->url(array('action' => 'switch', 'controller' => $this->controllerName, 'module' => $this->moduleName));?>"});
});
<?php $this->headScript()->captureEnd(); ?>

<?php $this->placeholder('headling_1')->captureStart(); ?>
<?php if (isset($this->error) || isset($this->infos)) : ?>
    <div class="ui-state-message">
        <?php if (isset($this->errors)):
            $this->errors = (array) $this->errors;
            foreach ($this->errors as $error):
                ?>
                    <p class="ui-state-error"">
                        <?php echo $error;?>
                    </p>
                <?php
            endforeach;
            ?>
        <?php endif;?>
        <?php if (isset($this->infos)):
            $this->infos = (array) $this->infos;

            foreach ($this->infos as $info):
                ?>
                <p class="ui-state-highlight">
                    <?php echo $info;?>
                </p>
                <?php
            endforeach;
        endif;?>
    </div>
<?php endif;?>
<div class="ui-button-big">
    <a href="<?php echo $this->url(array('saving' => null, 'action' => 'create', 'controller' => $this->controllerName, 'module' => $this->moduleName), 'default'); ?>" class="ui-button ui-button-bg-white ui-button-text-red ui-button-text-only">
        <span class="ui-button-text"><?php echo $this->translate('Add a new %s', $this->placeholder('headling_1_add_button')); ?></span>
    </a>
</div>
<h1><?php echo $this->placeholder('headling_1_content'); ?></h1>
<?php $this->placeholder('headling_1')->captureEnd(); ?>

<div class="grid pages">
    <div class="heading">
        <?php echo $this->translate('Title'); ?>
        <div class="cells">
            <?php if (isset($this->publishColumn)): ?>
                <div class="online"><?php echo $this->translate('Is published'); ?></div>
            <?php endif ?>
            <?php if (isset($this->publishDateColumn)): ?>
                <div class="date"><?php echo $this->translate('At'); ?></div>
            <?php endif ?>
            <div class="actions"><?php echo $this->translate('Actions'); ?></div>
            <div class="clear"></div>
        </div>
    </div>
    <div class="jstree" id="tree">
        <?php echo $this->partial('mptt/index.json.phtml', $this);?>
        <?php //$this->isRoot = true; echo $this->partial('mptt/_tree.phtml', $this); ?>
    </div>
</div>