<?
	$x = get_data("select B.guid, B.Naziv, B.Sifra, B.parent_guid
		from organizacijska_shema_drustva A RIGHT outer join organizacijska_shema_drustva B On A.guid = B.parent_guid
		order by A.sifra, B.sifra", 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();
<?
			for($i = 0; $i < sizeof($x['guid']); $i++){
?>
				a_data[<?=$i?>] = new Array({v:'<?=$x['guid'][$i]?>', f:'<?=$x['Naziv'][$i]?>'}, '<?=$x['parent_guid'][$i]?>', '<?=$x['Naziv'][$i]?>');
<?
			}
?>
		
		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);
			
			//data.addRows([
<?
			//for($i = 0; $i < sizeof($x['guid']); $i++){
?>
				//[{v:'<?=$x['guid'][$i]?>', f:'<?=$x['Naziv'][$i]?>'}, '<?=$x['parent_guid'][$i]?>', '<?=$x['Naziv'][$i]?>'],
<?
			//}
?>
				<?/*?>
				[{v:'Mike', f:'Mike<div>President</div>'}, '', 'The President'],
				[{v:'Jim', f:'Jimmy<br><a href="#" onclick="javascript:return false;" style="display:block">ADD +</a>'}, 'Mike', 'VP'],
				['Alice', 'Mike', ''],
				['Alice2', 'Mike', ''],
				['Alice3', 'Mike', ''],
				['Alice4', 'Mike', ''],
				['Alice5', 'Mike', ''],
				['Alice6', 'Mike', ''],
				['Alice7', 'Mike', ''],
				['Bob', 'Jim', 'Bob Sponge'],
				['Carol', 'Bob', ''],
				['Carol2', 'Bob', ''],
				['Carol3', 'Carol2', ''],
				['Carol4', 'Carol2', ''],
				['Carol5', 'Bob', ''],
				['Carol6', 'Carol2', ''],
				['Carol7', 'Carol4', '']
				<?*/?>
			//]);
				
			// Create and draw the visualization.
			viz = new google.visualization.OrgChart(document.getElementById('visualization'));
			viz.draw(data, {allowHtml: true,nodeClass:'nodeStyle',selectedNodeClass:'sNodeStyle',size:'medium'});
			//viz.collapse(3, true);
			//viz.collapse(1, false);
			//a_sel = viz.getSelection();
			//alert(a_sel);
			//a_sel = new Array(0);
			//viz.setSelection(a_sel);
		
			google.visualization.events.addListener(viz, 'select', function() {
				// grab a few details before redirecting
				var selection = viz.getSelection();

				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('#');
					document.location = la_query[0] + '#1/1/' + data.getValue(row1, 0);
					
					//display_layer(data.getValue(row1, 0));
				}

				
				//var col = selection[0].column;
				//var year = data.getValue(row, 0);
				//location.href = 'http://www.google.com?row=' + row + '&col=' + col + '&year=' + title;
				//display_layer(row1);
				//alert(data.getValue(row1, 0));
			});
	
		}
		
		function close_layers(){
			document.getElementById('visBottomFolder').style.display = 'none';
			document.getElementById('mapSpan').innerHTML = '';
			document.getElementById('menuButtons').innerHTML = '';
			document.getElementById('menuAction').innerHTML = '';
			drawVisualization(a_data);
		}
		
		function display_layer(value){
			//document.write('<?=$g_path_sl?>res/aj/get_osd.php?value=' + value);
			makerequest_4('<?=$g_path_sl?>res/aj/osd_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';
			
			for(i = 0; i < a_data.length; i++){
				if(a_data[i][0]['v'] == value){
					viz.setSelection([{'row': i}]);
					break;
				}
			}
		}
		
		//google.setOnLoadCallback(drawVisualization(a_data));
		
		google.setOnLoadCallback(function(){
			drawVisualization(a_data);
		});
		
	</script>
