(function($) {
    $.fn.jQupload = function(options) {  
		var defaults = {
			upload_id: "1",
			uid: "",
			maxSize: "50000000",
			FileType: "",
			BasePath: "data/",
			Titre: "Fichier :",
			itemID: ""
		};
		var opts = $.extend(defaults, options);
		$(this).append('<iframe id="upload_target" name="upload_target" src="#" style="width:0px;height:0px;border-style:none;"></iframe>');
		$(this).attr("target","upload_target");
		$(this).attr("action","upload.traitement.php");
		$(this).append('<input type="hidden" name="APC_UPLOAD_PROGRESS" id="progress_key" value="'+opts.uid+'" />');
		$(this).append('<input type="hidden" name="upload_id" value="'+opts.upload_id+'" />');
		$(this).append('<input type="hidden" name="itemID" value="'+opts.itemID+'" />');
		$(this).append('<input type="hidden" name="BasePath" value="'+opts.BasePath+'" />');
		$(this).append('<div id="inputFile'+opts.upload_id+'" class="fileInput"><b>'+opts.Titre+' </b><input name="myfile" type="file" class="fileInp" id="myfile" onchange="FileChange('+opts.upload_id+');" /></div>');
		$(this).append('<div id="bar'+opts.upload_id+'" style="display:none;" class="bar"><span id="progressbar'+opts.upload_id+'" class="progressbar">0%</span></div>');
		$(this).append('<div id="submit'+opts.upload_id+'" style="display:none;" class="submit-btn"><input type="submit" name="sub" value="uploader" class="Submit" /></div>');
		$(this).append('<div id="waiting'+opts.upload_id+'" style="display:none;" class="waiting-div"><img src="../../images/loader.gif" /> Veuillez patienter durant le traitement du fichier</div>');		
		$(this).append('<div id="error'+opts.upload_id+'" class="error-div"></div>');		
		$("#progressbar"+opts.upload_id).progressBar();
		$(this).submit(function () {
			return StartUpload(opts.uid,opts.maxSize,opts.FileType,opts.upload_id);
		});
	};
})(jQuery);

function StartUpload(uid, maxSize, FileType, upload_id)
{
	setTimeout('getProgress("'+uid+'","'+maxSize+'","'+FileType+'","'+upload_id+'")', 600);
	HideForm(upload_id);
	return true;
}

function ShowForm(upload_id)
{
	$("#bar"+upload_id).fadeOut("slow");
	$("#submit"+upload_id).fadeOut("slow");
	$("#inputFile"+upload_id).fadeIn("slow");
}

function HideForm(upload_id)
{
	$("#inputFile"+upload_id).fadeOut("slow");
	$("#submit"+upload_id).fadeOut("slow");
	$("#bar"+upload_id).fadeIn("slow");
}
	
function getProgress(uid, maxSize, FileType, upload_id) {
	$.get("upload.progress.php?uid="+uid+"&maxSize="+maxSize+"&FileType="+FileType, function(data) {
		if(!data) return "";
		var msg = "";
		if(data == "errType") msg = "Le type de fichier n'est pas autorisé";
		if(data == "errPoids") msg = "Le fichier dépasse le poids maximum autorisé";			
		if(msg!="")
		{
			DisplayError(msg, upload_id);
			upload_target.location.href='upload.traitement.php?rf='+Math.random();
			ShowForm(upload_id);
		}
		else
		{
			$("#progressbar"+upload_id).progressBar(data);
			if(data < 100) setTimeout('getProgress("' + uid + '","'+maxSize+'","'+FileType+'","'+upload_id+'")', 200);
			if(data == 100)
			{
				$("#bar"+upload_id).fadeOut("slow");
				$("#waiting"+upload_id).fadeIn("slow");
			}
		}
	});
}

function DisplayError(msg, upload_id)
{
	$(document).ready(function () {
		$("#error"+upload_id).show();
		$("#error"+upload_id).html("<b>Erreur :</b> <br />" + msg);

	});
}

function FileChange(upload_id)
{
	$("#submit"+upload_id).show();
	$("#error"+upload_id).hide();
}

function EndUpload(url)
{
	document.location.href=url;
}


