<?php

include_once "open_db.pinc";
include_once "search.pinc";
include_once "html.pinc";

$genre = new MusicGenre( $_REQUEST['genre'] );

$songs = mysql_select_item( "tbl_Song", 0, "COUNT(*)-1" );
$artists = mysql_select_item( "tbl_Artist", 0, "COUNT(*)-1" );
$albums = mysql_select_item( "tbl_Album", 0, "COUNT(*)-1" );
$genres = mysql_select_item( "tbl_Genre", 0, "COUNT(*)-1" );

$playtime = mysql_select_item( "tbl_Song", "genre=" . $genre->Id(), "SUM(play_time)" );
$years = mysql_select_assoc( "tbl_Song", "genre=" . $genre->Id(), "MIN(release_year) AS syear,MAX(release_year) AS eyear" );
if( $years['syear'] == $years['eyear'] ) {
	$release_years = $years['syear'];
} else {
	$release_years = $years['syear'] . " - " . $years['eyear'];
}
?>
<html>
<head>
<title>Music Database: <? print $genre->Name(); ?> Genre</title>
<? PrintMusicDBStyleSheet(); ?>
</head>
<body bgcolor=white>
<? print MusicDBHeader(); ?>
<br>
<table border=0 cellpadding=3 cellspacing=5 width=100%>
<tr><td colspan=3 bgcolor=#9999cc>&nbsp;<? printf( "Genre: <font color=white>%s</font>", htmlencode( $genre->Name() ) ); ?></font></td></tr>
<tr><td width=100% valign=top><table border=0 cellpadding=3 cellspacing=5>
<tr><td width=15%>Genre:</td><td><?  print htmlencode( $genre->Name() ); ?></td></tr>
<tr><td valign=top>Years:</td><td><? print htmlencode($release_years); ?></td></tr>
<tr><td valign=top>Play Time:</td><td><? printf( "%d:%02d:%02d", $playtime / 3600, ( $playtime % 3600 ) / 60, $playtime % 60 ); ?></td></tr>
<tr><td valign=top>Description:</td><td><? print htmlencode( $genre->Description() ); ?></td></tr>
<tr><td colspan=2 valign=top>
<table border=0 cellpadding=5 cellspacing=0>
<tr bgcolor=#9999cc><th valign=bottom>Album</th><th valign=bottom>Artist</th><th valign=bottom>Time</th><th valign=bottom>Release<br>Year</th></tr>
<?
	$search = new MusicSearch();
	$search->GenreIds( $genre->Id() );
	$search->SetSearchType( "album" );
	$search->SortBy("tbl_Album.album");
	$all_complete = 1;
	$num_complete = 0;
	
	while( $album = $search->Next() ) {
		if( !$album->Complete() ) {
			$all_complete = 0;
			continue;
		}
		
		$num_complete++;
		
		printf( "<tr bgcolor=%s><td><a href=\"album.phtml?album=%s\">%s</td>\n", AlternateColor(), $album->Id(), $album->Name() );
		printf( "<td><a href=\"artist.phtml?artist=%s\">%s</td>\n", $album->Artist(), $album->Artist() );
		printf( "<td align=right>%d:%02d</td>\n", $album->Playtime() / 60, $album->Playtime() % 60 );
		printf( "<td align=center>%s</td><tr>\n", $album->ReleaseYear() );		
	}
	
	if( $num_complete == 0 ) {
		print "<tr><td colspan=3>There are no complete albums.</td></tr>\n";
	}		
	
	if( !$all_complete ) {
		print "<tr><td colspan=3 align=center><font size=-1>-- Incomplete Albums --</font></td></tr>\n";
		$search->Rewind();
		while( $album = $search->Next() ) {
			if( $album->Complete() || $album->Id() == 0 ) continue;

			printf( "<tr bgcolor=%s><td><a href=\"album.phtml?album=%s\">%s</td>\n", AlternateColor(), $album->Id(), $album->Name() );
			printf( "<td><a href=\"artist.phtml?artist=%s\">%s</td>\n", $album->Artist(), $album->Artist() );
			printf( "<td align=right>%d:%02d</td>\n", $album->Playtime() / 60, $album->Playtime() % 60 );
			printf( "<td align=center>%s</td><tr>\n", $album->ReleaseYear() );		
		}
	}
?>
</td>
</tr>
</table>
<td align=right valign=top>
<table border=0 cellpadding=5 cellspacing=0>
<tr bgcolor=#9999cc><th valign=bottom>Artist</th><th valign=bottom>Time</th><th valign=bottom>Release<br>Years</th></tr>
<?
	$search = new MusicSearch();
	$search->GenreIds( $genre->Id() );
	$search->SetSearchType( "artist" );
	$search->SortBy("tbl_Artist.artist");
	
	while( $artist = $search->Next() ) {		
		printf( "<tr bgcolor=%s><td><a href=\"artist.phtml?artist=%s\">%s</td>\n", AlternateColor(), $artist->Id(), $artist->Name() );
		printf( "<td align=right>%d:%02d</td>\n", $artist->Playtime() / 60, $artist->Playtime() % 60 );
		printf( "<td align=center>%s</td><tr>\n", $artist->ReleaseYears() );		
	}	
?>
</td>
</tr>
</table>
</td></tr>
</table>
</td>
</tr>
</table>
<?
	print MusicDBFooter();
?>
</body>
</html>
