<?php

function page_comparator($a,$b){
    //This function is the sorting criterion for the later call of usort, which will sort the page objects
    //from pages.json in increasing order of their "index" value.
    return $a->index > $b->index;
}

function gen_nav_element($page){
    if(isset($page->file)){
        $href = "index.php?page=".$page->query_value;
        $target = "_self";
        //If the page is associated with a file, then point the navibar href to the file's query value.
    } elseif(isset($page->link)) {
        $href = $page->link;
        $target = "_blank";
        //If instead the page is associated with an external link, then point the navibar href there and make it open in a new window.
    }
    echo "<a href='".$href."' target='".$target."'><div class='nav-tab'>".$page->name."</div></a>";
}

$page_array = get_object_vars($pages);
//Convert $pages from object to an associative array of [key=>val, key=>val]
//where the keys are the names of the page objects from the JSON file and the
//vals are the page objects themselves.
usort($page_array,"page_comparator");
//Sort the pages in order of increasing "index" value.

foreach($page_array as $key=>$p){
    if($p->index>-1){
        gen_nav_element($p);
        //Pages with indices less than 0 are hidden from the navibar.
    }
}

?>
