jQuery(document).ready(function() { 

	jQuery('#uploadform').attr('action', '../library/upload.php');
	jQuery('#allfiles a').attr("target", "_blank");
	var groupID = jQuery('input[name="id"]').val(); // retrieve the group ID
	var folder = groupID;
	var allfiles = new Array();
	var globalcounter = 0;
	
	jQuery('#uploadform').after('<button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary" id="start_uploads"><span class="ui-button-icon-primary ui-icon ui-icon-circle-arrow-e"><\/span><span class="ui-button-text">'+ upbutton +'<\/span><\/button>');
	jQuery('#uploadform').after('<button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary" id="cancel_uploads"><span class="ui-button-icon-primary ui-icon ui-icon-cancel"><\/span><span class="ui-button-text">'+ delbutton +'<\/span><\/button>');
	jQuery('#uploadform button').after('<div>'+ addfiles +'</div>');

	$('#start_uploads').click(function () {
	    $('.file_upload_start button').click();
	});
	$('#cancel_uploads').click(function () {
        $('.file_upload_cancel button').click();
    });
	
	jQuery('#shared_file').click(function(){
		if (typeof jQuery('#shared_file:checked').val() !== "undefined") {
			folder='shared';
		} else {
			folder=groupID;
		}
	});
	
	function updateList(fileslist) {
		jQuery.ajax({url: '../workshop/_ajax_filelist.php', data: "id=" + groupID + "&folder=" + folder + "&rooturl=" + rooturl + "&upfiles=" + fileslist,cache: false, success: function(r){
			var $html = $(r);

	        $html.find("a[href$='.jpg'],a[href$='.jpeg'],a[href$='.png'],a[href$='.gif'], a[href$='.JPG'],a[href$='.JPEG'],a[href$='.PNG'],a[href$='.GIF']").not('.dlfile').colorbox({transition:"fade"});
			jQuery('#allfiles').empty().append($html);
		}});
	}
	
	jQuery('#uploadform').fileUploadUI({
		    uploadTable: jQuery('#files'),
		    downloadTable: jQuery('#files'),
		    buildUploadRow: function (files, index) {
				allfiles.push(files[index].name);
		        return jQuery('<tr><td class="file_upload_preview"><\/td>' +
		                '<td>' + files[index].name + '<\/td>' +
		                '<td class="file_upload_progress"><div><\/div><\/td>' +
		                '<td class="file_upload_start">' +
		                '<button class="ui-state-default ui-corner-all" title="'+ upbutton +'" disabled="disabled">' +
		                '<span class="ui-icon ui-icon-circle-arrow-e">'+ upbutton +'<\/span>' +
		                '<\/button><\/td>' +
		                '<td class="file_upload_cancel">' +
		                '<button class="ui-state-default ui-corner-all" title="'+ delbutton +'">' +
		                '<span class="ui-icon ui-icon-cancel">'+ delbutton +'<\/span>' +
		                '<\/button><\/td><\/tr>');
		    },
		    //buildDownloadRow: function (file) {
		    //  return $('<tr><td>' + file.name + '<\/td><\/tr>');
		    //},
	        dropZone: jQuery('#file_upload_container'),
		    beforeSend: function (event, files, index, xhr, handler, callBack) {
		    	// Maximum size test
		    	if (files[index].size > maxsize) {
		            handler.uploadRow.find('.file_upload_progress').html(size_msg);
		            setTimeout(function () {
		                handler.removeNode(handler.uploadRow);
		            }, 10000);
		            return;
		        }
		    	// File Type limit
		    	var regexp = eval('/\.'+regexpattern+'$/i');
		        // Using the filename extension for our test,
		        // as legacy browsers don't report the mime type
		        if (!regexp.test(files[index].name)) {
		            handler.uploadRow.find('.file_upload_progress').html(notallowed_msg);
		            setTimeout(function () {
		                handler.removeNode(handler.uploadRow);
		            }, 10000);
		            return;
		        }
		        handler.uploadRow.find('.file_upload_start button').click(function () {
		            handler.formData = {
		                foldername: folder,
		                id: groupID
		            };
		            callBack();
		        });
		    },
		    onComplete: function (event, files, index, xhr, handler) {
	            handler.onCompleteAll(files);
	        },
	        onAbort: function (event, files, index, xhr, handler) {
	            handler.removeNode(handler.uploadRow);
	        },
		    onCompleteAll: function (files) {
	            // The files array is a shared object between the instances of an upload selection.
	            // We extend it with a uploadCounter to calculate when all uploads have completed:
	            if (!files.uploadCounter) {
	                files.uploadCounter = 1;  
	            } else {
	                files.uploadCounter = files.uploadCounter + 1;
	            }
	            if (files.uploadCounter === files.length) {
	                /* your code after all uploads have completed */
	            }
	            // on incrémente le compteur
	            globalcounter++;
	            
	            if(globalcounter == allfiles.length) {
	            	/* your code after all uploads have completed */
	            	var msg = '';
	            	var fileslist = allfiles.join('|');
		            for(i=0; i< allfiles.length; i++) {
		            	msg += allfiles[i] + ' : ' + succeed + "<br \/>";
		            }
		            globalcounter = 0;
		            allfiles = Array();
		            jQuery('#files').before('<div class="info" id="msgupload">' + msg + '</div>');
			    	setTimeout('jQuery("div#msgupload").fadeOut();', 2000);
			    	// alert(fileslist);
			    	setTimeout(updateList(fileslist), 3000);
	            }

	        }
		});
});
