<?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) );
		$mtype = $link->getAttribute('data-maptype');
		// default behaviour if null
		if (is_null($mtype) || ($mtype == 'Argo')) {
			// 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");
		}
		else {
			if ($mtype == 'FS') {
				$x = explode('/', $href);
				$n = count($x);
				$dt = substr($x[$n-1],0,8);
				$region = $x[$n-3];
				$link->setAttribute('href', "/fishsoop_php/fsa.php?region=$region&date=$dt");
			}
		}
	}   
}

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 and FishSOOP 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 an Argo symbol on the map (or select one by <a href="profile_index.php">[WMO number]</a>) to see T &amp; S profiles, or on a FishSOOP symbol for a cluster of T profiles.
</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");
?>
