<?php

define('RENDER_MODE', 'catalyz');

$properties = array();
$properties['page'] = $page;
$properties['page'] = $page;
$node = $page->getNode();
$properties['node'] = $node;
if (!isset($culture)) {
	$culture = sfConfig::get('translations_default_language');
}
$properties['culture'] = $culture;
$properties['ContentTree'] = $ContentTree;
$ContentTreeNode = $ContentTree->getCurrentNode();

ob_start();


?>
<div id="catalyz-content">

<?php

include_partial('catalyz/header');
?>

<div id="catalyz-content-internal">

<?php

function generateTree($nodes, $node_classes)
{
    if (!is_array($nodes)) {
        $node_classes = generateTree(array($nodes), $node_classes);
    } elseif (count($nodes) > 0) {
		echo '<ul>' ;
        foreach($nodes as $node) {
        	$node_classes[$node->getNodeClass()] = true;
			printf('<li id="node-%d" rel="%s">', $node->getId(), $node->getNodeClass());
        	printf('<a href="#">%s</a>', $node->getTitle());
        	$node_classes = generateTree($node->children, $node_classes);
        	printf('</li>');
        }
        echo '</ul>' ;
    }
	return $node_classes;
}

echo '<div id="tree">';
$node_classes = array();
$node_classes = generateTree($ContentTree->getRoot(), $node_classes);
echo '</div>';

$drag_rules = array();
foreach ($node_classes as $node_class => $true){
	$class = sprintf('Node%s', $node_class);
	$object = new $class();
	foreach ($object->getValidSubClasses() as $sub_class){
		$drag_rules[] = sprintf('"%s inside %s"', $sub_class, $node_class);
	}
}

use_javascript('catalyz/jquery-1.3.2.min.js');
use_javascript('catalyz/tree/jquery.cookie.js');
use_javascript('catalyz/tree/jquery.listen.js');
use_javascript('catalyz/tree/css.js');
use_javascript('catalyz/tree/tree_component.js');
use_stylesheet('catalyz/tree_component.css');

?>

<script type="text/javascript" language="JavaScript">
/* <![CDATA[ */

$(function () {
	$("#tree").tree({
		rules: {
	        multiple    : false,    // FALSE | CTRL | ON - multiple selection off/ with or without holding Ctrl
	        metadata    : false,    // FALSE or STRING - attribute name (use metadata plugin)
	        type_attr   : "rel",    // STRING attribute name (where is the type stored if no metadata)
	        multitree   : false,    // BOOL - is drag n drop between trees allowed
	        createat    : "bottom", // STRING (top or bottom) new nodes get inserted at top or bottom
	        use_inline  : false,    // CHECK FOR INLINE RULES - REQUIRES METADATA
	        clickable   : "all",    // which node types can the user select | default - all
	        renameable  : "none",    // which node types can the user select | default - all
	        deletable   : "none",    // which node types can the user delete | default - all
	        creatable   : "none",    // which node types can the user create in | default - all
	        draggable   : "all",   // which node types can the user move | default - none | "all"
	        dragrules   : [ <?php echo implode(', ', $drag_rules) ?> ],    // what move operations between nodes are allowed | default - none | "all"
	        drag_copy   : false,    // FALSE | CTRL | ON - drag to copy off/ with or without holding Ctrl
	        droppable   : []
	    },
		callback: {
	        onmove      : function(NODE,REF_NODE,TYPE,TREE_OBJ) { reorganizeNode(NODE.id, REF_NODE.id, TYPE); },    // move completed (TYPE is BELOW|ABOVE|INSIDE)
	    }
	});
	$.tree_reference('tree').select_branch($('#node-<?php echo $sf_request->getParameter("node_id", 1) ?>'));
});

function reorganizeNode(html_node_id, html_node_sibling_id, position){
	$.ajax({
		type: "POST",
		url: "<?php echo url_for('@node-reorganize-update') ?>",
		data: "node_id="+html_node_id+"&sibling_id="+html_node_sibling_id+"&position="+position,
		success: function(msg){}
	});
}

/* ]]> */
</script>

</div>
</div><?php

$content = ob_get_contents();

ob_end_clean();

$layout = LayoutManager::instance()->getCatalyzTemplate($ContentTree->getCurrentNode()->getCurrentLayoutNameForAction());
$properties['layout_family'] = $layout[0];
$properties['layout_name'] = $layout[1];
$properties['content'] = $content;

include_component($properties['layout_family'], $properties['layout_name'], $properties);

?>