<?php 
/**
 * Si el contenido no está generado entonces lo generamos automáticamente a partir
 * de los ficheros del directorio text
 */

include('init.php');

if(is_dir(CODE)) {
	$temp = scandir(CODE);
	if(count($temp) <= 2) { // Directorio vacío
		
		$dirlist = scandir(TEXTO);
		
		$numfiles = 0;
		foreach($dirlist as $file) { // Contamos el número total de páginas
				
			if(strncmp($file, '.', 1) != 0 && strcmp($file, 'index.txt') != 0 &&
			strncmp($file, 'bloque', 6) != 0 && strcmp($file, 'titulo.txt') != 0) {
				$numfiles++;
			}
		}
	
		$page = 0;
		$pagename = '';
		foreach($dirlist as $file) { // Para cada fichero de página creamos la página
			
			if(strncmp($file, '.', 1) != 0 && strcmp($file, 'index.txt') != 0 &&
				strncmp($file, 'bloque', 6) != 0 && strcmp($file, 'titulo.txt') != 0) {
				
				$page++;
				
				if(!is_array($pagename)) {
					$pagename = preg_split("/[\d]+/", $file);
				}
					
				$name = explode(".", $file);
				$fd = fopen(CODE.'/'.$pagename[0].$page.'.php', 'w');
				$text = file(TEXTO.'/'.$file);
				$content = generate_page($text, $page, $pagename, $numfiles);
				
				fwrite($fd, $content);
				
				fclose($fd);
			}
		}
	}
}

?>

<!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>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<meta name="Author" lang="es" content="Alicia García Holgado" />
		<meta name="keywords" lang="es" content="sacyl, teleoftalmología, curso" />
		<link href="css/main.css" rel="stylesheet" type="text/css" />
		<link href="css/mlightbox.css" rel="stylesheet" type="text/css" />
		<script src="js/prototype.js" type="text/javascript"></script>
		<script src="js/mf_lightbox.js" type="text/javascript"></script>
		<script type="text/javascript">
			Lightbox.init('img/close.gif');
		</script>
		
        <title><?php echo file_get_contents('text/titulo.txt') ?></title>
    </head>

    <body id="main">
        <div id="page">
        	<div id="header">
        		
        	</div> <!-- Fin header -->
			
			<div id="title">
				<h2><a href="#"><?php echo file_get_contents('text/titulo.txt') ?></a></h2>
				<a class="grial" href="http://grial.usal.es"></a>
			</div> <!-- Fin modulo -->

			
			<div id="contentblock">
				<div id="content1">
					<?php $blocks = countblocks();
					for($j=1; $j<=$blocks-1; $j++) { ?>
						<div class="bloque">
							<?php // Cargamos los objetivos
								$temp = file('text/bloque'.$j.'.txt');
							?>
							<div class="headerBloque"><h3><?php echo $temp[0]; ?></h3></div>
							<div class="contentBloque">
								<?php
									for($i=1; $i<count($temp); $i++) echo $temp[$i];
								?>
							</div>
						</div>
					<?php } ?>
				</div> <!-- Fin contentleft -->
				
				<div id="content2">
					<div class="bloque2">
						<div class="headerR">Índice</div>
						<div class="contentR">
							<ul>
							<?php 
								$temp = file('text/index.txt');
								$num = 1;
								foreach($temp as $line) {
									echo '<li class="element">
											<div>
												<span>'.$line.'</span>'.
											'</div></li>';
								}
							?>
							</ul>
						</div>
					</div>
					
					<div class="bloque">
                        <?php // Autores
                            $temp = file('text/bloque3.txt');
                        ?>
                        <div class="headerBloque"><h3><?php echo $temp[0]; ?></h3></div>
                        <div class="contentBloque grialbackground">
                            <?php
                                for($i=1; $i<count($temp); $i++) echo $temp[$i];
                            ?>
                        </div>
                    </div>
				</div> <!-- Fin contentright -->
				
				<div id="pag">
                    <div id="prev-next">
                        <span><a href="content/page1.php">1</a></span>
                        <span><a href="content/page2.php">2</a></span>
                        <span><a href="content/page3.php">3</a></span>
                        <span><a href="content/page4.php">4</a></span>
                        <span><a class="nav" href="content/page1.php">Siguiente &raquo;</a></span>
                    </div>
                </div>
			</div> <!-- Fin content -->

			<div id="footer">
				<p>
					<img class="left" src="http://i.creativecommons.org/l/by-nc-sa/3.0/es/88x31.png" style="padding: 0 20px 0; height: 30px"/>
					<span>Este artículo está licenciado bajo <a href="http://creativecommons.org/licenses/by-nc-sa/3.0/es/" rel="license">Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License</a></span>
					<span>© 2011 GRupo de investigación en InterAcción y eLearning (GRIAL)</span>
				</p>
			</div> <!-- Fin footer -->

        </div><!-- Fin page -->
    </body>
</html>
