<?php 	session_start(); 
		ini_set('display_errors', 1);
		ini_set('display_startup_errors', 1);
		error_reporting(E_ALL); 
		require("classes.php");
?>
<!doctype html>
<?php 
# set db login based on server host 

if($_SERVER['HTTP_HOST'] == "localhost") { $conn = new mysqli("localhost", "root", "", "pgl" ); }
elseif(strpos($_SERVER['HTTP_HOST'], "isappointment") || strpos($_SERVER['HTTP_HOST'], "awoftheplayground")) { $conn = new mysqli("localhost", $_SERVER['REDIRECT_DBUSER'], $_SERVER['REDIRECT_DBPASS'], $_SERVER['REDIRECT_DBUSER']); }

if($conn->connect_error) { die("Connection failed: ".$conn->connect_error); }
if(!isset($_SESSION['filters'])) $_SESSION['filters'] = 4;

if(!isset($_SESSION['admin'])) $_SESSION['admin'] = "no";

//sanitize $_GET['page'] if it's there, set it to 1 if it isn't - so we don't have to repeat this line
if(isset($_GET['page']) && intval($_GET['page']) == $_GET['page']) $get_page = $_GET['page']; else $get_page = 1;

if(isset($_GET['staging'])) {
	$page = new stagingIntro($_GET['staging']);
}  elseif(isset($_GET['letter'])) {
	$get_letter = strtolower(substr($_GET['letter'], 0, 1));
 	$page = new AlphaPage($get_letter, $get_page, $conn);
} elseif(isset($_GET['subject'])) {
	$get_subject = intval($_GET['subject']);
	if($get_subject > 0) $page = new SubjectPage($get_subject, $conn);
                 else    $page = new IntroPage();
} elseif (isset($_GET['cockfingers'])) {
	$page = new Cockfingers($get_page, $conn);
} elseif (isset($_GET['pupil'])) {
    $page = new PupilPage($_GET['pupil'], $conn); 
} elseif (isset($_POST['search'])) {
    $page = new SearchPage($_POST['search'], $conn);
} elseif (isset($_GET['type']) && $_GET['type'] == "latest") {
    $page = new LatestPage($conn, $get_page);
} elseif (isset($_GET['type']) && $_GET['type'] == "random") {
    $page = new RandomPage($conn);
} else {	
    $page = new IntroPage();
}



?>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<?php if(is_a($page, "PupilPage") || $_SESSION['filters'] < 4) { ?><meta name="robots" content="noindex"><?php } ?>
<title>The Law of the Playground</title>
<link href="playground.css" rel="stylesheet" type="text/css">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Cutive+Mono&family=Yantramanav:wght@400;900&display=swap" rel="stylesheet">

<script>
$(function(){
    $("#alpha-link").click(function(){ $("#search-menu, #filter-menu").hide(); $("#alpha-menu").slideToggle(); })
    $("#search-link").click(function(){ $("#filter-menu, #alpha-menu").hide(); $("#search-menu").slideToggle(); })
    $("#filter-link").click(function(){ $("#search-menu, #alpha-menu").hide(); $("#filter-menu").slideToggle(); })
})

function setDisplayVars(staging) {
	sessionStorage.setItem("staging3", "hiding");
	sessionStorage.setItem("staging2", "hiding");
	sessionStorage.setItem("staging1", "hiding");
	
	if(staging <= 3) {
		toggleDisplay('staging3', 'unapproved');
	}
	if(staging <= 2) {
		toggleDisplay('staging2', 'disapproved');
	}
	if(staging == 1) {
		toggleDisplay('staging1', 'deleted');
	}
	
}
function toggleDisplay (classToToggle, feedbackID) {
	if(sessionStorage.getItem(classToToggle) === null) { return false; }
	else {
		var classElements = document.getElementsByClassName(classToToggle);
		var lengthOfArray=classElements.length;

		for (var i=0; i<lengthOfArray;i++){
			if(sessionStorage.getItem(classToToggle) == "hiding")
			{ classElements[i].style.display='block'; }
			else 
			{ classElements[i].style.display='none'; }
		}
		
		if(sessionStorage.getItem(classToToggle) == "hiding") {
			sessionStorage.setItem(classToToggle, "showing");
			document.getElementById(feedbackID).innerHTML = "hide "+feedbackID;
		} else {
			sessionStorage.setItem(classToToggle, "hiding");
			document.getElementById(feedbackID).innerHTML = "show "+feedbackID;			
		}

	}	
}
</script>
</head>

<body onload="setDisplayVars(<?= $_SESSION['filters']; ?>)">
	<div class='page_container'>

		<div class='header'>
			<?php $page->show_header_image(); ?>

		</div>
		<div class='sidebar hidemobile'>
            <?php $page->on_page(); ?>
			<?= Page::$alphaGrid; ?>
            <?php $page->search_box(); ?>
            <?php if(!is_a($page, "IntroPage")) { $page->filters(); } ?>

		</div>
		<div class='content'>
			<?= $page->content; ?>
		</div>
		<div style='clear: both;'></div>
		<div class='footer'>
			<?= Page::$pageFooter; ?>
		</div>
        <div class="bottom-nav">
            <div class="nav-content" id="alpha-menu">
                <a href="index.php?letter=1">#</a> 
                <a href="index.php?letter=a">a</a> 
                <a href="index.php?letter=b">b</a> 
                <a href="index.php?letter=c">c</a> 
                <a href="index.php?letter=d">d</a> 
                <a href="index.php?letter=e">e</a> 
                <a href="index.php?letter=f">f</a> 
                <a href="index.php?letter=g">g</a> 
                <a href="index.php?letter=h">h</a><br>
                <a href="index.php?letter=i">i</a>
                <a href="index.php?letter=j">j</a> 
                <a href="index.php?letter=k">k</a> 
                <a href="index.php?letter=l">l</a> 
                <a href="index.php?letter=m">m</a> 
                <a href="index.php?letter=n">n</a> 
                <a href="index.php?letter=o">o</a> 
                <a href="index.php?letter=p">p</a> 
                <a href="index.php?letter=q">q</a><br> 
                <a href="index.php?letter=r">r</a> 
                <a href="index.php?letter=s">s</a> 
                <a href="index.php?letter=t">t</a> 
                <a href="index.php?letter=u">u</a> 
                <a href="index.php?letter=v">v</a> 
                <a href="index.php?letter=w">w</a> 
                <a href="index.php?letter=x">x</a> 
                <a href="index.php?letter=y">y</a> 
                <a href="index.php?letter=z">z</a><br>
                <a href="index.php?type=latest">Latest</a> | <a href="index.php?type=random">Random</a>
            </div>
            <div class="nav-content" id="search-menu">
                <form method='post' action='index.php'>
                    <input type='text' class='bot-nav-searchbox' name='search' placeholder='search Lotp' maxlength='25'> 
                    <input type='submit' class='bot-nav-searchbutton' name='submit' value='i feel pretty'>
                </form>
            </div>
            <div class="nav-content" id="filter-menu">
<?php
    $link = $_SERVER[ 'REQUEST_URI' ]; if ( $link == "index.php" ) { $link = "index.php?staging="; } else { $link = $link . "?staging="; }
?>
                You were never supposed to see these<br><a href="<?= $link ?>1">* Deleted *</a><br><a href="<?= $link ?>2">** Snubbed **</a><br><a href="<?= $link ?>3">*** Pending ***</a><br><a href="<?= $link ?>4">**** Beloved ****</a>
            </div>
            <div class="nav-buttons">
            <?php if($page->pages > 1) { $page->show_pagination(false); } ?>
            </div>
            <div class="nav-buttons">
                <div class="nav-button" id="alpha-link">Browse</div>
                <div class="nav-button" id="search-link">Search</div>
                <div class="nav-button" id="filter-link">Filters</div>
                <div class="nav-button" id="about-link"><a href="index.php">About</a></div>
            </div>
        </div>
	</div>
</body>
</html>
