<?
include_once 'sys/inc/start.php';
include_once 'sys/inc/compress.php';
include_once 'sys/inc/sess.php';
include_once 'sys/inc/home.php';
include_once 'sys/inc/settings.php';
include_once 'sys/inc/db_connect.php';
include_once 'sys/inc/ipua.php';
include_once 'sys/inc/fnc.php';
include_once 'sys/inc/user.php';
only_reg(); 
$set['title']='Новые сообщения';
include_once 'sys/inc/thead.php';
title();


$k_post=mysql_result(mysql_query("SELECT COUNT(DISTINCT `mail`.`id_user`) FROM `mail`
 LEFT JOIN `users_konts` ON `mail`.`id_user` = `users_konts`.`id_kont` AND `users_konts`.`id_user` = '$user[id]'
 WHERE `mail`.`id_kont` = '$user[id]' AND (`users_konts`.`type` IS NULL OR `users_konts`.`type` = 'common' OR `users_konts`.`type` = 'favorite') AND `mail`.`read` = '0'"),0);


$k_page=k_page($k_post,$set['p_str']);
$page=page($k_page);
$start=$set['p_str']*$page-$set['p_str'];
if ($k_post==0)
{
?>
<div class='noy'>Нет новых сообщений</div>
<?
}
else
{
$q=mysql_query("SELECT MAX(`mail`.`time`) AS `last_time`, COUNT(`mail`.`id`) AS `count`, `mail`.`id_user`, `users_konts`.`name` FROM `mail`
 LEFT JOIN `users_konts` ON `mail`.`id_user` = `users_konts`.`id_kont` AND `users_konts`.`id_user` = '$user[id]'
 WHERE `mail`.`id_kont` = '$user[id]' AND (`users_konts`.`type` IS NULL  OR `users_konts`.`type` = 'common' OR `users_konts`.`type` = 'favorite') AND `mail`.`read` = '0'
  GROUP BY `mail`.`id_user` ORDER BY `count` DESC LIMIT $start, $set[p_str]");
//echo mysql_error(),"<br />\n";
while ($kont = mysql_fetch_assoc($q))
{
$ank=get_user($kont['id_user']);
if($num==1){
echo "<div class='enk_div'>\n";
$num=0;
}else{
echo "<div class='enk2_div'>\n";
$num=1;}
if ($ank){
echo "".avatar2($ank['id'])."<br />\n";
echo "".on($ank['id'])."\n";
if ($ank['id']!=0){echo "".status($ank['id'])."";}
echo "<a href='/mail.php?id=$ank[id]'>".($kont['name']?$kont['name']:$ank['nick'])."</a>\n";
echo "".medal($ank_kont['id'])."";
echo "(+$kont[count]) (".vremja($kont['last_time']).")<br />";
}else{
echo "<a href='/mail.php?id=$ank[id]'>[DELETED] (+$kont[count])\n";
echo "<br />\n";
echo "Отправлено: ".vremja($kont['last_time'])."<br />\n";
}

echo "</div>\n";
}
}





if ($k_page>1)str('?',$k_page,$page); // Вывод страниц

echo "<div class='fyt'>\n";
echo "• <a href='/konts.php?$passgen'>Список контактов</a><br />\n";
if(isset($_SESSION['refer']) && $_SESSION['refer']!=NULL && otkuda($_SESSION['refer']))
echo "• <a href='$_SESSION[refer]'>".otkuda($_SESSION['refer'])."</a><br />\n";
echo "• <a href='umenu.php'>Мое меню</a><br />\n";
echo "</div>\n";
include_once 'sys/inc/tfoot.php';
?>