<?php

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

require_once 'handler/update.php'; // Оновлення аватарки

// Переход по fid
if (!empty($_GET['fid']) && is_numeric($_GET['fid']) && mysql_result(mysql_query("SELECT COUNT(`id`) FROM `avatars` WHERE `id` = '".abs(intval($_GET['fid']))."'"),0) == true)
{
	$fid = abs(intval($_GET['fid']));
	$arr_fid = mysql_fetch_array(mysql_query("SELECT `id`, `user_id`, `time` FROM `avatars` WHERE `id` = '".$fid."'"));
	$n = mysql_result(mysql_query("SELECT COUNT(`id`) FROM `avatars` WHERE `user_id` = '".$arr_fid['user_id']."' AND `time` > '".$arr_fid['time']."' ORDER by `time` DESC"),0);
	$n = $n+1;
	go(URL.'/avatars/preview_modal.php?id='.$arr_fid['user_id'].'&n='.$n);
}
// Переход по fname
if (!empty($_GET['fname']) && mysql_result(mysql_query("SELECT COUNT(`id`) FROM `avatars` WHERE `name` = '".check($_GET['fname'])."'"),0) == true)
{
	$fname = check($_GET['fname']);
	$arr_fname = mysql_fetch_array(mysql_query("SELECT * FROM `avatars` WHERE `name` = '".$fname."'"));
	$n = mysql_result(mysql_query("SELECT COUNT(`id`) FROM `avatars` WHERE `user_id` = '".$arr_fname['user_id']."' AND `time` > '".$arr_fname['time']."' ORDER by `time` DESC"),0);
	$n = $n+1;
	go(URL.'/avatars/preview_modal.php?id='.$arr_fname['user_id'].'&n='.$n);
}

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.'/');
	}
}
else
{
	go(URL.'/');
}

$title = $arr_user['name'].' '.$arr_user['surname'].' :: '.lang('Просмотр','Перегляд').' аватарки';
require_once '../incfiles/head.php';

echo user_access_page($arr_user);
echo '<div class="title"><div class="for_pc right"><a href="" onclick="Window.Close(); return false"><img src="'.URL.'/design/img/del.png"/></a></div><a class="title_a_hover" href="'.URL.'/?id='.$id.'" onclick="Page.Go(this.href); return false">'.$arr_user['name'].' '.$arr_user['surname'].'</a></div>';

echo '<div class="title">'.lang('Просмотр','Перегляд').' аватарки</div>';

	$count_photos = $arr_user['count_avatars'];
	$n = (isset($_GET['n']) and is_numeric($_GET['n']) and $_GET['n']>1 and $_GET['n']<=$count_photos) ? $_GET['n'] : 1;
	$start = $n*1-1;

	$query = mysql_query("SELECT * FROM `avatars` WHERE `user_id` = '".$arr_user['id']."' ORDER by `time` DESC LIMIT $start,1");
		while ($arr_ava = mysql_fetch_array($query))
		{

			echo '<div class="block">
			<center><a href="'.URL.'/files/avatars/'.$arr_user['id'].'/'.$arr_ava['name'].'"><img class="preview" src="'.URL.'/files/avatars/'.$arr_user['id'].'/preview/'.$arr_ava['name'].'"/></a></center>';
		
			echo '<p><center>';
			p($n,$count_photos,URL.'/avatars/preview_modal.php?id='.$id.'&');
			echo ' | <b>'.$n.' '.lang('из','з').' '.$count_photos.'</b> |';
			n($n,$count_photos,URL.'/avatars/preview_modal.php?id='.$id.'&' );
			echo'</center></p>';


			echo '<div class="post_panel">';
			echo''.lang('Мне нравится','Мені подобається').'';
			echo  like('avatars',$arr_ava['id'],$arr_ava['ulike'],$arr_ava['clike'],$arr_ava['user_id']);
			echo '</div>
			</div>';
			
	
mysql_query("UPDATE `users` SET `balans` = '".($user['balans']-0)."' WHERE `id` = '$user[id]' LIMIT 1");
mysql_query("UPDATE `avatars_rating` SET `rating` = '".($arr_ava['rating']+1)."' WHERE `id` = '$arr_ava[id]' LIMIT 1",$db);
mysql_query("INSERT INTO `avatars_rating` (`user_id`, `fid`) values('$user[id]', '$fid[id]')",$db);
			

			
			echo'<div class="block">Фотоальбом: <a href="'.URL.'/avatars/?id='.$arr_user['id'].'" onclick="Page.Go(this.href); return false;">Аватарки</a><br/>'.lang('Добавлено','Додана').': '.vtime($arr_ava['time']).'</div>';
			if (!empty($user_id) && $user_id == $arr_user['id'])
			{ 
				echo '<div class="title">';
				if ($user['avatar'] != $arr_ava['name']) echo '<a href="'.URL.'/avatars/preview_modal.php?fid='.$arr_ava['id'].'&update='.$arr_ava['id'].'" onclick="Page.Go(this.href); return false">'.lang('Установить','Встановити').' на аватарку</a> ';
				echo '<a href="'.URL.'/avatars/?id='.$arr_user['id'].'&del_avatar='.$arr_ava['id'].'" onclick="Page.Go(this.href); return false">'.lang('Удалить','Видалити').'</a></div>';
			}
			
			if (!empty($user_id))
			{
			?>
<script type="text/javascript">
$(function(){
	$('#form').on('submit', function(e){
    e.preventDefault();
    var $that = $(this),
        formData = new FormData($that.get(0));
    $.ajax({
      url: $that.attr('action'),
      type: $that.attr('method'),
      contentType: false,
      processData: false,
      data: formData,
      success: function(response){
        if(response){
 			$('#content_comm').load($that.attr('action')+ '#content_comm .comm');
			$('#textarea').val('');
       }
     }
    });
  });
});

$(function(){
	$('#form_re_comm').on('submit', function(e){
    e.preventDefault();
    var $that = $(this),
        formData = new FormData($that.get(0));
    $.ajax({
      url: $that.attr('action'),
      type: $that.attr('method'),
      contentType: false,
      processData: false,
      data: formData,
      success: function(response){
        if(response){
 			Page.Go($that.attr('action'));
       }
     }
    });
  });
});
</script>

<?
			
			require_once 'handler/del_comm.php';
			require_once 'handler/comm.php';
			
			echo '<div class="block2">
				<form id="form" method="post" action="'.URL.'/avatars/preview_modal.php?id='.$arr_user['id'].'&n='.$n.'">
				<textarea id="textarea" class="input" name="comm" rows="4"></textarea>
				<input class="button2" type="submit" value="'.lang('Написать','Написати').'"/>
				<a class="button3" href="'.URL.'/ajax/smiles.php" onclick="FormContent.Smiles(this.href); return false"><img src="'.URL.'/design/img/smile.png"/></a>
				</form>
				</div>
				<div id="form_content"></div>';
			}

			echo '<div id="content_comm"><div class="comm">
			<div class="title">'.lang('Комментарии','Коментарі').' <div class="right">'.$arr_ava['comm'].'</div></div>';
		
			$all = mysql_result(mysql_query("SELECT COUNT(`id`) FROM `avatars_comm` WHERE `fid` = '".$arr_ava['id']."' LIMIT 1"),0);
			if ($all == true)
			{
				$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;

				$arr_comm = mysql_query("SELECT * FROM `avatars_comm` WHERE `fid` = '".$arr_ava['id']."' ORDER by `time` DESC LIMIT $start,10");
				while ($arr = mysql_fetch_array($arr_comm))
				{
					echo '<div class="block">';
					if (!empty($user_id) && $user_id == $arr_user['id'] or !empty($user_id) && $user_id == $arr['user_id']) echo '<div class="right"><a href="'.URL.'/avatars/preview_modal.php?id='.$arr_user['id'].'&n='.$n.'&p='.$p.'&del_comm='.$arr['id'].'" onclick="Form.delPost(this.href); return false"><img src="'.URL.'/design/img/closed.png"/></a></div>';
					echo user_name($arr['user_id']).'<br/>'.vtime($arr['time']).'<ul>';
					if (!empty($arr['quote_id'])) echo '<div class="quote">'.user_name($arr['quote_id']).' '.lang('пишет','пише').':<ul>'.smiles(text($arr['quote'])).'</ul></div>';
					echo smiles(text($arr['text'])).'</ul>';
					if (!empty($user_id) && $user_id != $arr['user_id'])
					{
						echo '<ul><a class="link_re_comm" id="link_re_comm_'.$arr['id'].'" href="" onclick="Form.reComm('.$arr['id'].'); return false">'.lang('Ответить','Відповісти').'</a></ul>
						<div class="re_comm" id="re_comm_'.$arr['id'].'">
						<form id="form_re_comm" method="post" action="'.URL.'/avatars/preview_modal.php?id='.$arr_user['id'].'&n='.$n.'">
						<textarea class="input" name="re_comm" rows="4"></textarea>
						<input type="hidden" name="re_id" value="'.$arr['id'].'"/>
						<input class="button2" type="submit" value="'.lang('Написать','Написати').'"/>
						</form>
						</div>';
					}
					echo '</div>';
				}
			}
			navi($p,$cp,''.URL.'/avatars/preview_modal.php?id='.$arr_user['id'].'&n='.$n.'&');
			echo '</div></div>';
		}

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

?>