<?php
/*------------------------------------------------------------------------
# com_guru
# ------------------------------------------------------------------------
# author    iJoomla
# copyright Copyright (C) 2013 ijoomla.com. All Rights Reserved.
# @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
# Websites: http://www.ijoomla.com
# Technical Support:  Forum - http://www.ijoomla.com.com/forum/index/
-------------------------------------------------------------------------*/
defined('_JEXEC') or die('Restricted access');
$upload_mb = guruAdminHelper::getUploadMaxSize();
$upload_mb *= 1048576; //transform in bytes

$doc = JFactory::getDocument();
$doc->addScript('components/com_guru/js/fileuploader.js');
$doc->addStyleSheet('components/com_guru/css/fileuploader.css');
?>

<script>
	jQuery(document).ready($ => {
		const baseUri = Joomla.getOptions('system.paths').base;

		function createImageUploader() {
			var uploader = new qq.FileUploader({
				element: document.getElementById('imageUploader'),
				action: baseUri + '/index.php?option=com_guru&controller=guruConfigs&tmpl=component&format=raw&task=guru_file_uploader',
				params: {
					folder: 'media',
					mediaType: 'image',
					size: document.getElementById('media_fullpx').value,
					type: document.getElementById('media_prop').value
				},
				onSubmit: function(id, fileName, folder) {
					$('.qq-upload-list li').css('display', 'none');
				},
				onComplete: function(id, fileName, responseJSON) {
					$('.qq-upload-success').append('- <span style="color:#387C44;">Upload successful</span>');
					$('#view_imagelist23').attr("src", "../" + responseJSON.locate + "/" + fileName + "?timestamp=" + new Date().getTime());
					$('#image').val("/" + fileName);
					$('#is_image').val("1");
				},
				allowedExtensions: ['jpg', 'jpeg', 'png', 'gif', 'JPG', 'JPEG', 'PNG', 'GIF', 'xls', 'XLS'],
				sizeLimit: <?php echo $upload_mb ?>,
				multiple: false,
				maxConnections: 1
			});
		}
		createImageUploader();

		function createVideoUploader() {
			var uploader = new qq.FileUploader({
				element: document.getElementById('videoUploader'),
				action: baseUri + '/index.php?option=com_guru&controller=guruConfigs&tmpl=component&format=raw&task=guru_file_uploader',
				params: {
					mediaType: 'video'
				},
				onSubmit: function(id, fileName, folder) {
					$('.qq-upload-list li').css('display', 'none');
				},
				onComplete: function(id, fileName, responseJSON) {
					$('.qq-upload-success').append('- <span style="color:#387C44;">Upload successful</span>');
					$('#source_local_v2').val('local');
					$('#source_local_v2').attr("checked", "checked");
					$('#source_code_v').attr("checked", "");
					$('#source_url_v').attr("checked", "");
					document.getElementById("source_local_v2").checked = true;

					const values = [];

					$('#localfile > option').each((i, el) => {
						values.push(el.value);
					});

					if (values.indexOf(fileName) === -1) {
						$('#localfile').append(`<option value="${fileName}" >${fileName}</option>`);
					}

					$('#localfile').val(fileName);

				},
				allowedExtensions: ['ax', 'amv', 'asf', 'gif', 'avi', 'mp4', 'fla', 'swf', 'mov', 'mpg', 'mpeg', 'rm', 'xls', 'XLS'],
				sizeLimit: <?php echo $upload_mb ?>,
				multiple: false,
				maxConnections: 1
			});
		}
		createVideoUploader();

		function createAudioUploader() {
			var uploader = new qq.FileUploader({
				element: document.getElementById('audioUploader'),
				action: baseUri + '/index.php?option=com_guru&controller=guruConfigs&tmpl=component&format=raw&task=guru_file_uploader',
				params: {
					mediaType: 'audio'
				},
				onSubmit: function(id, fileName, folder) {
					$('.qq-upload-list li').css('display', 'none');
				},
				onComplete: function(id, fileName, responseJSON) {
					$('.qq-upload-success').append('- <span style="color:#387C44;">Upload successful</span>');
					$('#source_local_a2').val('local');
					$('#source_local_a2').attr("checked", "checked");

					const values = [];

					$('#localfile_a > option').each((i, el) => {
						values.push(el.value);
					});

					if (values.indexOf(fileName) === -1) {
						$('#localfile_a').append(`<option value="${fileName}" >${fileName}</option>`);
					}

					$('#localfile_a').val(fileName);
				},
				allowedExtensions: ['aac', 'aob', 'ada', 'au', 'mid', 'midi', 'mp3', 'ogg', 'wav', 'm4a', 'xls', 'XLS'],
				sizeLimit: <?php echo $upload_mb ?>,
				multiple: false,
				maxConnections: 1
			});
		}
		createAudioUploader();

		function createDocUploader() {
			var uploader = new qq.FileUploader({
				element: document.getElementById('docUploader'),
				action: baseUri + '/index.php?option=com_guru&controller=guruConfigs&tmpl=component&format=raw&task=guru_file_uploader',
				params: {
					mediaType: 'doc'
				},
				onSubmit: function(id, fileName, folder) {
					$('.qq-upload-list li').css('display', 'none');
				},
				onComplete: function(id, fileName, responseJSON) {
					$('.qq-upload-success').append('- <span style="color:#387C44;">Upload successful</span>');
					$('#source_local_d2').val('local');
					$('#source_local_d2').attr("checked", "checked");

					const values = [];

					$('#localfile_d > option').each((i, el) => {
						values.push(el.value);
					});

					if (values.indexOf(fileName) === -1) {
						$('#localfile_d').append(`<option value="${fileName}" >${fileName}</option>`);
					}

					$('#localfile_d').val(fileName);
				},
				allowedExtensions: ['doc', 'docx', 'txt', 'pdf', 'csv', 'htm', 'html', 'xhtml', 'xml', 'sxw', 'rtf', 'odt', 'css', 'odp', 'pps', 'ppt', 'pptx', 'sxi', 'xls', 'xlsx'],
				sizeLimit: <?php echo $upload_mb ?>,
				multiple: false,
				maxConnections: 1
			});
		}
		createDocUploader();

		function createFileUploader() {
			var uploader = new qq.FileUploader({
				element: document.getElementById('fileUploader'),
				action: baseUri + '/index.php?option=com_guru&controller=guruConfigs&tmpl=component&format=raw&task=guru_file_uploader',
				params: {
					mediaType: 'file'
				},
				onSubmit: function(id, fileName, folder) {
					$('.qq-upload-list li').css('display', 'none');
				},
				onComplete: function(id, fileName, responseJSON) {
					$('.qq-upload-success').append('- <span style="color:#387C44;">Upload successful</span>');
					$('#source_local_f2').val('local');
					$('#source_local_f2').attr("checked", "checked");

					const values = [];

					$('#localfile_f > option').each((i, el) => {
						values.push(el.value);
					});

					if (values.indexOf(fileName) === -1) {
						$('#localfile_f').append(`<option value="${fileName}" >${fileName}</option>`);
					}

					$('#localfile_f').val(fileName);
				},
				allowedExtensions: ['exe', 'zip', 'xls', 'XLS'],
				sizeLimit: <?php echo $upload_mb ?>,
				multiple: false,
				maxConnections: 1
			});
		}
		createFileUploader();
	})
</script>