<?php
    $this->headTitle( $this->translate( '#structure management#' ) );
    $this->partialLoop()->setObjectkey( 'entry' );
    
    $this->headLink()->appendStylesheet($this->cdnUrl( '/scripts/jstree/themes/default/style.css'));
    $this->inlineScript()
    	//->appendFile($this->cdnUrl( '/scripts/jstree/jquery.tree.min.js'))
    	//->appendFile($this->cdnUrl( '/scripts/jstree/plugins/jquery.tree.contextmenu.js'))
		
    	->appendFile('/resources/library/scripts/jstree1/jquery.jstree.js')
    	->appendFile('/resources/application/modules/structure/acmin-struct.js')
		
		;
    
?>

<div id="nestedSortable" style="width: 500px">
    <ul class="nestedSortableList"><?php
    $cpt   = 1;
    $depth = 0;
    foreach( $this->structures as $cat ) {
        echo $this->partial(
            'admin-structure/_partials/catList.phtml',
            array( 'entry' => $cat, 'counter' => $cpt, 'depth' => 0 )
        );

        $cpt++;
    }
    ?></ul>
</div>

<?php /*<p style="clear: both;"><a href="#" class="nestedSortable_save"><img
	alt="delete" src="<?= $this->baseUrl()?>/images/icons/16/disk.png" />
Save hierarchy</a> - <a href="#" class="nestedSortable_undo"><img
	alt="delete"
	src="<?= $this->baseUrl()?>/images/icons/16/arrow_rotate_clockwise.png" />
Undo</a></p>

<script type="text/javascript">
	$('.nestedSortable_save').bind('click',function(){
		var nodes = getNSNodes('#nestedSortable');

		$.ajax({
			url: '<?= $this->url(array('action'=>'savepos','format'=>'json')) ?>',
			type : 'POST',
			data: "nodes="+$.toJSON(nodes)
		});

	});

	</script>
*/ ?>
<div class="clearfix"></div>
<p style="clear:both;">
	<a href="#" id="saveJstreePositions" class="btn ui-state-default ui-corner-all saveJstreePositions" style="display:none;"><img
	alt="delete" src="<?= $this->baseUrl()?>/resources/library/images/icons/16/disk.png" />
<?= $this->translate('#__label::save_positions__#') ?></a>
	<a href="#" id="cancelJstreePositions" class="btn ui-state-default ui-corner-all saveJstreePositions" style="display:none;"><span class="ui-icon ui-icon-arrowreturnthick-1-w"></span><?= $this->translate('#__label::cancel__#') ?></a>
	<a href="#" class="btn ui-state-default ui-corner-all" id="jstreeOpenAll"><span class="ui-icon ui-icon-circlesmall-plus"></span><?= $this->translate('#__label::open_all__#') ?></a>  
	<a href="#" class="btn ui-state-default ui-corner-all" id="jstreeCloseAll"><span class="ui-icon ui-icon-circlesmall-minus"></span><?= $this->translate('#__label::close_all__#') ?></a> 
	<a href="<?= $this->url(array('action'=>'save'))?>" class="btn ui-state-default ui-corner-all" ><span class="ui-icon ui-icon-circle-plus"></span><?= $this->translate('#__label::add_node__#') ?></a> 

</p>
<div class="clearfix"></div>

<?php 

$locales = array(''=>'same locale') + Alfred_Hod::locales();

?>
<div id="dialog-form" title="Duplicate node/branch" style="display: none">
	<p class="validateTips">All form fields are required.</p>

	<form>
	<fieldset>
		<p>
			<label for="name">Locale</label>
			<?= $this->formSelect('locale','',array(),$locales); ?>
		</p>
		<p>
			
		<label for="email">Only Node</label>
		<?= $this->formHidden('branch',1); ?>
		<?= $this->formCheckbox('branch',0); ?>
		</p>
		
	</fieldset>
	</form>
</div>


<?php $this->inlineScript()->captureStart(); ?>
$(function(){
	$('#nestedSortable a.action').hide();
	/*
	$("#nestedSortable").jstree({
		"plugins" : [ "html_data" ],
		rules : {
			// only nodes of type root can be top level nodes
			valid_children : [ "root" ]
		},
		types : {
			// all node types inherit the "default" node type
			"default" : {
				deletable : true,
				renameable : true
			},
			"domain" : {
				deletable : false,
				draggable : false,
				renameable : false,
				valid_children : [ "locale" ],
				icon : { 
					image : "<?= $this->cdnUrl('/images/icons/16/world_add.png'); ?>"
				}
			},	
			"locale" : {
				renameable : true,
				deletable : false,
				draggable : false,
				valid_children : [ "default" ]/*,
				icon : { 
					image : "<?= $this->cdnUrl('/images/icons/16/link.png'); ?>"
				}*/
			},	
			"root" : {
				deletable : false,
				draggable : false,
				valid_children : [ "default" ]
				/* icon : { 
					image : "drive.png"
				}*/
			}			
		},
		callback : {
			onmove : function (NODE, REF_NODE, TYPE, TREE_OBJ) { 
				var id = $(NODE).attr('id').split('|');
				var parent = $(REF_NODE).attr('id').split('|');

				$.ajax({
					type	: 'POST',
					url		: '<?= $this->url(array('action'=>'savenodepos','format'=>'json')) ?>',
					dataType: "json",
					//data 	: {'nodes':id[0],'parent':parent[0]},
					data 	: {'node':id[0],'parent':parent[0],'movetype':TYPE},
					success : function(response){
						if(response.error){
							FlashMessenger.error(response.message);
						}else{
							$('#saveJstreePositions').hide();
							FlashMessenger.success(response.message);
						}
					},
					error	: function(response){
						FlashMessenger.error('Ajax request error : try later or contact webmaster');
					}
						 
				});
						
			}
		},
		plugins : {
			contextmenu : {
				items: {
					'rename' : true,
					'create' : false,
					'remove' : false,
					'myedit' : {
						label   : "Edit", 
						action  : function (NODE, TREE_OBJ) {

							var url = '<?= $this->url(array('module'=>'content','controller'=>'admin-structure','action'=>'save'),'content-admin-structure',true) ?>';
							var id = NODE.attr('id').split('|');
							document.location.href = url + '/'+id[0];
							
						},
						separator_before : true,
						icon : "edit"
					},
					'myadd' : {
						label   : "Add Child", 
						action  : function (NODE, TREE_OBJ) {
							var url = '<?= $this->url(array('module'=>'content','controller'=>'admin-structure','action'=>'save'),'content-admin-structure',true) ?>';
							var id = NODE.attr('id').split('|');
							document.location.href = url + '//parent/'+id[0];
						},
						separator_before : true,
						icon : "add"
					},
					'mydelete' : {
						label   : "Delete", 
						action  : function (NODE, TREE_OBJ) {
							var id = NODE.attr('id').split('|')[0];
							var no = NODE;
							var url = '<?= $this->url(array('module'=>'content','controller'=>'admin-structure','action'=>'delete'),'content-admin-structure',true); ?>';
							url += '/'+id+'/format/json';

							$.post(url,{},function(response){
								if(response.error){
									FlashMessenger.error(response.message);
								}else{
									$.tree.focused().remove(no);
									FlashMessenger.success(response.message);
								}
							},'json');
						},
						separator_before : true,
						icon : "delete"
					},
					'myduplicate' : {
						'label' : 'Duplicate',
						'action' : function(NODE,TREE_OBJ){
							var id = NODE.attr('id').split('|')[0];
							
							$( "#dialog-form" ).dialog({
								autoOpen: false,
								height: 300,
								width: 350,
								modal: true,
								buttons: {
									"Duplicate node/branch": function() {
										
										var url = '<?= $this->url(array('module'=>'content','controller'=>'admin-structure','action'=>'duplicate'),'content-admin-structure',true); ?>';
										url =  url + '/' + id + '/locale/' + $('#locale').attr('value') + '/branch/' + $('#branch').attr('value');
										window.location.href=url;
									},
									Cancel: function() {
										$( this ).dialog( "close" );
									}
								}
							});
							
							$( "#dialog-form" ).dialog( "open" );
							
							
								
						}	
					}
				}
			}
		}
	});

	$('a#jstreeOpenAll').click(function(event){
		event.preventDefault();
		$.tree.focused().open_all();
	})
	$('a#jstreeCloseAll').click(function(event){
		event.preventDefault();
		$.tree.focused().close_all();
	})
	$('#saveJstreePositions').click(function(event){
		event.preventDefault();
		
		var nodes = $.tree.focused().get(null,'json');
		$.ajax({
			type	: 'POST',
			url		: '<?= $this->url(array('action'=>'savepos','format'=>'json')) ?>',
			dataType: "json",
			data 	: 'nodes='+$.toJSON(nodes),
			success : function(response){
				if(response.error){
					FlashMessenger.error(response.message);
				}else{
					$('#saveJstreePositions').hide();
					FlashMessenger.success(response.message);
				}
			},
			error	: function(response){
				FlashMessenger.error('Ajax request error : try later or contact webmaster');
			}
				 
		});
			
	});

	$('#cancelJstreePositions').click(function(){window.location.reload();});

	$('a.langedit').click(function(){
		document.location.href = $(this).attr('href');
	});
	*/
});




		

<?php $this->inlineScript()->captureEnd(); ?>
	
<style>
 .tree-default li > a > ins.jstree-icon{
 	//background-image: url(<?= $this->cdnUrl('/images/icons/16/page.png'); ?>);
 }
 .tree-default li[lang=fr] > a > ins.jstree-icon{
 	background-image: url(<?= $this->cdnUrl('/images/icons/16/flags/fr.gif'); ?>);
 }
 .tree-default li[lang=en] > a > ins.jstree-icon{
 	background-image: url(<?= $this->cdnUrl('/images/icons/16/flags/en.gif'); ?>);
 }
 .tree-default li[lang=de] > a > ins.jstree-icon{
 	background-image: url(<?= $this->cdnUrl('/images/icons/16/flags/de.gif'); ?>);
 }
 .tree-default li[lang=nl] > a > ins.jstree-icon{
 	background-image: url(<?= $this->cdnUrl('/images/icons/16/flags/nl.gif'); ?>);
 }
 .tree-default li[lang=it] > a > ins.jstree-icon{
 	background-image: url(<?= $this->cdnUrl('/images/icons/16/flags/it.gif'); ?>);
 }
 .tree-default li[lang=es] > a > ins.jstree-icon{
 	background-image: url(<?= $this->cdnUrl('/images/icons/16/flags/es.gif'); ?>);
 }
 
</style>