var fileuploads = new Array();


function Fileupload(folder_id, callback, file_params, post_params, callback_params, manualInit)
{
	fileuploads[fileuploads.length] = this;
	
	this.imagebutton = document.createElement('div');
	this.imagebutton.id = 'filesbutton.'+folder_id;
	this.imagebutton.className = 'filebutton';
	document.body.insertBefore(this.imagebutton, document.body.firstChild);
	
	this.folder_id = folder_id;
	
	this.settings = {
		flash_url : "interface/javascript/swfupload/swfupload.swf",
		flash9_url : "interface/javascript/swfupload/swfupload_fp9.swf",
		upload_url: "http://www.doctorswiss.ch/index.php?section=update&action=upload&ajax=true&PHPSESSID=1178248099ee660ed820f0e2b546dc2e",
		post_params: post_params,
		file_size_limit : file_params.size, //"100 MB",
		file_types : file_params.types, //"*.jpg",
		file_types_description : file_params.description, //"Images jpeg",
		file_upload_limit : 100,
		file_queue_limit : 0,
		custom_settings : {
			folder_id: folder_id,
			callback_params: callback_params,
			callback: callback
		},
		debug: false,
		
		// Button settings
		button_placeholder_id: "fileplaceholder." + folder_id,
		button_width: '100%',
		button_height: '100%',
		button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
		button_cursor: SWFUpload.CURSOR.HAND,
		
		// The event handler functions are defined in handlers.js
		
		swfupload_preload_handler : this.preLoad,
		swfupload_load_failed_handler : this.loadFailed,
		file_dialog_start_handler : this.fileDialogStart,
		file_queued_handler : this.fileQueued,
		file_queue_error_handler : this.fileQueueError,
		file_dialog_complete_handler : this.fileDialogComplete,
		upload_start_handler : this.uploadStart,
		upload_progress_handler : this.uploadProgress,
		upload_error_handler : this.uploadError,
		upload_success_handler : this.uploadSuccess,
		upload_complete_handler : this.uploadComplete,
		queue_complete_handler : this.queueComplete	// Queue plugin event
	};
	
	if (!manualInit)
		this.load();
}

Fileupload.unloadAll = function ()
{
	for (var i = 0; i < fileuploads.length; i++)
	{
		fileuploads[i].unload();
	}
}

Fileupload.prototype.unload = function()
{
	if (this.swfu && this.swfu.destroy())
	{
		this.swfu = false;
	}
	
	if (this.imagebutton)
	{
		this.imagebutton.style.display = 'none';
	}
}

Fileupload.prototype.load = function ()
{	
	if (!this.swfu)
	{
		var fileplaceholder = document.createElement('div');
		fileplaceholder.id = 'fileplaceholder.' + this.folder_id;
		
		this.imagebutton.appendChild(fileplaceholder);
	
		this.swfu = new SWFUpload(this.settings);
		this.swfu.startNextUpload		= this.startNextUpload;
		this.swfu.startUploadProcess	= this.startUploadProcess;
		this.swfu.setNextFile			= this.setNextFile;
	}
}

Fileupload.prototype.adaptToElement = function (element)
{
	this.imagebutton = document.getElementById('filesbutton.' + this.folder_id);
	this.imagebutton.style.zIndex = 500;
	this.imagebutton.style.display = 'block';
	
	setBounds(this.imagebutton, getBounds(element));
	
	this.load();
}

Fileupload.prototype.setParams = function (post_params, callback_params)
{
	if (post_params)		this.settings.post_params = post_params;
	if (callback_params)	this.settings.custom_settings.callback_params = callback_params;
}

Fileupload.prototype.setData = function (data)
{
	this.settings.post_params.data = data;
}

Fileupload.prototype.preLoad = function()
{
	if (!this.support.loading) {
		//alert("You need the Flash Player 9.028 or above to use SWFUpload.");
		return false;
	}
}

Fileupload.prototype.loadFailed = function()
{
	//alert("Something went wrong while loading SWFUpload. If this were a real application we'd clean up and then give you an alternative");
}

Fileupload.prototype.fileDialogStart = function()
{
	this.files = new Array();
	this.customSettings.callback.initUpload(this);
}

Fileupload.prototype.setNextFile = function(file)
{
	this.files[this.files.length] = file;
	
	
	if (this.numFilesQueued && this.files.length == this.numFilesQueued)
	{
		this.startUploadProcess();
	}
}

Fileupload.prototype.startUploadProcess = function ()
{
	var progressbar = document.createElement('div');
		progressbar.className = 'progress';
	
	var bar = document.createElement('div');
		bar.className = 'bar';
	
	var loaded = document.createElement('div');
		bar.appendChild(loaded);
	
	var percent = document.createElement('div');
		percent.className = 'percent';
		percent.innerHTML = '100%';
	
	progressbar.appendChild(bar);
	progressbar.appendChild(percent);
	
	progressbar.percent = percent;
	progressbar.loaded = loaded;
	
	this.progressbar = progressbar;
	
	this.startNextUpload();
}

Fileupload.prototype.startNextUpload = function()
{
	if (this.files[0])
	{
		this.startUpload(this.files[0].id);
	}
	this.files = this.files.slice(1);
}

Fileupload.prototype.fileQueued = function(file)
{
	this.customSettings.callback.fileQueued(this, file, this.customSettings.callback_params);
	//this.customSettings.container.add(false, 'Image', '&dummy=true', this, file);
}

Fileupload.prototype.fileQueueError = function(file, errorCode, message)
{
	
	try {
		if (errorCode === SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED) {
			alert("You have attempted to queue too many files.\n" + (message === 0 ? "You have reached the upload limit." : "You may select " + (message > 1 ? "up to " + message + " files." : "one file.")));
			return;
		}
	} catch (ex) {
		this.debug(ex);
	}
}

Fileupload.prototype.fileDialogComplete = function(numFilesSelected, numFilesQueued, numFilesInQueue)
{
	this.numFilesSelected = numFilesSelected;
	this.numFilesQueued = numFilesQueued;
	
	
	
	try {
		if (numFilesSelected == 0)
		{
			if (this.customSettings.callback.uploadEnd)
				this.customSettings.callback.uploadEnd();
		}
		else if (this.files.length == this.numFilesQueued)
		{
			this.startUploadProcess();
		}
	} catch (ex)  {
		this.debug(ex);
	}
}


Fileupload.prototype.uploadStart = function(file)
{
	this.customSettings.callback.uploadStart(this.progressbar, file);
}

Fileupload.prototype.uploadProgress = function(file, bytesLoaded, bytesTotal)
{
	
	var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);
	
	try {
		
		this.progressbar.loaded.style.width = percent +'%';
		this.progressbar.percent.innerHTML = '';
		this.progressbar.percent.appendChild(document.createTextNode(percent + '%'));
		
	} catch (ex) {
		this.debug(ex);
	}
}

Fileupload.prototype.uploadSuccess = function(file, serverData, responseReceived)
{
		var xml = load_xml_content_string(serverData);
	
		var xhr = {responseXML : xml, responseText : serverData};
		
		this.customSettings.callback.uploadSuccess(this, file, xhr);
		
		if (this.progressbar.parentNode)
		this.progressbar.parentNode.removeChild(this.progressbar);
		this.progressbar.loaded.style.width = '0px';
		this.progressbar.percent.innerHTML = '';
		this.progressbar.percent.appendChild(document.createTextNode('0%'));
		this.startNextUpload();
		
	/*} catch (ex) {
		this.debug(ex);
	}*/
}

Fileupload.prototype.uploadError = function(file, errorCode, message)
{
	
	try {
		
	} catch (ex) {
		this.debug(ex);
	}
}

Fileupload.prototype.uploadComplete = function(file)
{
		
	/*if (this.getStats().files_queued === 0) {
		
	}*/
}

// This event comes from the Queue Plugin
Fileupload.prototype.queueComplete = function(numFilesUploaded)
{
	
	//status.innerHTML = numFilesUploaded + " file" + (numFilesUploaded === 1 ? "" : "s") + " uploaded.";
}
