/*
	A simple class for displaying file information and progress
	Note: This is a demonstration only and not part of SWFUpload.
	Note: Some have had problems adapting this class in IE7. It may not be suitable for your application.
*/

// Constructor
// file is a SWFUpload file object
// targetID is the HTML element id attribute that the FileProgress HTML structure will be added to.
// Instantiating a new FileProgress object with an existing file will reuse/update the existing DOM elements
function FileProgress(file, targetID) {
	var pattern = /([0-9]+)$/;
	var info = pattern.exec(file.id);
	
	if(info[1]%2 == 1)
	{
		var dark = 'dark';
	} else
	{
		var dark = '';
	}
	
	this.fileProgressID = file.id;

	this.opacity = 100;
	this.height = 0;
	

	this.fileProgressWrapper = document.getElementById(this.fileProgressID);
	if (!this.fileProgressWrapper) 
	{
		
		$image_pattern = /(gif|jpg|png|jpeg)$/i;
		$type = '';
		if($image_pattern.test(file.name))
		{
			$type = 'image';
		}
		
		$file_info = '<div id="'+this.fileProgressID+'">'+
						'<div class="container '+dark+'">'+
							'<div class="name"><strong class="'+$type+'">'+file.name+'</strong></div>'+
							'<div class="size">'+byteToKb(file.size)+' </div>'+
							'<div class="remove"><a href="#" class="delete">delete</a></div>'+
						'</div>'+
						'<div class="progress-wrapper" style="display:none">'+
							'<div class="progressContainer">'+
								'<div class="progress-bar" style="width:500px">'+
									'<div class="progress-holder" style="padding:0px">'+
										'<div class="progress-frame" style="padding:0px"></div>'+
									'</div>'+
								'</div>'+
								'<span class="percent">0 %</span>'+
								'<span class="text">&nbsp;</span>'+
							'</div>'+
						'</div>'+
					'</div>';

		$('#'+targetID).append($file_info);
		
		this.fileProgressWrapper = document.getElementById(this.fileProgressID);
		this.fileProgressElement =  $('#'+this.fileProgressID+' .progressContainer');
	} else {
		this.fileProgressElement =  $('#'+this.fileProgressID+' .progressContainer');
		this.reset();
	}

}

FileProgress.prototype.reset = function () 
{
	this.setProgress(0);
	this.setStatus('&nbsp;');
};

FileProgress.prototype.setProgress = function (percentage) 
{
	if(percentage > 0)
	{
		$('#'+this.fileProgressID+' .progress-wrapper').show();
		$('#'+this.fileProgressID+' .container').hide();
	}
	
	$('#'+this.fileProgressID+' .progress-holder').css('width', percentage+'%');
	$('#'+this.fileProgressID+' .progressContainer span:eq(0)').text(percentage+'%');
};

FileProgress.prototype.setStatus = function (status) {
	$('#'+this.fileProgressID+' .progressContainer span:eq(1)').html(status);
};

FileProgress.prototype.setComplete = function () {
	this.setProgress(100);
	
	$('#'+this.fileProgressID+' .progressContainer span:eq(1)').attr('class', 'complete');
	$('#'+this.fileProgressID+' .progress-wrapper').show();
	$('#'+this.fileProgressID+' .container').hide();
	
	$('#'+this.fileProgressID).show().delay(10000).fadeOut();
};

FileProgress.prototype.setError = function () 
{
	alert('not user FileProgress.prototype.setError in fileprogress.js lint 117');
};

FileProgress.prototype.setCancelled = function () 
{
	total_files--;
	
	if(total_files == 1)
	{
		var str = selected_text_one;
	}
	
	if(total_files > 1)
	{
		var  str = selected_text_many;
	}
	
	if(total_files == 0)
	{
		var  str = selected_text_zero;
	}
	
	$('.multi-file input').val(str.replace(/:count:/, total_files));
	
	$('#'+this.fileProgressID).remove();
};

// Show/Hide the cancel button
FileProgress.prototype.toggleCancel = function (show, swfUploadInstance) 
{
	//$('#'+this.fileProgressID+' .container').toggle();
	
	if (swfUploadInstance) 
	{
		var fileID = this.fileProgressID;
		
		$('#'+this.fileProgressID+' .delete').click(function(){
			swfUploadInstance.cancelUpload(fileID);
			return false;
		});
	}
};
