I noticed that entries in #nav are stripped of question marks and such. I suggest changing
echo $stags." href='".$hostname.$page."'>".str_replace('-',' ',$page)." ".$etags." \n";
into:
echo $stags." href='".$hostname.$page."'>".$mlist[$i]." ".$etags." \n";