<?php
require_once ("../common.php");

scopConnect();
if (isset($_GET['ver']))
    $ver = $_GET['ver'];
else if (isset($_POST['ver']))
    $ver = $_POST['ver'];

$scopReleaseID = 0;
if (isset($ver))
    $scopReleaseID = lookupSCOPVersion(mysqli_real_escape_string($mysqlLink,$ver));
$maxSCOP = getLatestSCOPRelease();
if ($scopReleaseID==0)
    $scopReleaseID = $maxSCOP;
$scopRelease = getSCOPVersion($scopReleaseID);
$dbName = getDBName($scopReleaseID);
if (($isProduction) && ($hackedURLs))
    $verString = "ver=$scopRelease";
else
    $verString = "?ver=$scopRelease";

printCommonHeader("tab7",$scopReleaseID);
     
$helpTopics = array();
$helpDesc = array();
$lastLevel = 0;

function addHelp($topic, $description, $level) {
    global $helpTopics;
    global $helpDesc;
    global $verString;
    global $rootURL;
    global $scopRelease;
    global $scopReleaseID;
    global $isProduction;
    global $hackedURLs;
    global $lastLevel;

    if (count($helpTopics)>1) {
	// end previous 1-level topic
        if ($lastLevel==1) {
            print("<hr>\n");
	    print("</section>\n");
        }
        if ($level==0)
	    print("</div>\n");
    }
    if ($level == 0) {
	print("<div id=\"$topic\">\n");
	$topic = ">$topic";
    }
    else {
	print("<section id=\"$topic\">\n");
	print("<div class=\"feature\">\n");
	include "topics/$topic.php";
	print("</div>\n");
    }
    
    array_push($helpTopics,$topic);
    array_push($helpDesc,$description);
    $lastLevel = $level;
}

function showIndex() {
    global $helpTopics;
    global $helpDesc;
?>
</section>
</div>
</div>
<div class="col-md-3">
<nav id="helpnav" data-spy="affix">
<ul class="nav hidden-xs hidden-sm">
<?php
    $len = count($helpTopics);
    for ($i=0; $i<$len; $i++) {
	$topic = $helpTopics[$i];
	$description = $helpDesc[$i];
	$level = 1;
	if (strpos($topic,">")===0) {
	    $level = 0;
	    $topic = substr($topic,1);
	}
	if ($level==0) {
	    if ($i>0) {
		print("</ul>\n");
		print("</li>\n");
	    }
	    print("<li>\n<a href=\"#$topic\" class=\"nowrap\">$description</a>\n");
	    print("<ul class=\"nav\">\n");
	    $topic = ">$topic";
	}
	else
	    print("<li>\n<a href=\"#$topic\">$description</a>\n</li>\n");
    }
?>
</ul>
</li>
<li class="back-to-top"><a href="#top">back to top</a></li>
</ul>
</nav>
</div>
<?php
}

function helpReferences($firstID, $lastID, $refs="") {
    global $verString;
    global $rootURL;

    $firstVer = getDBName($firstID)." version ".getSCOPVersion($firstID);
    $lastVer = "current release";
    if ($lastID > 0)
	$lastVer = getDBName($lastID)." version ".getSCOPVersion($lastID);
    
    print("<div class='fproperties'>\n");
    print("<ul>\n");
    print("<li>Applies to: $firstVer through $lastVer</li>");
    if (strlen($refs) > 0) {
	$r = "Reference";
	if (strlen($refs) > 2)
	    $r .= "s";
	print("<li>$r: <a href='$rootURL"."references/$verString'>$refs</a></li>\n");
    }
    print("</ul>");
    print("</div>\n");
}

function sunidLink($sunid, $anchortext) {
    global $rootURL;
    print("<a href='$rootURL?sunid=$sunid'>$anchortext</a>");
}

function pubmedLink($pubmedID, $anchortext) {
  $pubmedURL = "http://www.ncbi.nlm.nih.gov/pubmed/";
  print("<a href='$pubmedURL$pubmedID'>$anchortext</a>");
}

?>
<div id="help" class="col-md-9">
<h2><span class="dbplain"><?php print("$dbName $scopRelease"); ?></span> help</h2>
<?php

include($scopRelease.".php");
showIndex();

?>
<script type="text/javascript">
  $(document).ready(function(){
    $('body').css({
	position: "relative"
    });
    $('body').scrollspy({
	target: "#helpnav",
	offset: $('#help').position().top
    });
  });
</script>
<?php

printCommonFooter($scopReleaseID);
mysqli_close($mysqlLink);
?>
