<?php 
    $UploadMaxSize = AJXP_Utils::convertBytes(ini_get('upload_max_filesize'));
    $UploadMaxPostSize = AJXP_Utils::convertBytes(ini_get('post_max_size'));
    if($UploadMaxPostSize > 0 && $UploadMaxPostSize < $UploadMaxSize) $UploadMaxSize = $UploadMaxPostSize;
    $confMaxSize = ConfService::getConf("UPLOAD_MAX_FILE");
    if($confMaxSize != 0 &&  $confMaxSize < $UploadMaxSize) $UploadMaxSize = $confMaxSize;
    $confTotalSize = ConfService::getConf("UPLOAD_MAX_TOTAL");
    $confTotalNumber = ConfService::getConf("UPLOAD_MAX_NUMBER");    
    $pluginConfigs = AJXP_PluginsService::getInstance()->findPlugin("uploader", "plupload")->getConfigs();    

	$repository = ConfService::getRepository();
	$accessType = $repository->getAccessType();
	if($accessType == "fs"){
		$partitionLength = $UploadMaxSize - 1000;	
	}else if($accessType == "remotefs"){
		$maxFileLength = $UploadMaxSize;
	}else if($accessType == "ftp"){
		$maxFileLength = $UploadMaxSize;
	}

    $minisite_session = "";
    if(strpos(session_name(), "AjaXplorer_Shared") === 0){
    $minisite_session = "&minisite_session=".substr(session_name(), strlen("AjaXplorer_Shared"));
    }
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">	
</head>
<body bgcolor="ffffff" style="overflow:hidden; padding: 0px; padding-left: 0px; margin: 0px;">

<link rel="stylesheet" href="plugins/uploader.plupload/plupload/jquery.plupload.queue/css/jquery.plupload.queue.css?v=2" type="text/css" media="screen" />
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>

<script type="text/javascript" src="plugins/uploader.plupload/plupload/plupload.full.min.js"></script>
<script type="text/javascript" src="plugins/uploader.plupload/plupload/jquery.plupload.queue/jquery.plupload.queue.min.js"></script>

<script language="javascript" type="text/javascript">
// Fix height of modal box
//$(window.parent.document).find("#generic_dialog_box").height(355); 


var clientId = '<?php echo session_id(); ?>';

var partitionLength = <?php echo ($partitionLength?$partitionLength:-1); ?>;
var maxFileLength = <?php echo ($maxFileLength?$maxFileLength:-1); ?>;

var ftpUrl = '<?php echo ($ftpURL?$ftpURL:""); ?>';

var uploadUrl = '<?php print($_SERVER["SCRIPT_NAME"]); ?>?get_action=upload_chunks_unify_plupload&secure_token=<?php echo $httpVars["secure_token"];?>&ajxp_sessid=<?php echo session_id().$minisite_session; ?>';

var maxFileSize = '<?PHP echo ($UploadMaxSize/1048576) . "mb"; ?>';
var maxHTML4 = '<?PHP echo $UploadMaxSize; ?>';
var config_runtimes = '<?php echo $pluginConfigs["RUNTIMES"]; ?>';
var config_max_file_size = '<?php echo $pluginConfigs["MAX_FILE_SIZE"]; ?>';
var config_chunk_size = '<?php echo (!empty($pluginConfigs["CHUNK_SIZE"]) ? $pluginConfigs["CHUNK_SIZE"] : (($UploadMaxSize/1048576) - 1)."mb"); ?>';
var currentDir = parent.ajaxplorer.getContextNode().getPath();

$(function() {
	$("#plupload_uploader").pluploadQueue({
		// General settings
		runtimes : config_runtimes,
		url : uploadUrl,
		max_file_size : config_max_file_size,
		chunk_size : config_chunk_size,

		unique_names : false,
        multiple_queues : true,
		multipart : true,
		multipart_params : { dir: currentDir },

		// Specify what files to browse for
		filters : [],

		// Flash settings
        flash_swf_url : 'plugins/uploader.plupload/plupload/Moxie.swf',
        silverlight_xap_url : 'plugins/uploader.plupload/plupload/Moxie.xap',

		// attach callbacks before queue
		preinit: attachCallbacks
	});
	
	// attach callbacks for FileUploaded
	function attachCallbacks(Uploader) {
		Uploader.bind('UploadFile', function(up, file, res) {
            parent.document.fire("ajaxplorer:longtask_starting");
		});
		Uploader.bind('FileUploaded', function(up, file, res) {
			if(this.total.queued <= 1) {
				parent.ajaxplorer.fireContextRefresh();
                parent.document.fire("ajaxplorer:longtask_finished");
			}
		});
	}

});
</script>

<div id="plupload_uploader">Your browser does not support uploads, please install Flash Player or Microsoft Silverlight.</div>
</body>
</html>
