<?php

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

if (!empty($_GET['id']) && is_numeric($_GET['id']))
{
	$id = abs(intval($_GET['id']));
	$result = mysql_query("SELECT * FROM `wall` WHERE `id` = '".$id."' LIMIT 1");
	if (mysql_num_rows($result) == true)
	{
		$arr_post = mysql_fetch_array(mysql_query("SELECT * FROM `wall` WHERE `id` = '".$id."' LIMIT 1"));
		$arr_user = mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE `id` = '".$arr_post['wall_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">
	<a class="title_a_hover" href="'.URL.'/?id='.$arr_user['id'].'" onclick="Page.Go(this.href); return false">'.$arr_user['name'].' '.$arr_user['surname'].'</a>
	</div>
	<div class="block">';
	
if ($arr_post['type'] == 'update_avatar') go(URL.'/avatars/preview.php?fname='.$arr_post['photos']);

else if ($arr_post['type'] == 'repost' || $arr_post['type'] == 'group_repost')
{
	echo wall_post($arr_post,2);
}
else
{
	echo user_name($arr_post['user_id']).'<ul>'.vtime($arr_post['time']).'</ul>';
	if (!empty($arr_post['text'])) echo '<p><ul>'.smiles(links(($_GET['text']=='full'?$arr_post['text']:cut($arr_post['text'],URL.'/wall/post.php?id='.$arr_post['id'].'&text=full')))).'</ul></center></p>';
	
	if (!empty($arr_post['photos']))
	{
		echo '<p>';
		$q = explode("||", substr($arr_post['photos'], 1, -1));
		$count = count($q);
		$n = (isset($_GET['n']) and is_numeric($_GET['n']) and $_GET['n']>1 and $_GET['n']<=$count) ? $_GET['n'] : 1;
		if ($count == 1)
		{
			if (file_exists(HOME .'/files/wall/'.$arr_post['user_id'].'/photos/'.$q[0])) echo '<center><img class="preview" src="'.URL.'/files/wall/'.$arr_post['user_id'].'/photos/preview/'.$q[0].'"/></center>'; else echo '<center><img class="preview_mini" src="'.URL.'/design/img/image_not_found.png"/></center>';
		}
		else if ($count > 1 && !empty($_GET['n']))
		{	
			if (file_exists(HOME .'/files/wall/'.$arr_post['user_id'].'/photos/'.$q[($n-1)])) echo '<center><a href="'.URL.'/wall/post.php?id='.$id.'" onclick="Page.Go(this.href); return false"><img class="preview" src="'.URL.'/files/wall/'.$arr_post['user_id'].'/photos/preview/'.$q[($n-1)].'"/></a></center>'; else echo '<center><img class="preview_mini" src="'.URL.'/design/img/image_not_found.png"/></center>';

			echo '<p><center>';
			p($n,$count,URL.'/wall/post.php?id='.$id.'&');
			echo ' | <b>'.$n.' '.lang('из','з').' '.$count.'</b> |';
			n($n,$count,URL.'/wall/post.php?id='.$id.'&');
			echo'</center></p>';
		}
		else
		{
			echo '<center>';
			for ($i = 0; $i < $count; $i++)
			{
			if (file_exists(HOME .'/files/wall/'.$arr_post['user_id'].'/photos/'.$q[$i])) echo '<a href="'.URL.'/wall/post.php?id='.$id.'&n='.($i+1).'" onclick="Page.Go(this.href); return false"><img class="preview_mini" src="'.URL.'/files/wall/'.$arr_post['user_id'].'/photos/mini/'.$q[$i].'"/></a>'; else echo '<img class="preview_mini" src="'.URL.'/design/img/image_not_found.png"/>';
			}
			echo '</center>';
		}
		echo '</p>';
	}
	if (!empty($arr_post['audio']))
	{
		echo '<ul><p>';
		$q = explode("||", substr($arr_post['audio'], 1, -1));
		$count = count($q);
		for ($i = 0; $i < $count; $i++)
		{
			echo '<p>'.track($q[$i]).'</p>';
		}
		echo '</p></ul>';
	}

	echo '<div class="post_panel">';
	echo repost('wall',$id,$arr_post['urepost'],$arr_post['crepost'],$arr_post['user_id']);
	echo like('wall',$id,$arr_post['ulike'],$arr_post['clike'],$arr_post['user_id']);
	echo '</div>';

}
	echo '</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('<?echo URL.'/wall/post.php?id='.$id;?>');
       }
     }
    });
  });
});
</script>

<?
			
	require_once 'handler/del_comm.php';
	require_once 'handler/comm.php';
	
	
	if ($user_id == $arr_post['user_id'] || $user_id == $arr_post['wall_id'])
	{
		echo '<div class="title">';
		if ($user_id == $arr_post['user_id'] && $arr_post['type'] != 'update_avatar') echo '<a href="'.URL.'/wall/edit_post.php?id='.$id.'" onclick="Page.Go(this.href); return false">'.lang('Редактировать запись','Редагувати запис').'</a> ';
		echo '<a href="'.URL.'/?id='.$arr_post['wall_id'].'&del_post='.$id.'" onclick="Page.Go(this.href); return false">'.lang('Удалить запись','Видалити запис').'</a>
		</div>';
	}
	
	echo '<div class="block2">
	<form id="form" method="post" action="'.URL.'/wall/post.php?id='.$id.'">
	<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_post['comm'].'</div></div>';
		
	$all = mysql_result(mysql_query("SELECT COUNT(`id`) FROM `wall_comm` WHERE `pid` = '".$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 `wall_comm` WHERE `pid` = '".$id."' ORDER by `time` DESC LIMIT $start,10");
		while ($arr = mysql_fetch_array($arr_comm))
		{
			echo '<div class="block">';
			if (!empty($user_id)) if ($user_id == $arr_post['wall_id'] || $user_id == $arr_post['user_id'] || $user_id == $arr['user_id']) echo '<div class="right"><a href="'.URL.'/wall/post.php?id='.$id.'&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']).'<ul>'.vtime($arr['time']).'</ul>';
			if (!empty($arr['quote_id'])) echo '<ul><div class="quote">'.user_name($arr['quote_id']).' '.lang('пишет','пише').':<ul>'.smiles(text(hastag($arr['quote']))).'</ul></div></ul>';
			echo '<ul>'.smiles(text(hastag($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.'/wall/post.php?id='.$id.'">
				<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.'/wall/post.php?id='.$id.'&');
	}
	echo '</div></div>';

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

?>