<?php
$title = "Gliders";
ini_set('include_path', './' . PATH_SEPARATOR . '../' . PATH_SEPARATOR . '../../' . PATH_SEPARATOR . ini_get('include_path'));
include_once("include/header.php");
include_once("include/normpath.php");
?>

    <!--
   **** start with layout component from http://getbootstrap.com/css/#grid
   Any relative links not in the same directory as the templated file should be prepended with
   <?php /* echo $BASEDIR */ ?>
-->
<?php
$debug = False;
$rlink = $_REQUEST["link"];
if (substr($rlink,0,4) === 'http' ) die('link error');

$xrlink = normalise_path( $rlink );
if (substr($xrlink,0,2) === '..' ) die('link error');

$dom = new DOMDocument;
$dom->loadHTMLFile('yearmaps/'.$xrlink);

if ($debug) {
	$stuff = htmlspecialchars($dom->saveXML());
	echo "<p>INPUT<code>$stuff</code></p>";
}

// Create the output document
$ndom = new DOMDocument;
//$ndom->formatOutput = true;

// Extract the interesting bits
$xpath = new DOMXPath($dom);

// Copy the 'a' nodes
foreach($dom->getElementsByTagName('a') as $rec) {
	$node = $ndom->importNode($rec,true);
	$ndom->appendChild($node);
}
// Stick in a break
$node = $ndom->createElement('br');
$ndom->appendChild($node);

// Copy the 'map' nodes
foreach($dom->getElementsByTagName('map') as $rec) {
	$node = $ndom->importNode($rec,true);
	$ndom->appendChild($node);
}

// Copy the image
$img = $xpath->query('/html/body/img[1]')->item(0);
$node=$ndom->importNode($img,true);
$ndom->appendChild($node);
// Fix the image link
$src = $node->getAttribute('src');
$node->setAttribute('src', 'yearmaps/'.$src );

// Replace the first
$ndom->firstChild->setAttribute('href','index.php');

// Fix the remaining links
foreach($ndom->getElementsByTagName('a') as $link ) {
	if ($link === $ndom->firstChild ) continue;
	if ($link->hasAttribute( 'href' )) {
		$href = $link->getAttribute( 'href' );
		// Remove any "Conditions of use" links from the body
		// as it's already in the footer.
		if ($href === 'http://imos.org.au/imostermsofuse.html') {
			$link->parentNode->removeChild($link);
		}
		else {
			$link->setAttribute('href','yearmaps.php?link='.$href);
		}
	}
}

// Fix the links in the map
foreach($ndom->getElementsByTagName('area') as $link ) {
	if ($link->hasAttribute( 'href' )) {
		$href = $link->getAttribute( 'href' );
		if ($href !== '#') {
			if (substr($href,0,3)==='../') {
				$link->setAttribute('href','4day.php?link='.substr($href,3));
			}
		}
	}
}

if ($debug) {
	$stuff = htmlspecialchars($ndom->saveXML());
	echo "<p>OUT<code>$stuff</code></p>";
}

// print the beast.
echo $ndom->saveHTML();

?>


<?php
include("include/sfooter.php");
?>
