// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function prepareUpload(currentTab) {
	//$('fileupload_' + currentTab).show();
	Effect.SlideDown('fileupload_div_' + currentTab, { duration: 0.5 });
	$('fileupload_' + currentTab + '_description').focus();
}

function startUpload(currentTab) {
	desc = $('fileupload_' + currentTab + '_description').value;
	if (desc == '' || desc == 'Description') {
		return false;
	}

	
	$('progress').show();
	$('uploadbtn').hide();

	//update the progress bar
	var uuid = $('X-Progress-ID').value;
	new PeriodicalExecuter(
		function(pe){
			if(Ajax.activeRequestCount == 0){
				new Ajax.Request("/progress",{
					method: 'get',
					parameters: 'X-Progress-ID=' + uuid,
					onSuccess: function(xhr){
						var upload = xhr.responseText.evalJSON();
						if(upload.state == 'uploading'){
							upload.percent = Math.floor((upload.received / upload.size) * 100);
							$('bar').setStyle({width: upload.percent + "%"});
							$('bar').update(upload.percent + "%");
						}	else {
							pe.stop();
							$('progress').hide();
							$('bar').setStyle({width: "0%"});
							$('bar').update("0%");							
							$('uploadbtn').show();
						}
					}
				})
			}
			},0.10);

			return false; 
		}