<?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);

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($result);
		if ($arr_post['type'] == 'update_avatar') go(URL.'/groups/avatars/preview.php?fname='.$arr_post['photos']);
		$arr_group = mysql_fetch_array(mysql_query("SELECT * FROM `groups` WHERE `id` = '".$arr_post['wall_id']."' LIMIT 1"));
		if ($user_id != $arr_post['user_id'] && !in_array($user_id, explode("||", substr($arr_group['admins'], 1, -1)))) go(URL);
	}
	else go(URL);
}
else go(URL);

$title = lang('Редактирование записи','Редагування запису');
require_once '../../incfiles/head.php';
echo group_access($arr_group);
require_once 'handler/edit_post.php';

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

<?
	echo '<div class="title">'.$title.'</div>
	<div class="block2">
	<form id="form" method="post" action="'.URL.'/groups/wall/edit_post.php?id='.$id.'">
	<textarea id="textarea" class="input" name="text" rows="4">'.no_tags($arr_post['text']).'</textarea>
	<p><div id="cache_photos">';
	if (!empty($arr_post['photos']))
	{
		$q = explode("||", substr($arr_post['photos'], 1, -1));
		$count = count($q);
		for ($i = 0; $i < $count; $i++)
		{
			echo '<a id="'.($i+1).'" class="del_cache" href="'.URL.'/groups/wall/edit_post.php?id='.$id.'&del_photo='.$q[$i].'"&from='.$arr_group['id'].' onclick="FormContent.DelCache(this.id,this.href); return false">'.(file_exists(HOME .'/files/'.($arr_post['from_group']>0?'groups/wall/'.$arr_post['wall_id']:'wall/'.$arr_post['user_id']).'/photos/'.$q[$i])?'<img class="preview_icons" src="'.URL.'/files/'.($arr_post['from_group']>0?'groups/wall/'.$arr_post['wall_id']:'wall/'.$arr_post['user_id']).'/photos/icons/'.$q[$i].'"/>':'<img style="width:50px;" class="preview_icons" src="'.URL.'/design/img/image_not_found.png"/>').'</a>';
		}
	}	
	echo '</div></p>';
	if (!empty($arr_post['audio']))
	{
		$q = explode("||", substr($arr_post['audio'], 1, -1));
		$count = count($q);
		for ($i = 0; $i < $count; $i++)
		{
			echo '<div id="'.($i+1).'"><p>';
			echo track($q[$i]);
			echo ' <a id="'.($i+1).'" href="'.URL.'/groups/wall/edit_post.php?id='.$id.'&del_audio='.$q[$i].'" onclick="FormContent.DelCache(this.id,this.href); return false"> <img src="'.URL.'/design/img/closed.png"/></a>
			</p></div>';
		}
	}
	echo '<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>';


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

?>