<?

include "config.php";
include "function.php";

$connection=connectdb();

//switching random play on or off
if ($switch){
	$queryprefsinit="select * from prefs";
	$resultprefsinit=mysql_query($queryprefsinit, $connection);
	$row=mysql_fetch_array($resultprefsinit);
	if ($row[randomplay] == y){
		$statusfirst="ON";
	}
	else 
	$statusfirst="off";

	if ($statusfirst=="ON"){ $queryprefs="update prefs set randomplay='n'"; } 
	else { $queryprefs="update prefs set randomplay='y'"; }

	$resultprefs=mysql_query($queryprefs, $connection);
	if (!$resultprefs){ echo "Ging nie goei"; }
}

//checking what the current random status is
$queryprefsinit="select * from prefs";
$resultprefsinit=mysql_query($queryprefsinit, $connection);
$row=mysql_fetch_array($resultprefsinit);
if ($row[randomplay] == y){
	$status="ON";
}
else 
	$status="off";

session_write_close();
//starting the jukebox
if ($start) {
	startthebox();
}
//killing the jukebox
if ($kill){
	killthebox();
}
session_start();

//stop playing after current song
if ($stopplaying){
	$queryprefs="update prefs set play='n'";
	$resultprefs=mysql_query($queryprefs, $connection);
	if (!$resultprefs){
		echo "Ging nie goei";
	}
}

//making the genres entry for playing randomly when no playlist is set
if ($genre){
	if (in_array("all", $genre) || count($genre)<"1") { unset($genre); $genre[]='all'; }
	$joinedgenre = join (",", $genre);
	$queryprefsgenre="update prefs set genre='$joinedgenre'";
	$resultprefsgenre=mysql_query($queryprefsgenre, $connection);
	if (!$resultprefsgenre){
		echo "Ging nie goei";
	}
}

if ($row[genre]=="") { $genre[]="all"; }

if (!$genre) {
	$genre=explode (",", $row[genre]);
}

html_header();
?>
<form method="post">
<? start_maintable(); ?>
<p align="center"><img src="<? echo $skindir.$skin; ?>/header-admin.jpg"></p>
                <table class="admintable" cellspacing="<? echo $skininfo['cellspacing']['admintable']; ?>">
                  <tr> 
                    <th width="25%" class="admintable">
						<img src="<? echo $skindir.$skin; ?>/header-boxkill.jpg">
                    </th>
                    <th width="25%" class="admintable"> 
                    	<img src="<? echo $skindir.$skin; ?>/header-boxstop.jpg">
                    </th>
                    <th width="25%" class="admintable"> 
                    	<img src="<? echo $skindir.$skin; ?>/header-playgenres.jpg">
                    </th>
                    <th width="25%" class="admintable"> 
                    	<img src="<? echo $skindir.$skin; ?>/header-playrandom.jpg">
                    </th>
                  </tr>
                  <tr> 
                    <td width="25%" class="admintable">
                        <input type="submit" name="kill" value="kill the box">
                        <input type="submit" name="start" value="start the box"><br>
                        <input type="submit" name="stopplaying" value="stop playing after current song">
                    </td>
                    <td width="25%" class="admintable" valign="top">
                        <select name="setskin" size="9">
<?
// SKIN SUPPORT;
$dp=opendir($htmldocroot."/".$skindir);
while ($fn=readdir($dp)) {
//	if (is_dir($htmldocroot."/".$skindir.$fn) && $fn!='.' && $fn!='..') {
	if (is_readable($htmldocroot."/".$skindir.$fn."/settings.php") && $fn!='.' && $fn!='..') {
		include $htmldocroot."/".$skindir.$fn."/settings.php";
		echo "<option value='$fn'>".$skininfo["skinname"]."</option>";
	}
}
closedir($dp);
include $htmldocroot."/".$skindir.$skin."/settings.php";
?>
						</select><br><br>
						<input type="submit" name="postskin" value="select the skin">
                    </td>
                    <td width="25%" class="admintable" valign="top">
						<select name="genre[]" size="9" multiple>
						<option value="all"<? if (in_array("all", $genre)) { echo " selected"; } ?>>-Play all-</option>
                        
<?
			//selecting filled genres and give the current random genres
				$sql1="select * from genres";
				$res1=mysql_query($sql1, $connection);

				while ($r=mysql_fetch_array($res1)) {
					$genres[$r[id]]=$r[description];
				}

				$sql="select DISTINCT genreid from files";
				$res=mysql_query($sql, $connection);

				while ($r=mysql_fetch_array($res)) {
					if ($r[genreid] == "0"){continue;}
					if (in_array($r[genreid], $genre)) { $selected=" selected"; } else { $selected=""; }
				echo"<option value=\"$r[genreid]\"$selected>".$genres[$r[genreid]]."</option>\n";
				}

				echo "</select><br>";
  				echo "<br>\n<i>Currently playing:</i><br>\n";
				
				if (!in_array("all", $genre)) {
					while (list($i, $v) = each ($genre)) {
						echo $genres[$v]."<br>\n";
					}
				} else { echo "All genres<br>\n"; }
?>
						<br><input type="submit" name="postgenres" value="select the genres">
                    </td>
                    <td width="25%" class="admintable">
                        <input type="submit" name="switch" value="switch"><br>
                        <? echo "Currently $status";?>
                    </td>
                  </tr>
                </table>
</form>
<?
end_maintable();
html_footer();
?>