<?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 `photos_file` WHERE `id` = '".abs(intval($_GET['fid']))."'"),0) == true)
{
	$fid = abs(intval($_GET['fid']));
	$arr_fid = mysql_fetch_array(mysql_query("SELECT `aid`, `time` FROM `photos_file` WHERE `id` = '".$fid."'"));
	$n = mysql_result(mysql_query("SELECT COUNT(`id`) FROM `photos_file` WHERE `aid` = '".$arr_fid['aid']."' AND `time` > '".$arr_fid['time']."' ORDER by `time` DESC"),0);
	$n = $n+1;
	go(URL.'/photos/preview.php?id='.$arr_fid['aid'].'&n='.$n);
}
// Переход по fname
if (!empty($_GET['fname']) && mysql_result(mysql_query("SELECT COUNT(`id`) FROM `photos_file` WHERE `name` = '".check($_GET['fname'])."'"),0) == true)
{
	$fname = check($_GET['fname']);
	$arr_fname = mysql_fetch_array(mysql_query("SELECT `aid`, `time` FROM `photos_file` WHERE `name` = '".$fname."'"));
	$n = mysql_result(mysql_query("SELECT COUNT(`id`) FROM `photos_file` WHERE `aid` = '".$arr_fname['aid']."' AND `time` > '".$arr_fname['time']."' ORDER by `time` DESC"),0);
	$n = $n+1;
	go(URL.'/photos/preview.php?id='.$arr_fname['aid'].'&n='.$n);
}

if (!empty($_GET['id']) && is_numeric($_GET['id']))
{
	$id = abs(intval($_GET['id']));
	$result = mysql_query("SELECT * FROM `photos_albums` WHERE `id` = '".$id."' LIMIT 1");
	if (mysql_num_rows($result) == true)
	{
		$arr_album = mysql_fetch_array(mysql_query("SELECT * FROM `photos_albums` WHERE `id` = '".$id."' LIMIT 1"));
		$arr_user = mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE `id` = '".$arr_album['user_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">'.lang('Просмотр фотографии','Перегляд фотографії').'</div>
	<div class="block"><center>';

	$count_photos = mysql_result(mysql_query("SELECT COUNT(`id`) FROM `photos_file` WHERE `aid` = '".$arr_album['id']."'"),0);
	$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 `photos_file` WHERE `aid` = '".$arr_album['id']."' ORDER by `time` DESC LIMIT $start,1");
		while ($arr_photo = mysql_fetch_array($query))
		{
			echo '<a href="'.URL.'/files/photos/'.$arr_user['id'].'/'.$arr_photo['name'].'"><img class="preview" src="'.URL.'/files/photos/'.$arr_user['id'].'/preview/'.$arr_photo['name'].'"/></a>';
		
			echo '</center>
			<p><center>';
			p($n,$count_photos,URL.'/photos/preview.php?id='.$id.'&');
			echo ' | <b>'.$n.' '.lang('из','з').' '.$count_photos.'</b> |';
			n($n,$count_photos,URL.'/photos/preview.php?id='.$id.'&');
			echo'</center></p></div>';


			echo '<div class="title">';
			if (!empty($user_id) && $user_id != $arr_user['id'])
			{
				if (!in_array($user_id, explode("||", substr($arr_photo['ulike'], 1, -1)))) echo '<div id="like_'.$arr_photo['id'].'"><a href="'.URL.'/photos/like.php?id='.$arr_photo['id'].'" onclick="Like.Go(this.href,'.$arr_photo['id'].'); return false"><img class="ico" src="'.URL.'/design/img/like.png"/>'.lang('Мне нравиться','Мені подобається').' <b>'.$arr_photo['clike'].'</b></a></div>'; else echo '<div id="like_'.$arr_photo['id'].'"><a href="'.URL.'/photos/like.php?id='.$arr_photo['id'].'" onclick="Like.Go(this.href,'.$arr_photo['id'].'); return false"><img class="ico" src="'.URL.'/design/img/like_ok.png"/>'.lang('Мне нравиться','Мені подобається').' <b>'.$arr_photo['clike'].'</b></a></div>';
			}
			else echo '<a href="'.URL.'/photos/show_like.php?id='.$arr_photo['id'].'" onclick="Page.Go(this.href); return false"><img class="ico" src="'.URL.'/design/img/like.png"/>'.lang('Мне нравиться','Мені подобається').' <b>'.$arr_photo['clike'].'</b></a>';
			echo '</div>
			<div class="block">
			Фотоальбом: <a href="'.URL.'/photos/album.php?id='.$arr_album['id'].'" onclick="Page.Go(this.href); return false">'.$arr_album['name'].'</a><br/>
			'.lang('Добавлено','Додана').': '.vtime($arr_photo['time']).'</div>';
			if (!empty($user_id) && $user_id == $arr_user['id'])
			{ 
				echo '<div class="block">';
				if ($user['avatar'] != $arr_photo['name']) echo '<a href="'.URL.'/photos/preview.php?fid='.$arr_photo['id'].'&update='.$arr_photo['id'].'" onclick="Page.Go(this.href); return false">'.lang('Установить','Встановити').' на аватарку</a><br/>';
				echo '<a href="'.URL.'/photos/album.php?id='.$arr_album['id'].'&del_photo='.$arr_photo['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.'/photos/preview.php?id='.$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_photo['comm'].'</div></div>';
		
			$all = mysql_result(mysql_query("SELECT COUNT(`id`) FROM `photos_comm` WHERE `fid` = '".$arr_photo['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 `photos_comm` WHERE `fid` = '".$arr_photo['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.'/photos/preview.php?id='.$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.'/photos/preview.php?id='.$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.'/photos/preview.php?id='.$id.'&n='.$n.'&');
			}
			echo '</div></div>';
		}

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

?>