$(document).ready(function(){
    $('.date_mask').mask('99/99/9999');
    $("#tabs").tabs();
    
    $('#elements').add('#notice').add('#gabarit_1').add('#gabarit_2').MultiFile({
        STRING: {
            file: '<em title="Cliquez pour supprimer" onclick="$(this).parent().prev().click()">$file</em>',
            remove: '<img src="/images/bin.gif" height="16" width="16" alt="x"/>'
        }
    });
    if ($('#progress_key').length)
    {
	    $("#uploadprogressbar").progressBar({ barImage: '/images/progressbg_green.gif'});
	    $('#uploadForm').attr("target","progressFrame").bind('submit',beginUpload);
	    
    }

});

// fades in the progress bar and starts polling the upload progress after 1.5seconds
function beginUpload() {
	// uses ajax to poll the uploadprogress.php page with the id
	// deserializes the json string, and computes the percentage (integer)
	// update the jQuery progress bar
	// sets a timer for the next poll in 750ms
	$("#uploadprogressbar").fadeIn();

	var i = setInterval(function() { 
		$.getJSON("/participer/rendu-de-projet/getuploadstatus?id=" + $('#progress_key').val(), function(data) {
			if (data == null) {
				clearInterval(i);
                location.reload(true);
				return;
			}

			var percentage = Math.floor(100 * parseInt(data.bytes_uploaded) / parseInt(data.bytes_total));
			$("#uploadprogressbar").progressBar(percentage);
		});
	}, 1500);
};
