<?php
$title = "Argo map";
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($xrlink);
$dom->formatOutput = true;
$xpath = new DOMXPath($dom);

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

// Extract the interesting bits
$prev = $xpath->query('//a[text()="[PREV]"]')->item(0)->getAttribute('href');
$next = $xpath->query('//a[text()="[NEXT]"]')->item(0)->getAttribute('href');
$img = $dom->getElementsByTagName('img')->item(1);
$imgfile = dirname($xrlink).'/'.$img->getAttribute('src');
$imgmap = $img->getAttribute('usemap');

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

// First off we copy the map to the output document
foreach ($dom->getElementsByTagName('map') as $rec) {
	$node = $ndom->importNode($rec,true);
	$ndom->appendChild($node);
}

// Fix up the hrefs
foreach($ndom->getElementsByTagName('area') as $link ) {
	if ($link->hasAttribute( 'href' )) {
		$href = $link->getAttribute( 'href' );
		// Check for a trailing space and remove it (this can be removed after the files are regenerated).
		$href = str_replace( '.html ', '.html', $href );
		$link->setAttribute( 'href', 'profile.php?link='.substr($href,3) );
		// Link to new cycle page...
		$v = explode('_', basename($href));
		$wmoid = $v[1];
		$cycle = substr($v[2],0,-4);
		$link->setAttribute('href', "cycle.php?wmoid=$wmoid&cycle=$cycle&depth=0");
	}   
}

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

// Write out the map first
echo $ndom->saveHTML();

// Start sticking everything back together (mostly html)
?>

<h3>Argo profiles in the Australian region</h3>
<h4>Temperature and salinity down to 2000m</h4>
<p>
	<a href="<?php echo 'map.php?link='.dirname($xrlink).'/'.$prev; ?>">[PREV]</a> 
	<a href="<?php echo 'map.php?link='.dirname($xrlink).'/'.$next; ?>">[NEXT]</a>
	<a href="map_index.php?link=<?php echo dirname($xrlink); ?>">[DATE INDEX]</a>
	<a href="whatsshown.php">[What's shown]</a> <br>
	<img src="<?php echo $imgfile; ?>" usemap="<?php echo $imgmap; ?>"><br>
</p>
<p>
	Click on the map below or choose a <a href="profile_index.php">[WMO number]</a> to see the temperature and 
	salinity data from the selected float compared with satellite-adjusted climatology.
</p>
<p>
	Depending on your settings, your browser may or may not open these links in a separate window.<br>
	If nothing seems to open, try right-click/open-in-new-window
</p>

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