<?php

session_start();

include_once("config.php");
include_once("sem/sparql.php");


$qname = $_REQUEST["qname"];

$query = "select ?t ?l ?dt
		  where { {<$qname> rdf:type ?t .
		  		  	?t rdfs:label ?l .
		  			OPTIONAL {?t iv:direct-supertype ?dt}}
		  		UNION
		  		{<$qname> rdfs:subClassOf ?t .
		  		  	?t rdfs:label ?l .
		  			OPTIONAL {?t iv:direct-supertype ?dt}}
		  }";
$query = buildBasicSparql($query);
$json = executeSparqlOnService($query, "ont");
$types = array();
$map = array();

foreach ($json[results][bindings] as $value){
	if (!isset($map[ $value[t][value] ])){
		$map[$value[t][value]] = array('expand' => true, 'icon' => 'concept.png', 'title' =>  $value[l][value], 'key' => $value[t][value], 'isFolder' => true, 'children' => array(), 
				'childrenStr' => array(), 'types' => array());
	}
	if (!is_null($value[dt])){
		$map[$value[t][value]][types][] = $value[dt][value];
	}
}

foreach ($map as $i => $value) {
	for ($j = 0; $j < count($value[types]); $j = $j + 1){
		$map[$value[types][$j]][childrenStr][] = $value[key];
	}
}

foreach ($map as $i => $value) {
	if (count($value[types]) == 0){
		$types[] = buildNode($value);
	}
}

function buildNode($node){
	global $map;
	//echo $node[title] . "<br>";
	//if (count($node[childrenStr]) == 0)
	//	echo "DONE<br>";
	for ($j = 0; $j < count($node[childrenStr]); $j = $j + 1){
	//	echo "-->" . $node[childrenStr][$j] . " | " . $map[$node[childrenStr][$j]][title] . "<br>";
		$node[children][] = buildNode($map[$node[childrenStr][$j]]);
	}
	return $node;
}
	
//print_r($map);

?>

<!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>
<link rel="stylesheet" href="js/jquery-ui/development-bundle/themes/base/jquery.ui.all.css">
<link rel="stylesheet" href="css/jquery.ui.autocomplete.css">
<script src="js/jquery/jquery.js" type="text/javascript"></script>
<script src="js/jquery/jquery-ui.custom.js" type="text/javascript"></script>
<script src="js/jquery/jquery.cookie.js" type="text/javascript"></script>
<script type="text/javascript" src="js/jquery-ui/js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="js/jquery-ui/js/jquery-ui-1.8.17.custom.min.js"></script>
<script src="js/jquery-ui/development-bundle/ui/jquery.ui.tabs.js"></script>
<script src="js/jquery-ui/development-bundle/ui/jquery.effects.core.js"></script>
<script src="js/jquery-ui/development-bundle/ui/jquery.effects.blind.js"></script>
<script src="js/jquery-ui/development-bundle/ui/jquery.ui.core.js"></script>
<script src="js/jquery-ui/development-bundle/ui/jquery.ui.widget.js"></script>
<script src="js/jquery-ui/development-bundle/ui/jquery.ui.position.js"></script>
<script src="js/jquery-ui/development-bundle/ui/jquery.ui.autocomplete.js"></script>
<link href="style.css" rel="stylesheet" type="text/css" />
<!--<script type="text/javascript" src="js/cufon-yui.js"></script>
<script type="text/javascript" src="js/arial.js"></script>
<script type="text/javascript" src="js/cuf_run.js"></script>-->
<link href="js/dynatree/skin/ui.dynatree.css" rel="stylesheet" type="text/css" id="skinSheet">
<script src="js/dynatree/jquery.dynatree.js" type="text/javascript"></script>

<script type="text/javascript">
var treeData = <?php echo json_encode($types) ?>;

 $(function(){
 	$("#typeTree").dynatree({
      //checkbox: true,
      selectMode: 3,
      children: treeData,
      onSelect: function(select, node) {
      	/*var leaves = node.getLeaves();
      	var first = false;
      	for(var i = 0 ; i < leaves.length ; i++){
      		var tmp = "#" + leaves[i].data.key;
      		var folderNode = leaves[i].getFirstFolderParent();
      		if(first == false && select && $(tmp).offset() != null){
      			$("#tabs-1").scrollTop($(tmp).offset().top);
      			first = true;
      		}
      		//alert(folderNode.data.key);
      		if(select){
      			$(tmp).addClass(folderNode.data.key);
      		}
      		else{
      			$(tmp).removeClass(folderNode.data.key);
      		}
      	}*/
    },
    onDblClick: function(node, event) {
    	node.toggleSelect();
    },
    onClick: function(node, event) {
    	//if(!node.data.isFolder)
    	//	showElement(node.data.key);
    },
    onKeydown: function(node, event) {
		if( event.which == 32 ) {
          //node.toggleSelect();
          return false;
        }
    }
    });
  });

</script>

</head>
<body>
<div id="typeTree" style="border: 0px 0px 0px 0px;"></div>
</body>