<?
	//$x = get_data('select guid, Naziv from library order by Naziv', 1);
?>

	<script type="text/javascript" src="http://www.google.com/jsapi"></script>
	<script type="text/javascript">
		google.load('visualization', '1', {packages: ['orgchart']});
	</script>
	<script type="text/javascript">
		var viz;
		
		var a_data = new Array();
<?
		$l_cnt = 0;
		//for($i = 0; $i < sizeof($x['guid']); $i++){
		for($i = 0; $i < 1; $i++){
			$y = get_data('select B.guid, B.FolderName as Naziv, B.parent_guid
				from folder A RIGHT outer join folder B On A.guid = B.parent_guid
				order by A.FolderName, B.FolderName ', 1);
				
			for($j = 0; $j < sizeof($y['guid']); $j++){
?>
				a_data[<?=$l_cnt++?>] = new Array({v:'<?=$y['guid'][$j]?>', f:'<?=$y['Naziv'][$j]?>'}, '<?=$y['parent_guid'][$j]?>', '<?=$y['Naziv'][$j]?>');
<?
			}
		}
?>
		
		function drawVisualization(a_data_a){
			var data = new google.visualization.DataTable();
			data.addColumn('string', 'Name');
			data.addColumn('string', 'Manager');
			data.addColumn('string', 'ToolTip');
			data.addRows(a_data_a);
			
			// Create and draw the visualization.
			viz = new google.visualization.OrgChart(document.getElementById('visualization'));
			viz.draw(data, {allowHtml: true,nodeClass:'nodeStyle',selectedNodeClass:'sNodeStyle',size:'medium'});
			
			//alert(viz);
			//alert(a_data_a.length);
		
			google.visualization.events.addListener(viz, 'select', function() {
				// grab a few details before redirecting
				var selection = viz.getSelection();
				//alert(selection[0].row);
				if(selection == ''){
					close_layers();
					
					var la_query = document.location.href.split('#');
					document.location = la_query[0] + '#';
				}else{
					var row1 = selection[0].row;
					var la_query = document.location.href.split('#');
					//la_query[0] = la_query[0].replace('&edited=1', '').replace('&added=1', '').replace('&error=1', '');
					document.location = la_query[0] + '#3/2/' + data.getValue(row1, 0);
					
					//display_layer(data.getValue(row1, 0), data.getValue(row1, 2));
					//display_layer(data.getValue(row1, 0));
				}
			});
		}
		
		function close_layers(){
			document.getElementById('visBottomFolder').style.display = 'none';
			document.getElementById('mapSpan').innerHTML = '';
			document.getElementById('menuButtons').innerHTML = '<a href="#" onclick="folder_get_add(\'\'); display_layer(\'\'); return false;" title="Dodaj mapu nulte razine"><img src="' + g_path_sl_res + 'images/b-add_map_first.gif" border="0"></a>';
			document.getElementById('menuAction').innerHTML = 'Akcija: "Dodaj novu mapu nulte razine"';
		}
		
		/*
		function close_layers(){
			document.getElementById('visOver').style.display = 'none';
			document.getElementById('visBottomFolder').style.display = 'none';
			document.getElementById('visualization').innerHTML = '';
			drawVisualization(a_data);
		}
		*/
		
		function display_layer(value){
		
			//document.write('<?=$g_path_sl?>res/aj/folder_get.php?value=' + value);
			//return;
		
			//makerequest('<?=$g_path_sl?>res/aj/folder_get.php?value=' + value, 'visBottomFolder');
			makerequest_4('<?=$g_path_sl?>res/aj/folder_get.php?value=' + value, 'visBottomFolder', 'mapSpan', 'menuAction', 'menuButtons');
			/*document.getElementById('visOver').innerHTML = '<a href="#" onclick="close_layers(); return false"><b>X</b></a>';
			document.getElementById('visOver').style.display = 'block';*/
			document.getElementById('visBottomFolder').style.display = 'block';
			//document.getElementById('menuButtons').style.display = 'block';
			//document.getElementById('menuAction').style.display = 'block';
			//document.getElementById('mapSpan').innerHTML = ' :: Mapa "' + value2 + '"';
			
			
			for(i = 0; i < a_data.length; i++){
				if(a_data[i][0]['v'] == value){
					viz.setSelection([{'row': i}]);
					break;
				}
			}
				
			/*
			var selection = viz.getSelection();
			if(selection == ''){
			}else{
				//var la_href = document.location.href.split('#');
				//var la_hash = window.location.hash.split('/');
				//document.location = la_href[0] + la_hash[0] + '/' + la_hash[1] + '/' + value;
			}
			*/
			//viz.setSelection(viz.getSelection());
		}
		
		google.setOnLoadCallback(function(){
			drawVisualization(a_data);
		});
		
	</script>
