<div id="content">

<div class="huge_box">

<?php 
switch ($orby) {
	case 'sub_asc': {
		$ordby = ' numSub ASC ';
		break;
	} case 'sub_desc': {
		$ordby = ' numSub DESC ';
		break;
	} case 'meas_asc': {
		$ordby = ' numMes ASC ';
		break;
	} case 'meas_desc': {
		$ordby = ' numMes DESC ';
		break;
	} case 'priv_desc': {
		$ordby = ' u.Privilegy DESC ';
		break;
	} case 'priv_asc': {
		$ordby = ' u.Privilegy ASC ';
		break;
	} case 'user_desc': {
		$ordby = ' u.Username DESC ';
		break;
	} default : {
		$orby = 'user_asc';
		$ordby = ' u.Username ASC ';
		break;	
	}
}
switch ($sbm) {
	case 'mes': {
		$subm = '  ';
		break;
	} case 'my': {
		$subm = '  ';
		break;
	} default : {
		$subm = ' ';
		break;
	} 
}


if (isset($_GET['pg'])) {
	$pg = $_GET['pg'];
} else {
	$pg = 0;	
}
$displaying = 10;
$pagination = " LIMIT " . $pg . ", " . $displaying;
$currentpage = floor($pg / $displaying) + 1;

/*
?>
<div class="tab">
<ul>
<li><a href="#" class="selected">All Pages</a></li>
<li><a href="#">My Pages</a></li>
<li><a href="#">Measurement</a></li>
</ul>
</div>
*/ ?>
<div class="huge_box_header">
<div class="title">Users</div>
<div class="nums" style="font-size:12px;"><a href="?page=users&orby=<?php if ($orby == 'priv_desc') echo 'priv_asc'; else echo 'priv_desc';?>" style="font-size:12px">Privilegy</a></div>

<div class="nums" style="font-size:12px; text-align:center"><a href="?page=users&orby=<?php if ($orby == 'sub_desc') echo 'sub_asc'; else echo 'sub_desc';?>" style="font-size:12px">Submitted</a></div>

<div class="nums" style="font-size:12px; text-align:center"><a href="?page=users&orby=<?php if ($orby == 'meas_desc') echo 'meas_asc'; else echo 'meas_desc';?>" style="font-size:12px">Measurement</a></div>
</div>

<?php
$addSearch = "";
if ($search != '') {
	//$addSearch = " AND p.Title REGEXP '$search' ";
}

$res = mysql_query ("SELECT * FROM user ORDER BY FullName ASC ");

$nr = mysql_num_rows ($res);
$numresults = $nr;
$totalpages = floor(($nr - 1) / $displaying) + 1;

$query = "
SELECT 
    u.Id, u.Username, u.FullName, u.Privilegy, u.Verified,  
    COUNT(DISTINCT um.Id) AS numMes,
    COUNT(DISTINCT us.Id) AS numSub 
FROM 
    user AS u
    LEFT OUTER JOIN user_page AS um ON u.Id = um.UserId
    LEFT OUTER JOIN user_page_submission AS us ON u.Id = us.UserId 
GROUP BY 
    u.Id 
ORDER BY " . $ordby;	
$res = mysql_query ($query .  $pagination );

$nr = mysql_num_rows ($res);

if ($nr == 0) {
	?>
    <div class="huge_box_item">
	
    <div class="avatar"></div>
	<div class="description">
	<div class="title">No matches for query <?php echo $search; ?>.</div>
    </div>
    </div>
    <?php
}

if ($nr == 1 and $pg==0) {
	$row = mysql_fetch_array ($res);
		extract ($row);
		?>
 		<script type="text/javascript">
<?php /*		window.location = '?page=user&id=<?php echo $Id; ?>'; */ ?>
    	</script>
        <?php	
}

while ($nr > 0) {
	$row = mysql_fetch_array ($res);
	extract ($row);
	?>
    <?php if ($Verified != 1) { ?>
    <div class="private_huge_box">
    <?php } ?>
    
	<div class="huge_box_item">
	
	<div class="description">
	<div class="title"><a href="index.php?page=user&id=<?php echo $Id; ?>"><?php echo $Username; ?></a></div>
    <div class="category"><?php echo $FullName; ?></div>
	</div>
	
    <div class="fans">
    <div class="total"><?php echo $Privilegy; ?></div>
	</div>
	
    <div class="fans" style="text-align:center">
	<div class="total"><?php echo $numSub; ?></div>
	</div>
	
    <div class="fans" style="text-align:center">
	<div class="total"><?php echo $numMes; ?></div>
	</div>
    
    </div>
    <?php if ($Verified != 1) { ?>
    </div>
    <?php } ?>
    
    <?
	$nr--;
}

?>

<div class="huge_box_footer">
<?php if ($totalpages > 0) { ?>
	<div class="pagination">
	<?php if ($currentpage>2) { ?>
    	<a href="?page=users&orby=<?php echo $orby; ?><? if ($search!= '') echo "&searchbox=".$search; ?>&pg=0" title="first">&nbsp;&lt;&lt;&nbsp;</a>
	<?php } ?>
	<?php if ($currentpage>1) { ?>
    	<a href="?page=users&orby=<?php echo $orby; ?><? if ($search!= '') echo "&searchbox=".$search; ?>&pg=<?php echo $pg - $displaying; ?>" title="prev">&nbsp;&lt;&nbsp;</a>
	<?php } ?>
&nbsp;&nbsp;<?php echo $currentpage."/".$totalpages; ?>&nbsp;&nbsp;
	<?php if ($currentpage<$totalpages) { ?>
    	<a href="?page=users&orby=<?php echo $orby; ?><? if ($search!= '') echo "&searchbox=".$search; ?>&pg=<?php echo ($currentpage) * $displaying; ?>" target="_self" title="next">&nbsp;&gt;&nbsp;</a>
    <?php } ?>
	<?php if ($currentpage<$totalpages-1) { ?>
    	<a href="?page=users&orby=<?php echo $orby; ?><? if ($search!= '') echo "&searchbox=".$search; ?>&pg=<?php echo ($totalpages - 1) * $displaying; ?>" target="_self" title="last">&nbsp;&gt;&gt;&nbsp;</a>
    <?php } ?>
	</div>
<?php } ?>
<div class="results">Total results: <?php echo $numresults; ?>.</div>
</div>
</div>

<?php 
include ('components/submit_user.php');
//include ('components/sign_in.php');
//include ('components/my_pages.php');

?>

</div>
