<?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 `groups_wall` WHERE `id` = '".$id."' LIMIT 1");
	if (mysql_num_rows($result) == true)
	{
		$arr_post = mysql_fetch_array(mysql_query("SELECT * FROM `groups_wall` WHERE `id` = '".$id."' LIMIT 1"));
		$arr_group = mysql_fetch_array(mysql_query("SELECT * FROM `groups` WHERE `id` = '".$arr_post['wall_id']."' LIMIT 1"));
		if (in_array($user_id, explode("||", substr($arr_group['blacklist'], 1, -1)))) {go(URL.'/groups/?id='.$arr_group['id']);}
	}
	else go(URL.'/');
}
else go(URL.'/');

$title = checkout($arr_group['name']).' :: '.lang('Просмотр записи','Перегляд запису');
require_once '../../incfiles/head.php';
echo group_access($arr_group);

	echo '<div class="title">
	<a class="title_a_hover" href="'.URL.'/groups/?id='.$arr_group['id'].'" onclick="Page.Go(this.href); return false">'.$arr_group['name'].'</a>
	</div>
	<div class="block">';
	
if ($arr_post['type'] == 'update_avatar') go(URL.'/groups/avatars/preview.php?fname='.$arr_post['photos']);

else if ($arr_post['type'] == 'repost')
{
	echo group_wall_post($arr_post,2);
}
else
{
	echo ($arr_post['from_group']>0?group_name($arr_post['wall_id'],1):user_name($arr_post['user_id'])).'<ul>'.vtime($arr_post['time']).'</ul>';
	if (!empty($arr_post['text'])) echo '<p><ul>'.smiles(hastag(($_GET['text']=='full'?$arr_post['text']:cut($arr_post['text'],URL.'/groups/wall/post.php?id='.$arr_post['id'].'&text=full')))).'</ul></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/'.($arr_post['from_group']>0?'groups/wall/'.$arr_post['wall_id']:'wall/'.$arr_post['user_id']).'/photos/'.$q[0])) echo '<center><img class="preview" src="'.URL.'/files/'.($arr_post['from_group']>0?'groups/wall/'.$arr_post['wall_id']:'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/'.($arr_post['from_group']>0?'groups/wall/'.$arr_post['wall_id']:'wall/'.$arr_post['user_id']).'/photos/'.$q[($n-1)])) echo '<center><a href="'.URL.'/groups/wall/post.php?id='.$id.'" onclick="Page.Go(this.href); return false"><img class="preview" src="'.URL.'/files/'.($arr_post['from_group']>0?'groups/wall/'.$arr_post['wall_id']:'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.'/groups/wall/post.php?id='.$id.'&');
			echo ' | <b>'.$n.' '.lang('из','з').' '.$count.'</b> |';
			n($n,$count,URL.'/groups/wall/post.php?id='.$id.'&');
			echo'</center></p>';
		}
		else
		{
			echo '<center>';
			for ($i = 0; $i < $count; $i++)
			{
			if (file_exists(HOME .'/files/'.($arr_post['from_group']>0?'groups/wall/'.$arr_post['wall_id']:'wall/'.$arr_post['user_id']).'/photos/'.$q[$i])) echo '<a href="'.URL.'/groups/wall/post.php?id='.$id.'&n='.($i+1).'" onclick="Page.Go(this.href); return false"><img class="preview_mini" src="'.URL.'/files/'.($arr_post['from_group']>0?'groups/wall/'.$arr_post['wall_id']:'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 group_repost('wall',$id,$arr_post['urepost'],$arr_post['crepost'],$arr_post['user_id']);
	echo group_like('wall',$id,$arr_post['ulike'],$arr_post['clike'],$arr_post['user_id']);
	echo '</div>';

}
	echo '</div>';


if (!empty($user_id) && $arr_group['access_comm'] == 0 || !empty($user_id) && $arr_group['access_comm'] == 1 && in_array($user_id, explode("||", substr($arr_group['users'], 1, -1))))
{
			?>
<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.'/groups/wall/post.php?id='.$id;?>');
       }
     }
    });
  });
});
</script>

<?
			
if (!empty($user_id) && !in_array($user_id, explode("||", substr($arr_group['banlist'], 1, -1))))
{
	require_once 'handler/del_comm.php';
	require_once 'handler/comm.php';
}
	
	if (!empty($user_id))
	{
		if ($user_id == $arr_post['user_id'] || in_array($user_id, explode("||", substr($arr_group['admins'], 1, -1))) || !empty($user_id) && $arr_post['from_group'] == 0 && in_array($user_id, explode("||", substr($arr_group['moders'], 1, -1))))
		{
			echo '<div class="title">
			<a href="'.URL.'/groups/wall/show_like.php?id='.$id.'" onclick="Page.Go(this.href); return false">'.lang('Понравилось','Сподобалося').' <b>'.$arr_post['clike'].'</b></a>
			<a href="'.URL.'/groups/wall/show_repost.php?id='.$id.'" onclick="Page.Go(this.href); return false">'.lang('Поделились','Поділилися').' <b>'.$arr_post['crepost'].'</b></a>';
			echo '</div>';
		}
		if (in_array($user_id, explode("||", substr($arr_group['admins'], 1, -1))) || $arr_post['from_group'] == 0 && in_array($user_id, explode("||", substr($arr_group['moders'], 1, -1))))
		{
			echo '<div class="title">';
			if ($arr_post['type'] != 'update_avatar') echo '<a href="'.URL.'/groups/wall/edit_post.php?id='.$id.'" onclick="Page.Go(this.href); return false">'.lang('Редактировать запись','Редагувати запис').'</a> ';
			echo '<a href="'.URL.'/groups/?id='.$arr_group['id'].'&del_post='.$id.'" onclick="Page.Go(this.href); return false">'.lang('Удалить запись','Видалити запис').'</a>';
			echo '</div>';
		}
	}
	
	echo '<div class="block2">
	<form id="form" method="post" action="'.URL.'/groups/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">';
	if ($arr_group['access_comm'] != 2 || $arr_post['comm'] > 0) echo '<div class="title">'.lang('Комментарии','Коментарі').' <div class="right">'.$arr_post['comm'].'</div></div>';
		
	$all = mysql_result(mysql_query("SELECT COUNT(`id`) FROM `groups_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;

		if (in_array($user_id, explode("||", substr($arr_group['admins'], 1, -1)))) $access_user = 1;
		else if (in_array($user_id, explode("||", substr($arr_group['moders'], 1, -1)))) $access_user = 2;

	$arr_comm = mysql_query("SELECT * FROM `groups_wall_comm` WHERE `pid` = '".$id."' ORDER by `time` DESC LIMIT $start,10");
		while ($arr = mysql_fetch_array($arr_comm))
		{
			echo '<div class="block">';
			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(hastag(text($arr['quote']))).'</ul></div></ul>';
			echo '<ul>'.smiles(hastag(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.'/groups/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 '<a id="block_menu_'.$arr['id'].'" href="" onclick="BlockMenu.Open('.$arr['id'].'); return false"><img id="block_menu_'.$arr['id'].'" src="'.URL.'/design/img/menu_show.png"/></a></div>';
			if ($user_id == $arr_post['user_id'] || $user_id == $arr['user_id'] || $access_user > 0)
			{
				echo '<div id="block_menu_'.$arr['id'].'" class="title display_none">
				<a class="title_a_hover" id="bg_orange" href="'.URL.'/groups/wall/post.php?id='.$id.'&del_comm='.$arr['id'].'" onclick="Form.delPost(this.href); return false">'.lang('Удалить','Видалити').'</a>';
				if ($user_id != $arr['user_id'] && $access_user > 0 && !in_array($arr['user_id'], explode("||", substr($arr_group['admins'], 1, -1))) && !in_array($arr['user_id'], explode("||", substr($arr_group['moders'], 1, -1))) && !in_array($arr['user_id'], explode("||", substr($arr_group['banlist'], 1, -1)))) echo ' <a class="title_a_hover" id="bg_red" href="'.URL.'/groups/adm/ban.php?id='.$arr_group['id'].'&ban_id='.$arr['user_id'].'" onclick="Page.Go(this.href); return false">'.lang('Заблокировать','Заблокувати').'</a>';
				if ($user_id != $arr['user_id'] && $access_user == 1 && !in_array($arr['user_id'], explode("||", substr($arr_group['blacklist'], 1, -1)))) echo ' <a class="title_a_hover" id="bg_black" href="'.URL.'/groups/adm/users.php?id='.$arr_group['id'].'&blacklist='.$arr['user_id'].'" onclick="Page.Go(this.href); return false">'.lang('Черный список','Чорний список').'</a>';
				echo '</div>';
			}
		}
		navi($p,$cp,''.URL.'/groups/wall/post.php?id='.$id.'&');
	}
	echo '</div></div>';

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

?>