<?
////////////////////////////////////////////////////
// Conciliación: gestión de conciliación de activos
// Version: 1.0
//
// Copyright (C) 2014  Eustotem Multimedia, S.L.
//
// License: MIT, see LICENSE
////////////////////////////////////////////////////


include("../conf/preferences.php");
session_name(SYS_APP_SESSNAME);
session_start();
include("../conf/langs/".$langs[$_SESSION['userlang']][0]."/MAIN.php");
include("../conf/connections.php");
include("../phplib/auth.php");
require_once("../phplib/utils.php");
require_once("../core/plugins.php");

if(!isset($_GET['resId'])) header("Location:error.php");
$resId=$_GET['resId'];
$conf=simplexml_load_file("../conf/resources/".$langs[$_SESSION['userlang']][0]."/".$_SESSION['conf'][$resId]->masterview);
$titulogeneral=$conf->title!=NULL ? $conf->title : $_SESSION['conf'][$resId]->title;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><? echo $_SESSION['application']['title'] ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="../resources/themes/default/common.css" rel="stylesheet" type="text/css" media="screen" />
<link href="../resources/themes/default/main.css" rel="stylesheet" type="text/css" media="screen" />
<style type="text/css">
body{background-image:none; overflow-x:hidden;}
.iconomenu{vertical-align:middle !important; border:none;}
.tree,.tree ul, .tree li.last,.jstree-classic.jstree-focused {	background-color:transparent !important;}
#infobar{
	display:none; width:100%; height:150px; overflow-y:scroll; padding:0; border:1px solid #999; background-color:#DDD;
	padding:4px !important;
}
</style>
	<link type="text/css" href="../jslib/jq/ui/css/<? echo UI_THEME ?>/jquery-ui.custom.css" rel="stylesheet" />
	<link type="text/css" href="../jslib/jq/ui/css/<? echo UI_THEME ?>/jquery-ui.ekain.css" rel="stylesheet" />
	<script type="text/javascript" src = "../jslib/jq/jquery-1.7.2.min.js" ></script>
	<script type="text/javascript" src = "../jslib/jq/jquery.blockUI.js" ></script>
	<script type="text/javascript" src = "../jslib/jq/ui/js/jquery-ui-1.8.21.custom.min.js" ></script>
	<script type="text/javascript" src = "../jslib/jq/ui/jquery.ui.selectmenu.js" ></script>
	<link type="text/css" href="../jslib/jq/ui/css/<? echo UI_THEME ?>/jquery.ui.selectmenu.css" rel="stylesheet" />
	<script type="text/javascript" src = "../jslib/jq/fancybox/jquery.fancybox-1.3.4.pack.js" ></script>
	<link rel="stylesheet" href="../jslib/jq/fancybox/jquery.fancybox-1.3.4.css" type="text/css" media="screen" />
	<script type="text/javascript" src="../jslib/jq/tree/jquery.jstree.js"></script>
	<script type="text/javascript" src="../jslib/jq/jqexts.js"></script>

	<script type="text/javascript" src="../jslib/utils.js"></script>
	<script src="../jslib/swfobject2.js" type="text/javascript"></script>
<script type="text/javascript">
var clh=true;			
var selectedNode=null;	
$(document).ready(function () {
	$("#treediv").jstree({
		core: {"animation": 0},
		plugins: ["html_data", "types", "themes", "ui", "crrm", "dnd", "hotkeys"],
		themes: {"theme": "classic", "dots": false, "icons":false},
		dnd: {"copy_modifier":false} ,
		html_data:{ajax:{
			url: "XHR.menu2.php",
			data: function(n){
				var x=n.attr ? n.attr("id") : <? echo $conf->data->root_id ?>;
				if(x!=<? echo $conf->data->root_id ?>){
					temp=x.split("_"); 
					id=temp[1];
				}else{id=x}
				return {parentId:id, action:"getTree", maxDepth:1, resId:"<? echo $resId ?>"};
			}
		}}
	})
	.bind("move_node.jstree", function (e, data) {
		data.rslt.o.each(function (i) {
			var id=$(this).attr("id").replace("phtml_","");
			var padre=data.rslt.cr === -1 ? 0 : data.rslt.np.attr("id").replace("phtml_","");
			var position=data.rslt.cp + i;
			var copy=data.rslt.cy ? 1 : 0;
			var url="template_detail_2.php?_action=move&resId=<? echo $resId ?>&";
			$.ajax({
				type:"POST", data:{id: id, padre: padre, pos: position, action:'move', resId:'<? echo $resId ?>'}, url: "XHR.menu2.php", 
				success: function(html){
					$.jstree._focused().refresh($("#phtml_"+padre));	
				}
			});
		});
	})
	.bind("refresh.jstree", function (e, data) {
	});
	
	$("#treediv").delegate("a","dblclick", function(e) {
		var a=$(this); var tipo=a.attr("rel");
		var idn = a.parent().attr("id").split("_")[1]; 
		if(tipo=="folder"){
			var lid=a.parent().attr("id");
			$("#treediv").jstree("toggle_node","#"+lid);
		}
	});
	$("#treediv").delegate("a","click", function(e) {
		if(clh==false) return;
		clh=false;
		t=setTimeout("clearTime()",1000);
		var a=$(this); var tipo=a.attr("rel");
		var idn = a.parent().attr("id").split("_")[1]; 
		if(tipo=="folder"){
			selectedNode=a.parent();
			undimAll();
			$("#infobar").text(selectedNode.html());
		}
	});
});

function undimAll(){$(".dimmed_50").removeClass("dimmed_50").addClass("dimmed_100");}
function dimAll(){$(".dimmed_100").removeClass("dimmed_100").addClass("dimmed_50");}
function clearTime(){clh=true;}

function newChild(){
	if(selectedNode==null) return;
	var idtemp=selectedNode.attr("id").split("phtml_")[1];
	var cadena="template_detail_2.php?_action=insert&resId=<? echo $resId ?>&<? echo $conf->data->field_dependency ?>="+idtemp;
	parent.mainFrame2.location.href=cadena;
}
function newSibling(){
	if(selectedNode==null) return;
	var idtemp=selectedNode.parent().parent().attr("id").split("phtml_")[1];
	if(idtemp==undefined) idtemp=0;
	var cadena="template_detail_2.php?_action=insert&resId=<? echo $resId ?>&<? echo $conf->data->field_dependency ?>="+idtemp;
	parent.mainFrame2.location.href=cadena;
}
function editNode(){
	if(selectedNode==null) return;
	var padre=selectedNode.parent().parent().attr("id").split("phtml_")[1];
	if(padre==undefined) padre=0;
	var idtemp=selectedNode.attr("id").split("phtml_")[1];
	var cadena="template_detail_2.php?_action=edit&resId=<? echo $resId ?>&id="+idtemp+"&<? echo $conf->data->field_dependency ?>="+padre;
	parent.mainFrame2.location.href=cadena;
}
function nodeDetails(){
	if(selectedNode==null) return;
	$.jstree._focused().refresh(selectedNode.parent());
}
function nodeReport(){
	if(selectedNode==null) return;
	$.jstree._focused().refresh(selectedNode.parent());
}
function removeThisNode(){
	if(selectedNode==null) return;
	var idtemp=selectedNode.attr("id").split("phtml_")[1];
	var idparent=selectedNode.parent().parent().attr("id").split("phtml_")[1];
	var x=confirm("¿Estás seguro de querer borrar este nodo ("+idtemp+", hijo de "+idparent+")?\nSe eliminarán también todos sus descendientes.");
	if(x){
		$.ajax({
			type:"POST", data:{id: idtemp, action:'remove', resId:'<? echo $resId ?>'}, url: "XHR.menu2.php", 
			success: function(html){
				$.jstree._focused().refresh(selectedNode.parent());
				dimAll();
			}
		});
	}
}
function insertCallback(nodo){
	var yuyu=$("#phtml_"+nodo);
	$.jstree._focused().refresh(yuyu);
}

$(document).ready(function(){
	$('#menu2_toolbar').buttonset();
	$('#menu2_toolbar a').click(function(){$(this).blur();});
	$('#btn_newchild').bind('click',newChild);
	$('#btn_newsibling').bind('click',newSibling);
	$('#btn_delete').bind('click',removeThisNode);
	$('#btn_edit').bind('click',editNode);
	$('#btn_details').bind('click',nodeDetails);
	$('#btn_report').bind('click',nodeReport);
	$('#btn_refresh').click(function(){location.reload()});
});
</script>
</head>

<body bgcolor="#EEEEEE" style="position:relative">
<table cellpadding="0" cellspacing="0" width="100%" height="100%" >
<tr>
<td id="panel_central" valign="top" style="width:100%; background-color:#EEEEEE; padding:0;">
	<div id="menu2_toolbar" class="ui-form" >
		<? if($conf->layout->but_newChild=="true"){ ?><a href="#" id="btn_newchild" class="dimmed_50 buttonset" title="Nuevo nodo hijo"><img src="../resources/themes/default/toolbar_btn_newchild.png" /></a><? } ?>
		<? if($conf->layout->but_newSibling=="true"){ ?><a href="#" id="btn_newsibling" class="dimmed_50 buttonset" title="Nuevo nodo hermano"><img src="../resources/themes/default/toolbar_btn_newsibling.png" /></a><? } ?>
		<? if($conf->layout->but_delete=="true"){ ?><a href="#" id="btn_delete" class="dimmed_50 buttonset" title="Eliminar nodo y descendientes"><img src="../resources/themes/default/toolbar_btn_removechild.png" /></a><? } ?>
		<? if($conf->layout->but_edit=="true"){ ?><a href="#" id="btn_edit" class="dimmed_50 buttonset" title="Editar el nodo seleccionado"><img src="../resources/themes/default/toolbar_btn_editnode.png" /></a><? } ?>
		<? if($conf->layout->but_report=="true"){ ?><a href="#" id="btn_report" class="dimmed_50 buttonset " title="Informe del nodo seleccionado y adyacentes"><img src="../resources/themes/default/toolbar_btn_report.png" /></a><? } ?>
		<? if($conf->layout->but_details=="true"){ ?><a href="#" id="btn_details" class="dimmed_50 buttonset " title="Detalles del nodo seleccionado"><img src="../resources/themes/default/toolbar_btn_details.png" /></a><? } ?>
		<a href="#" id="btn_refresh" title="Refrescar el listado" class="buttonset"><img src="../resources/themes/default/toolbar_btn_refresh.png" /></a>
	</div>
	<div id="treediv" style="height:100%"></div>
	<div id="infobar">dfgjb dsfgksdfg kjsd</div>
</td>
</tr>
</table>
<div id="debugger_div"></div>
</body>
</html>
