<?php
$title = "SST";
ini_set('include_path', './' . PATH_SEPARATOR . '../' . PATH_SEPARATOR . ini_get('include_path'));


// Check to see if we have a link
if (isset($_REQUEST["link"])) {
	include_once("include/config.php");
	include_once("include/normpath.php");
	//$BASEDIR = getBaseDir(getcwd(), constant('CONTEXT')); // context must be correct
	//$BASEURL = 'http://'.$_SERVER["HTTP_HOST"];
	$BASEURL = constant('BASEURL');

	$link = normalise_path($_REQUEST["link"]);
	if (substr($link,0,4) === 'http' ) die('link error');
	else if (substr($link,0,2) === '..' ) die('link error');
	if (!file_exists($link)) {
		header( 'Location: '.$BASEURL.'/404.php' );
		die('404');
	}

	
	// Try to figure out the permlink parameters and pass them through
	$dom = new DOMDocument;
	$dom->loadHTMLFile( $link );
	$xpath = new DOMXPath($dom);

	// Pull out 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(0)->getAttribute('src');

	$url = $BASEURL.'/sst.php';
	$url .= '?permlink=true';
	$url .= '&relativeFolderPath='.dirname($link);
	$url .= '&regionalMap=1';
	$url .= '&imageUrl='.htmlspecialchars($BASEURL.'/'.dirname($link).'/'.$img);
	$url .= '&previous='.$prev;
	$url .= '&current='.basename($link);
	$url .= '&next='.$next;
	$url .= '&imgNameDate=todo';
	$url .= '&popup=false';
	$url .= '&baseFolderUrl='.htmlspecialchars($BASEURL.'/'.dirname($link));
	$url .= '&datePickerUrl='.dirname($link);
	$url .= '&debug=True';
	$url .= '&error=';
	//header('Location: http://www.google.com.au');
	header('Location: '.$url);
	exit;
}
else {
	$link='none';
}
//

include_once("include/header.php");
$docroot = $_SERVER["DOCUMENT_ROOT"];

/*
if (isset($url)) {
	echo "<code>link=$link</code>";
	$stuff = htmlspecialchars($url);
	echo "<code>url=$stuff</code>";
}
*/
if (isset($_REQUEST["regiontype"])) $regiontype=$_REQUEST["regiontype"];
else $regiontype=0;
if ($regiontype > 2||$regiontype<0) $regiontype=0;

?>
<map name="imap1">
	<area shape="rect" coords="63   -2  107   31" href="TimorP/latest.html" alt="TimorP" title="Timor Passage-Darwin">
	<area shape="rect" coords="46  27  81  59" href="Broome/latest.html" alt="Broome" title="Broome">
	<area shape="rect" coords="-35 -15   9  19" href="XmasI/latest.html" alt="XmasI" title="Christmas Is.">
	<area shape="rect" coords="17  48  48  75" href="NWS/latest.html" alt="NWS" title="NW shelf">
	<area shape="rect" coords="9  58  39  92" href="Ningaloo/latest.html" alt="Ningaloo" title="Ningaloo">
	<area shape="rect" coords="28  112   36  119" href="Rottnest/latest.html" alt="Rottnest" title="Rottnest Is">
	<area shape="rect" coords="17  107   36  121" href="DonPer/latest.html" alt="DonPer" title="Dongara-Perth">
	<area shape="rect" coords="5   88   39  130" href="Perth/latest.html" alt="Perth" title="Shark Bay-Perth">
	<area shape="rect" coords="5   88   39  130" href="Perth/latest.html" alt="Perth" title="Perth">
	<area shape="rect" coords="9  107   43  145" href="CLeeu/latest.html" alt="CLeeu" title="Cape Leeuwin">
	<area shape="rect" coords="113  127  145  152" href="SAgulfs/latest.html" alt="SAgulfs" title="SA gulfs">
	<area shape="rect" coords="88  113  128  140" href="EGAB/latest.html" alt="EGAB" title="Eastern GAB">
	<area shape="rect" coords="70  113  124  146" href="RechEyre/latest.html" alt="RechEyre" title="Esperance-Eyre Pen.">
	<area shape="rect" coords="117  121  169  173" href="Adelaide/latest.html" alt="Adelaide" title="Adelaide-Melbourne">
	<area shape="rect" coords="194  104  204  113" href="Coffs/latest.html" alt="Coffs" title="Coffs Harbour">
	<area shape="rect" coords="180  116  206  145" href="SNSW/latest.html" alt="SNSW" title="Southern NSW">
	<area shape="rect" coords="191   80  209  104" href="Brisbane2/latest.html" alt="Brisbane2" title="Brisbane">
	<area shape="rect" coords="185   92  215  128" href="Bris-Syd/latest.html" alt="Bris-Syd" title="Brisbane-Sydney">
	<area shape="rect" coords="178   58  213   87" href="SGBR/latest.html" alt="SGBR" title="Southern GBR">
	<area shape="rect" coords="161   31  196   64" href="CGBR/latest.html" alt="CGBR" title="Central GBR">
	<area shape="rect" coords="143    2  189   34" href="NGBR/latest.html" alt="NGBR" title="Northern GBR">
	<area shape="rect" coords="187   73  222  121" href="Brisbane/latest.html" alt="Brisbane" title="Brisbane-Newcastle">
	<area shape="rect" coords="182  105  230  135" href="LordHoweS/latest.html" alt="LordHoweS" title="Lord Howe Is.-Sydney">
	<area shape="rect" coords="159  155  196  182" href="TasE/latest.html" alt="TasE" title="Tas-east">
	<area shape="rect" coords="169  121  204  173" href="Syd-Hob/latest.html" alt="Syd-Hob" title="Sydney-Hobart">
	<area shape="rect" coords="143  155  191  189" href="Tas/latest.html" alt="Tas" title="Tasmania">
	<area shape="rect" coords="26  121   74  147" href="AlbEsp/latest.html" alt="AlbEsp" title="Albany-Esperance">
	<area shape="rect" coords="46  18  83  48" href="Kimberley/latest.html" alt="Kimberley" title="Kimberley">
</map>
<map name="imap2">
	<area shape="rect" coords="139   78  199  152" href="SE/latest.html" alt="SE" title="South East">
	<area shape="rect" coords="3   62   71  134" href="SW/latest.html" alt="SW" title="South West">
	<area shape="rect" coords="129   15  190   87" href="NE/latest.html" alt="NE" title="North East">
	<area shape="rect" coords="20   7  95  80" href="NW/latest.html" alt="NW" title="North West">
	<area shape="rect" coords="54   98  156  156" href="GAB/latest.html" alt="GAB" title="South">
	<area shape="rect" coords="88  141  160  189" href="SO/latest.html" alt="SO" title="Southern Ocean">
</map>
<map name="imap3">
<!--
    <area shape="rect" coords="4  46  84  86" href="sst_n/latest.html" alt="sst_n"
          title="Sea Surface Temperature (Northern region)">
    <area shape="rect" coords="4  132   84  172" href="sst_s/latest.html" alt="sst_s"
          title="Sea Surface Temperature (Southern region)">
    <area shape="rect" coords="77   84  144  116" href="sst_anom/latest.html" alt="sst_anom"
          title="Sea Surface Temperature anomaly (Australasian region)">
-->
    <area shape="rect" coords="138   46  207   78" href="ht/latest.html" alt="ht"
          title="gridded sea level anomaly from altimeters">
<!--
    <area shape="rect" coords="117  154  207  186" href="uv/latest.html" alt="uv"
          title="Surface current (from sea level anomaly plus time-mean velocity)">
-->
</map>

<?php
/*
	// Create our imap1m from the index file
$dom = new DOMDocument;
$dom->loadHTMLFile( 'index.htm' );
$xpath = new DOMXPath($dom);

$imap1 = $xpath->query('//map[@name="imap1"]')->item(0);
$ndom = new DOMDocument;
$node = $ndom->importNode($imap1,true);
$node->setAttribute('name','imap1m');
$ndom->appendChild($node);

// Fix the hrefs so they point to the latest image
foreach( $ndom->getElementsByTagName('area') as $rec ) {
	if ($rec->hasAttribute('href')) {
		$href = $rec->getAttribute('href');
		$files = scandir( dirname($href), 1 );
		$ref = NULL;
		foreach( $files as $file ) {
			if ( substr($file,-4) === '.fli' ) {
				$ref = dirname($href).'/'.$file;
				break;
			}
		}
		if ( !is_null( $ref )) {
			$rec->setAttribute('href', $ref );
		}
		else {
			// we'll delete this node
			$rec->parentNode->removeChild( $rec );
		}
	}
}

echo $ndom->saveHTML();
*/
?>


<div class="row">
    <div class="col-md-4">
        <h3 class="highlightedHeader">Adjusted Sea Level Anomaly and SST maps</h3>
        <div class="panel-group" id="accordian" role="tablist" aria-multiselectable="true">
            <div class="panel panel-default">
                <div class="panel-heading" role="tab" data-toggle="collapse"  data-parent="#accordian" data-target="#zoomInsContent" >
                    <h4 class="panel-title">
                        <a class="external" href="#" aria-expanded="true" aria-controls="zoomInsContent"> Local regions </a>
                    </h4>
                </div>
				<?php if ($regiontype==0): ?>
                <div id="zoomInsContent" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="zoomIns">
				<?php else: ?>
                <div id="zoomInsContent" class="panel-collapse collapse" role="tabpanel" aria-labelledby="zoomIns">
				<?php endif ?>
                    <div class="panel-body">
						<img class="highlight" src="misc/imap1.gif" usemap="#imap1">
                    </div>
                </div>
            </div>

<!--
            <div class="panel panel-default">
                <div class="panel-heading" role="tab" data-toggle="collapse"  data-parent="#accordian" data-target="#zoomInsContentMovie" >
                    <h4 class="panel-title">
                        <a class="external" href="#"
                           aria-expanded="false"
                           aria-controls="zoomInsContentMovie">
                            Latest movies
                        </a>
                    </h4>
                </div>
                <div id="zoomInsContentMovie" class="panel-collapse collapse" role="tabpanel"
                     aria-labelledby="zoomInsMovie">
                    <div class="panel-body">
						<img class="highlight" src="misc/imap1.gif" usemap="#imap1m">
                    </div>
                </div>
            </div>
-->

            <div class="panel panel-default">
                <div class="panel-heading" role="tab"  data-toggle="collapse" data-parent="#accordian" data-target="#midSizeContent">
                    <h4 class="panel-title">
                        <a class="external" href="#" aria-expanded="false" aria-controls="midSizeContent"> State regions </a>
                    </h4>
                </div>
				<?php if ($regiontype==1): ?>
                <div id="midSizeContent" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="midSize">
				<?php else: ?>
                <div id="midSizeContent" class="panel-collapse collapse" role="tabpanel" aria-labelledby="midSize">
				<?php endif ?>
                    <div class="panel-body">
						<img class="highlight" src="misc/imap2.gif" usemap="#imap2">
                    </div>
                </div>
            </div>
            <div class="panel panel-default">
                <div class="panel-heading" role="tab"  data-toggle="collapse" data-parent="#accordian" data-target="#largeAreaContent">
                    <h4 class="panel-title">
                        <a class="external" href="#" aria-expanded="false" aria-controls="largeAreaContent"> Australia region </a>
                    </h4>
                </div>
				<?php if ($regiontype==2): ?>
                <div id="largeAreaContent" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="largeArea">
				<?php else: ?>
                <div id="largeAreaContent" class="panel-collapse collapse" role="tabpanel" aria-labelledby="largeArea">
				<?php endif ?>
                    <div class="panel-body">
<!--
						<img class="highlight" src="misc/imap3.gif" usemap="#imap3">
-->
						<p><a href="#" class="bproxy btn btn-primary btn-block" data-href="ht/latest.html" type="button">Adjusted Sea Level Anomaly</a></p>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-8">
        <div id="proxiedPagesContainer">
            <div class="voffset4"></div>
            <!--<h4>Choose and then click on a map to see the graphs</h4>-->
            <h4>Choose map scale and region</h4>
            <a href="<?php echo $BASEDIR ?>whatsshown.php" class="btn btn-primary " type="button">What's shown in
                these maps</a>
        </div>
    </div>
</div>

<script>
$('.bproxy').click(function(){
	var href=$(this).attr("data-href");
	var data = {'relUrl': href};
	$.ajax({
		url: "proxy.php",
		data: data,
		success: function (res, status) {
			if (status == "success") {
				$('#proxiedPagesContainer').html(res);
			}
		}
	});
});
</script>

<?php
include_once("include/footer.php");
?>
