<?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']));	
	if ($user_id == $id) go(URL.'/mail/');
	$result = mysql_query("SELECT * FROM `mail_dialog` WHERE `for_id` = '".$user_id."' AND `from_id` = '".$id."' LIMIT 1");
	if (mysql_num_rows($result) == true)
	{
		$arr_dialog = mysql_fetch_array(mysql_query("SELECT * FROM `mail_dialog` WHERE `for_id` = '".$user_id."' AND `from_id` = '".$id."' LIMIT 1"));
		require_once 'handler/del_cache.php';
		require_once 'handler/upload.php';
	}
	else go(URL);
}
else go(URL);

$title = lang('Загрузить фотографии','Завантажити фотографії');
if ($ajax != 'yes') require_once '../incfiles/head.php';

if ($ajax == 'yes') {
?>
<script type="text/javascript">
$(function(){
	$('#form_upload').on('submit', function(e){
	$('#form_content').slideUp(200);
    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,
      dataType: 'json',
      xhr: function(){
	        var xhr = $.ajaxSettings.xhr();
        xhr.upload.addEventListener('progress', function(evt){
 		$('#notifications').html('<?echo '<p>'.lang('Загрузка...','Завантаження...').'</p>';?><progress id="progressbar" value="0" max="100"></progress>').show();
		var progressBar = $('#progressbar');
         if(evt.lengthComputable) {
            var percentComplete = Math.ceil(evt.loaded / evt.total * 100);
            progressBar.val(percentComplete).text(percentComplete + '%');
				if(percentComplete == 100)
				{
					$('#notifications').hide();
					$('#cache_photos b').html('<?echo $arr_dialog['count_cache']+1;?>');
				}
          }
        }, false);
        return xhr;
      },
      success: function(json){
        if(json){
          $that.after(json);
		}      
      }
    });
  });
});
</script>
<?
}
	if ($ajax != 'yes') echo '<div class="title"><a href="'.URL.'/mail/?id='.$id.'">'.lang('Диалог','Діалог').'</a></div>';
	echo '<div class="title"><div class="right for_pc"><a href="" onclick="FormContent.Close(); return false"><img src="'.URL.'/design/img/del.png"/></a></div>'.lang('Загрузить фотографию','Завантажити фотографію').'</div>';


if (!empty($arr_dialog['count_cache']))
{
	echo '<div class="block">';
	$q = explode("||", substr($arr_dialog['cache_photos'], 1, -1));
	$count = count($q);
	for ($i = 0; $i < $count; $i++)
	{
		echo '<a id="'.$i.'" class="del_cache" href="'.URL.'/mail/upload.php?id='.$id.'&del='.$q[$i].'"><img class="preview_icons" src="'.URL.'/files/mail/'.$arr_dialog['dir'].'/photos/icons/'.$q[$i].'"/></a>';
	}
	echo '</div>';
}

if ($arr_dialog['count_cache'] < 10)
{
	echo '<div class="block">
	<form id="form_upload" action="'.URL.'/mail/upload.php?id='.$id.'" method="post" enctype="multipart/form-data">
	<input class="input2" name="file" type="file" accept="image/jpeg,image/png,image/gif"/>
	<input class="button2" type="submit" name="upl" value="'.lang('Загрузить','Завантажити').'"/>
	</form>
	</div>';
}

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

?>