<?php

define('N',"\n");

$instpath='/mastostart';
$path=preg_replace('/^'.preg_quote($instpath,'/').'/','',$_SERVER['REQUEST_URI']);
$ndir=preg_match_all('/\//',$path)-1;
$prepath='';
for ($i=0; $i<$ndir; $i++)
	$prepath.='../';

$menu=array(
	'guide'=>array('liadd'=>null, 'href'=>$instpath.'/', 'title'=>'Guide', 'selected'=>false, 'submenu'=>null),
	'instances'=>array('liadd'=>null, 'href'=>$instpath.'/instances', 'title'=>'Instances', 'selected'=>false, 'submenu'=>null),
	'about'=>array('liadd'=>null, 'href'=>$instpath.'/about', 'title'=>'About', 'selected'=>false, 'submenu'=>null),
	'language'=>array('liadd'=>null, 'href'=>null, 'title'=>'Language', 'selected'=>false, 'submenu'=>array(
			'inglese'=>array('liadd'=>'onclick="golang(\'en\')" onmouseover="this.style.cursor=\'pointer\'"', 'href'=>null, 'title'=>'English', 'selected'=>false, 'submenu'=>null),
			'italiano'=>array('liadd'=>'onclick="golang(\'it\')" onmouseover="this.style.cursor=\'pointer\'"', 'href'=>null, 'title'=>'Italiano', 'selected'=>false, 'submenu'=>null)
		)
	)
);

function tradmenu($lang) {
	global $menu;
	switch ($lang) {
		case 'it':
		$menu['guide']['title']='Guida';
		$menu['guide']['href'].='it';
		$menu['instances']['title']='Istanze';
		$menu['instances']['href'].='/it';
		$menu['about']['title']='Info';
		$menu['about']['href'].='/it';
		$menu['language']['title']='Lingua';
		break;
	}
}

switch($path) {

	case '/':
	$cont=array(
		'fp'=>'home_en.php',
		'lang'=>'en',
		'atit'=>' - Guide',
		'desc'=>'A thorough introduction to Mastodon',
		'js'=>array('shsum','guideanchors','scrolltrack')
	);
	$menu['guide']['liadd']='onclick="shsum()" onmouseover="this.style.cursor=\'pointer\'; this.style.textDecoration=\'underline\'" onmouseout="this.style.textDecoration=\'none\'"';
	$menu['guide']['href']=null;
	$menu['guide']['selected']=true;
	$menu['language']['submenu']['inglese']['liadd']=null;
	$menu['language']['submenu']['inglese']['selected']=true;
	break;

	case '/it':
	$cont=array(
		'fp'=>'home_it.php',
		'lang'=>'it',
		'atit'=>' - Guida',
		'desc'=>'Una approfondita introduzione a Mastodon',
		'js'=>array('shsum','guideanchors','scrolltrack')
	);
	tradmenu('it');
	$menu['guide']['liadd']='onclick="shsum()" onmouseover="this.style.cursor=\'pointer\'"';
	$menu['guide']['href']=null;
	$menu['guide']['selected']=true;
	$menu['language']['submenu']['italiano']['liadd']=null;
	$menu['language']['submenu']['italiano']['selected']=true;
	break;

	case '/instances':
	$cont=array(
		'fp'=>'instances_en.php',
		'lang'=>'en',
		'atit'=>' - Instances',
		'desc'=>'Recommended Mastodon Instances',
		'js'=>array()
	);
	$menu['instances']['href']=null;
	$menu['instances']['selected']=true;
	$menu['language']['submenu']['inglese']['liadd']=null;
	$menu['language']['submenu']['inglese']['selected']=true;
	break;

	case '/instances/it':
	$cont=array(
		'fp'=>'instances_it.php',
		'lang'=>'it',
		'atit'=>' - Istanze',
		'desc'=>'Istanze Mastodon consigliate',
		'js'=>array()
	);
	tradmenu('it');
	$menu['instances']['href']=null;
	$menu['instances']['selected']=true;
	$menu['language']['submenu']['italiano']['liadd']=null;
	$menu['language']['submenu']['italiano']['selected']=true;
	break;

	case '/about':
	$cont=array(
		'fp'=>'about_en.php',
		'lang'=>'en',
		'atit'=>' - About us',
		'desc'=>'Infos about Mastodon Startpage’s authors, contributors, license',
		'js'=>array()
	);
	$menu['about']['href']=null;
	$menu['about']['selected']=true;
	$menu['language']['submenu']['inglese']['liadd']=null;
	$menu['language']['submenu']['inglese']['selected']=true;
	break;

	case '/about/it':
	$cont=array(
		'fp'=>'about_it.php',
		'lang'=>'it',
		'atit'=>' - Info',
		'desc'=>'Informazioni sugli autori, i collaboratori e la licenza di Mastodon Startpage',
		'js'=>array()
	);
	tradmenu('it');
	$menu['about']['href']=null;
	$menu['about']['selected']=true;
	$menu['language']['submenu']['italiano']['liadd']=null;
	$menu['language']['submenu']['italiano']['selected']=true;
	break;

	default:
	$cont=array(
		'fp'=>'404.php',
		'lang'=>'en',
		'atit'=>' - 404',
		'desc'=>'Page not found',
		'js'=>array()
	);
	break;
}

/*
 <li><a href="<?php echo($instpath); ?>">Guide</a></li>
 <li><a href="instances">Instances</a></li>
 <li><a href="about">About us</a></li>
 <li onmouseover="chulsh(this,true)" onmouseout="chulsh(this,false)">Language
  <ul class="ula" onmouseover="ulsh(this,true)" onmouseout="ulsh(this,false)">
  <li><a href="#">English</a></li>
  <li><a href="#">Italiano</a></li>
  </ul>
 </li>
*/

$menuout='';

function buildmenu($menu) {
	global $menuout;
	foreach ($menu as $key=>$arr) {
		$menuout.='<li';
		if (!is_null($arr['liadd'])) $menuout.=' '.$arr['liadd'];
		if (!is_null($arr['submenu'])) $menuout.=' onmouseover="chulsh(this,true)" onmouseout="chulsh(this,false)"';
		if ($arr['selected']) $menuout.=' class="hil"';
		$menuout.='>';
		if (!is_null($arr['href']))
			$menuout.='<a href="'.$arr['href'].'">'.$arr['title'].'</a>';
		else
			$menuout.=$arr['title'];
		if (!is_null($arr['submenu'])) {
// qui bisognerebbe aggiungere che a seconda del "livello" imposta class giusta: ula, oppure ulb per livello > 1
			$menuout.=N.'<ul class="ula" onmouseover="ulsh(this,true)" onmouseout="ulsh(this,false)">'.N;
			buildmenu($arr['submenu']);
			$menuout.='</ul>'.N;
		}
		$menuout.='</li>'.N;
	}
}

buildmenu($menu);

//echo(htmlentities($menuout));

$cjrand=rand(0,999999);

?>
<!DOCTYPE HTML>
<html lang="<?php echo($cont['lang']); ?>">
<head>
<title>Mastodon Startpage<?php echo($cont['atit']); ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="description" content="<?php echo($cont['desc']); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="icon" type="image/png" href="<?php echo($prepath); ?>imgs/icona-32.png" sizes="32x32">
<link rel="icon" type="image/png" href="<?php echo($prepath); ?>imgs/icona-192.png" sizes="192x192">
<link rel="icon" type="image/png" href="<?php echo($prepath); ?>imgs/icona-512.png" sizes="512x512">
<link rel="apple-touch-icon-precomposed" href="<?php echo($prepath); ?>imgs/icona-180.png">
<link rel="stylesheet" type="text/css" href="<?php echo($prepath); ?>theme.css?v=<?php echo($cjrand); ?>">
<?php
foreach ($cont['js'] as $val)
	echo('<script language="JavaScript" src="js/'.$val.'.js?v='.$cjrand.'"></script>'.N);
?>
<script language="JavaScript">
<!--
function chulsh(el,sh) {
	if (sh)
		el.querySelector('ul').style='display:block';
	else
		el.querySelector('ul').style='display:none';
}
function ulsh(el,sh) {
	if (sh)
		el.style='display:block';
	else
		el.style='display:none';
}
function golang(lang) {
	var loc=document.location.href;
	loc=loc.replace(/#.*$/,'');
	loc=loc.replace(/\/$/,'');
	if (document.documentElement.lang=='en') {
		if (lang!='en')
			document.location.href=loc+'/'+lang;
	} else {
		if (lang!='en')
			document.location.href=loc.substr(0,loc.length-3)+'/'+lang;
		else
			document.location.href=loc.substr(0,loc.length-3);
	}
}
//-->
</script>
</head>
<?php
require($cont['fp']);
?>
</html>
