<?php

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

	if (!is_dir(HOME.'/files/obkl/'.$user_id))
	{
		mkdir(HOME.'/files/obkl/'.$user_id,0777);
		mkdir(HOME.'/files/obkl/'.$user_id.'/icons',0777);
		mkdir(HOME.'/files/obkl/'.$user_id.'/mini',0777);
		mkdir(HOME.'/files/obkl/'.$user_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/obkl/'.$user_id.'/'.$file_name);
		///
		$savePath = HOME.'/files/obkl/'.$user_id.'/mini/'.$file_name;
		$filePath = $_FILES['file']['tmp_name'];
		$image = AcImage::createImage($filePath);
		$image
		->cropCenter('4pr', '4pr')
		->resizeByWidth(150)
		->save($savePath);
		$savePath2 = HOME.'/files/obkl/'.$user_id.'/icons/'.$file_name;
		$filePath2 = HOME.'/files/obkl/'.$user_id.'/mini/'.$file_name;
		$image2 = AcImage::createImage($filePath2);
		$image2
		->cropCenter('4pr', '4pr')
		->resizeByWidth(50)
		->save($savePath2);
		$savePath3 = HOME.'/files/obkl/'.$user_id.'/preview/'.$file_name;
		$filePath3 = HOME.'/files/obkl/'.$user_id.'/'.$file_name;
		$image3 = AcImage::createImage($filePath3);
		$image3
		->resizeByWidth(600)
		->save($savePath3);

		mysql_query("UPDATE `users` SET `obkl` = '".$file_name."', count_obkl=count_obkl+1 WHERE `id` = '".$user_id."'");
		mysql_query("INSERT INTO `obkl` SET `name` = '".$file_name."', `user_id` = '".$user_id."', `time` = '".time()."'");
		mysql_query("INSERT INTO `wall` SET `photos` = '".$file_name."', `user_id` = '".$user_id."', `wall_id` = '".$user_id."', `type` = 'update_obkl', `time` = '".time()."'");
		
		if ($ajax_query == true) exit;
	}
}

?>
<script type="text/javascript">
	$('input:file').remove();
		$(document).ready(function() {
			new AjaxUpload('upload', {
        action: "/obkl/index.php?id=<?echo $user_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("/obkl/index.php?id=<?echo $user_id;?>");
				}
			});
		});
	</script>