<?php

require_once '../incfiles/core.php';
require_once '../incfiles/func.php';
require_once '../incfiles/auth.php';
require_once '../incfiles/user.php';

if (empty($user_id)) go(URL);

//$uid = 263;

if (!empty($_GET['id']) && is_numeric($_GET['id'])) // Діалог
{

	$id = abs(intval($_GET['id']));	
	$result = mysql_query("SELECT * FROM `users` WHERE `id` = '".$id."' LIMIT 1");
	if (mysql_num_rows($result) == true) $arr_user = mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE `id` = '".$id."' LIMIT 1"));
	else go(URL);

	$res = mysql_query("SELECT * FROM `mail_dialog` WHERE `for_id` = '".$uid."' AND `from_id` = '".$id."' AND `del` != '".$uid."' LIMIT 1");
	if (mysql_num_rows($res) == false) go(URL.'/mail/');
	$arr_dialog = mysql_fetch_array($res);

	$index = true;
	$title = lang('Диалог с','Діалог з').' '.$arr_user['name'].' '.$arr_user['surname'];
	require_once '../incfiles/head.php';


	echo '<div class="title">
	<a href="'.URL.'/mail.php" onclick="Page.Go(this.href); return false">'.lang('Диалоги','Діалоги').'</a>
	<a class="title_a_hover" href="'.URL.'/ajax/mail.php?id='.$id.'" onclick="Title.Open(); return false">'.$arr_user['name'].' '.$arr_user['surname'].' <img src="'.URL.'/design/img/title_menu.png"/></a>
	</div>
	<div id="title_menu">
	<a href="'.URL.'/?id='.$id.'" onclick="Page.Go(this.href); return false">'.$arr_user['name'].' '.$arr_user['surname'].'</a>
	</div>
	'.($arr_user['online']==0?'<div class="block" style="color:#777777;">'.$arr_user['name'].' '.user_visit($arr_user['sex'],$arr_user['visit']).'</div>':'');
	
	$all = mysql_result(mysql_query("SELECT COUNT(`id`) FROM `mail_msg` WHERE `for_id` = '".$uid."' AND `from_id` = '".$id."' AND `del` != '".$uid."' or `for_id` = '".$id."' AND `from_id` = '".$uid."' AND `del` != '".$uid."'"),0);
	if ($all > 0)
	{
		$cp = ceil($all/10);
		$p = (isset($_GET['p']) and is_numeric($_GET['p']) and $_GET['p']>1 and $_GET['p']<=$cp) ? $_GET['p'] : 1;
		$start = $p*10-10;
		$res = mysql_query("SELECT * FROM `mail_msg` WHERE `for_id` = '".$uid."' AND `from_id` = '".$id."' AND `del` != '".$uid."' or `for_id` = '".$id."' AND `from_id` = '".$uid."' AND `del` != '".$uid."' ORDER by `time` DESC LIMIT $start,10");
		while ($arr = mysql_fetch_array($res))
		{
			echo ($arr['np'] == 0?'<div class="block">':'<div class="block1">').''.user_name($arr['from_id'],0,0).'<div class="right"><a href="'.URL.'/mail/msg.php?id='.$arr['id'].'" onclick="Page.Go(this.href); return false;">'.vtime($arr['time']).'</a></div><ul>'.smiles($arr['text']).'</ul>';
			if (!empty($arr['photos']))
			{
				echo '<p>';
				$q = explode("||", substr($arr['photos'], 1, -1));
				$count = count($q);
				for ($i = 0; $i < $count; $i++) echo '<a href="'.URL.'/files/mail/'.$arr_dialog['dir'].'/photos/'.$q[$i].'"><img class="'.($count>1?'preview_mini':'preview').'" src="'.URL.'/files/mail/'.$arr_dialog['dir'].'/photos/'.($count>1?'mini':'preview').'/'.$q[$i].'"/></a>';
				echo '</p>';
			}
			echo '</div>';
		}
		
		navi($p,$cp,'/ajax/mail.php?id='.$id.'&');

	}

	echo '</span></div>';

	
}
else // Діалоги
{


	
	$title = lang('Мои Сообщения','Мої Повідомлення');
	require_once '../incfiles/head.php';

	echo '<div class="title">'.lang('Диалоги','Діалоги').'</div>';

	$all = mysql_result(mysql_query("SELECT COUNT(`id`) FROM `mail_dialog` WHERE `for_id` = '".$uid."' AND `del` != '".$uid."'"),0);
	if ($all > 0)
	{
		$cp = ceil($all/10);
		$p = (isset($_GET['p']) and is_numeric($_GET['p']) and $_GET['p']>1 and $_GET['p']<=$cp) ? $_GET['p'] : 1;
		$start = $p*10-10;

		$res = mysql_query("SELECT * FROM `mail_dialog` WHERE `for_id` = '".$uid."' AND `del` != '".$uid."' ORDER by `time` DESC LIMIT $start,10");
		while ($arr = mysql_fetch_array($res))
		{
			echo '<a style="text-decoration:none;" href="'.URL.'/ajax/mail.php?id='.$arr['from_id'].'" onclick="Page.Go(this.href); return false;">'.($arr['np']>0?'<div class="block1">':'<div class="block">').'<font color="#4C6B8F">'.user_name2($arr['from_id']).'</font><div class="right">'.vtime($arr['time']).'</div><ul style="color:#000000;">'.user_ava($arr['from'],16).' '.smiles(mb_strlen($arr['text'])>99?$arr['text'].'...':$arr['text']).'<div class="right">'.($arr['count']>0?'<span style="background:rgba(0,0,0,0.2);color:#FFFFFF;padding:3px 6px 3px 6px;border-radius:3px;">'.$arr['count'].'</span>':'').'</div></ul></div></a>';
		}
	}

	navi($p,$cp,'/ajax/mail.php?');
	
	
	
}

require_once '../incfiles/foot.php';

?>