<?PHP
/**
 * helper script that displays the examples including 
 * the template and PHP source.
 *
 * $Id: _viewExample.php 40 2006-01-18 08:20:02Z gerd $
 *
 * You may pass the following GET parameters to the
 * script:
 *
 * - example => id if the example
 *
 * @author		Stephan Schmidt
 * @author      gERD Schaufelberger
 * @package		patSession
 * @subpackage	Examples
 */

require_once 'index_sections.php';

if( !isset( $_GET['example'] ) ) {
	die( 'No example selected.' );
}

$exampleId = $_GET['example'];
	
$section = getExampleSection( $exampleId );
$example = getExample( $exampleId );
$exampleFile = $exampleId.'.php';

$nav = array(
	'output'	=>	'Output',
	'source'	=>	'PHP Source'
);
?>
<html>
<head>
	<title><?PHP echo $appName; ?> example</title>
	<style>
		@import url( _styles.css );
	</style>
	<script type="text/javascript" language="JavaScript1.2">
	
	var tabs = new Array( <?PHP echo "'".implode( "', '", array_keys( $nav ) )."'";	?> );
	var actTab = false;
	
	function hiTab( tabID )
	{
		document.getElementById( tabID + 'Tab' ).className = 'tabA';
	}
	
	function loTab( tabID )
	{
		if( tabID == actTab )
			return true;
			
		document.getElementById( tabID + 'Tab' ).className = 'tabN';
	}

	function displayTab( tabID ) 
    {
		for( var i = 0; i < tabs.length; i++ ) {
			if( tabID == tabs[i] )
				document.getElementById( tabs[i] ).style.display = 'block';
			else
				document.getElementById( tabs[i] ).style.display = 'none';
		}

		var oldAct = actTab;
				
		actTab = tabID;
		hiTab( actTab );
		
		if( oldAct !== false )
			loTab( oldAct );
	}
	
	</script>
	<style>
		BODY{margin:0px;}
	</style>
</head>
<body onload="displayTab( 'output' );">

<div style="padding:20px;padding-bottom:5px;">
	<div class="head"><b><?PHP echo $appName.' '.$section.' :: '.$example['title']; ?></b></div>
	<div class="abstract"><?PHP echo htmlentities( $example['descr'] ); ?></div>
</div>

<?php buildNavigation(); ?>

<!-- Output -->
<iframe class="exampleContent" style="display:block;" id="output" src="<?PHP echo $exampleFile; ?>"></iframe>

<!-- PHP source -->
<div class="exampleContent" id="source">
	<?PHP
	highlight_file( $exampleFile );
	?>
</div>

</body>
</html>

<?PHP
/**
 * various helper functions follow
 */
 
   /**
	* builds the tab navigation and displays it
	*
	* @access	public
	*/
 	function buildNavigation()
	{
		global $nav;
	
		$html	=	'<table border="0" cellpadding="0" cellspacing="0" class="tabs" width="100%">'
				.	'	<tr>'
				.	'		<td style="padding-left:15px;">&nbsp;</td>';
				
		foreach( $nav as $navId => $navTitle ) {
			$html	.=	'<td class="tabN" id="'.$navId.'Tab" onclick="displayTab( \''.$navId.'\' );" onmouseover="hiTab( \''.$navId.'\' );" onmouseout="loTab( \''.$navId.'\' );">'.$navTitle.'</td>'
					.	'<td>&nbsp;</td>';
		}
		
		$html	.=	'		<td width="100%">&nbsp;</td>'
				.	'	</tr>'
				.	'</table>';
				
		echo $html;
	}

   /**
	* get the example section
	*
	* @access	public
	* @param	string		example id
	* @return	string		section title
	*/
	function getExampleSection( $id )
	{
		global $sections;
		foreach( $sections as $title => $spec ) {
			if( strncmp( $spec['basename'], $id, strlen( $spec['basename'] ) ) === 0 )
				return $title;
		}
		return false;
	}

   /**
	* get the example section
	*
	* @access	public
	* @param	string		example id
	* @return	string		section title
	*/
	function getExample( $id )
	{
		global $sections;
		foreach( $sections as $title => $spec ) {
			if( strncmp( $spec['basename'], $id, strlen( $spec['basename'] ) ) !== 0 )
				continue;
				
			foreach( $spec['pages'] as $name => $data ) {
				if( $id != $spec['basename'].$name )
					continue;
					
				if( !isset( $data['templates'] ) ) {
					$data['templates'] = array(
												$id.'.tmpl'
											);
				}
			
				return $data;
			}
		}
		return false;
	}
?>