<?php

if (!empty($_FILES['file']))
{

	if (!is_dir(HOME.'/files/groups/groups/avatars/'.$id))
	{
		mkdir(HOME.'/files/groups/avatars/'.$id,0777);
		mkdir(HOME.'/files/groups/avatars/'.$id.'/icons',0777);
		mkdir(HOME.'/files/groups/avatars/'.$id.'/mini',0777);
		mkdir(HOME.'/files/groups/avatars/'.$id.'/preview',0777);
	}

	require_once HOME.'/toolkit/AcImage.php';
	AcImage::setTransparency(true);
	if(AcImage::isFileExists($_FILES['file']['tmp_name'])) {} else {$err = lang('Файл не существует!','Файл не існує!');}
	if($err == false && AcImage::isFileImage($_FILES['file']['tmp_name']))
	{
		$file_name = time().'.jpg';
		copy($_FILES['file']['tmp_name'], HOME.'/files/groups/avatars/'.$id.'/'.$file_name);
		///
		$savePath = HOME.'/files/groups/avatars/'.$id.'/mini/'.$file_name;
		$filePath = $_FILES['file']['tmp_name'];
		$image = AcImage::createImage($filePath);
		$image
		->cropCenter('4pr', '4pr')
		->resizeByWidth(150)
		->save($savePath);
		$savePath2 = HOME.'/files/groups/avatars/'.$id.'/icons/'.$file_name;
		$filePath2 = HOME.'/files/groups/avatars/'.$id.'/mini/'.$file_name;
		$image2 = AcImage::createImage($filePath2);
		$image2
		->cropCenter('4pr', '4pr')
		->resizeByWidth(50)
		->save($savePath2);
		$savePath3 = HOME.'/files/groups/avatars/'.$id.'/preview/'.$file_name;
		$filePath3 = HOME.'/files/groups/avatars/'.$id.'/'.$file_name;
		$image3 = AcImage::createImage($filePath3);
		$image3
		->resizeByWidth(600)
		->save($savePath3);

		mysql_query("UPDATE `groups` SET `avatar` = '".$file_name."', count_wall=count_wall+1 WHERE `id` = '".$id."'");
		mysql_query("INSERT INTO `groups_avatars` SET `name` = '".$file_name."', `group_id` = '".$id."', `time` = '".time()."'");
		mysql_query("INSERT INTO `groups_wall` SET `photos` = '".$file_name."', `from_group` = 1, `user_id` = '".$user_id."', `wall_id` = '".$id."', `type` = 'update_avatar', `time` = '".time()."'");
		
		if ($ajax_query == true) exit;
	}
}

?>
<script type="text/javascript">
	$('input:file').remove();
		$(document).ready(function() {
			new AjaxUpload('upload', {
        action: "/groups/avatars/index.php?id=<?echo $id;?>",
        name: "file",
		data: {ajax: "yes"},
        onSubmit: function(file, ext) {
            if(! (ext && /^(jpg|gif|png|jpeg)$/.test(ext))) {
                return false;
            }
 			Page.Loading('start');
        },
      onComplete: function(file, res) {
	  $('#preview_loading').remove();
            if(res)
                Page.Go("/groups/avatars/index.php?id=<?echo $id;?>");
				}
			});
		});
	</script>