/**
 *
 * Built on top of the swfupload library
 *   http://swfupload.org version 2.2.0
 *
 *  version 1.0.2
 */
var filenames="";

// on load change the upload to swfupload
function initSWFUpload() { 
	jQuery(function() {
		jQuery("#uploadimage_btn").after("<input class='button-primary' type='button' name='uploadimage' id='swfupload_btn' value='" + swf_upload.customSettings.upload + "' />")
								  .remove();
		jQuery("#swfupload_btn").click( function() { submitFiles(); } );
		jQuery("#imagefiles")
			.after("<div id='uploadQueue'></div>")
			.after("<input id='imagefiles' type='button' class='button-secondary uploadform' value='" + swf_upload.customSettings.browse + "' />")
			.after("<input type='text' id='txtFileName' readonly='readonly' />")				
			.remove();
		jQuery("#imagefiles").click( function() { fileBrowse(); } );
		jQuery("#progressbar-wrap").hide()
	});
}
 
// call the upload dialog
function fileBrowse() {
	jQuery("#txtFileName").val("");
	swf_upload.cancelUpload();
	swf_upload.selectFiles();
}

// called when a file is added
function fileQueued(fileObj) {
	filesize = " (" + Math.round(fileObj.size/1024) + " kB) ";;
	jQuery("#txtFileName").val(fileObj.name);
	filenames=filenames + "\n" + jQuery("#txtFileName").val();
	jQuery("#uploadQueue")
		.append("<div id='" + fileObj.id + "' class='UploadItem'> [<a href='javascript:removeFile(\"" + fileObj.id + "\");'>" + swf_upload.customSettings.remove + "</a>] " + fileObj.name + "</div>")
		.children("div:last").slideDown("slow")
		.end();
}

// start the upload
function submitFiles() {
	var ansprechfehl="";
	var telefehl="";
	var mailfehl="";
	var jobfehl="";
	var beschfehl="";
	var filefehl="";
	var gesamtfehl="";

 	if (jQuery('#ansprechperson').val() == "") 
	{
		ansprechfehl="Bitte den Namen einer Ansprechperson angeben!\n";
 	}

	if (jQuery('#telefon').val() == "")
 	{ 
		telefehl="Bitte eine Telefonnummer angeben!\n";
 	}

	if (jQuery('#email').val() == "")
 	{ 
		mailfehl="Bitte eine E-Mail angeben!\n";
 	}

	if (jQuery('#jobname').val() == "")
 	{ 
		jobfehl="Bitte einen Jobnamen angeben!\n";
 	}

	if (jQuery('#beschreibung').val() == "")
 	{ 
		beschfehl="Bitte eine File-Beschreibung angeben!\n";
 	}

	if (jQuery("#uploadQueue").children().size() == "0")
	{
		filefehl="Bitte w%E4hlen sie mindestens ein File aus!\n";
	}

	gesamtfehl=ansprechfehl+telefehl+mailfehl+jobfehl+beschfehl+filefehl;

	if(gesamtfehl=="")
	{
		jQuery('#uploadimage_form').prepend("<input type=\"hidden\" name=\"filenames\" value=\" " + filenames + " \">");
		jQuery("#progressbar-wrap").show();
		// get old post_params
		post_params = swf_upload.getSetting("post_params");
		// update the selected gallery in the post_params 
		swf_upload.startUpload();
	}
	else
	{
		alert(unescape(gesamtfehl));
		jQuery('#uploadimage_form').prepend("<input type=\"hidden\" name=\"swf_callback\" value=\"-1\">");
	}

}

// called when a file will be removed
function removeFile(fileID, name) {
	//Der filenames-string wird vor dem entfernen aktualisiert
	var removename="";
	removename= jQuery("#" + fileID).text();
	removename= removename.substring(13);
	filenames= filenames.replace("\n" + removename , "");

	// Das tatsächliche Entfernen
	swf_upload.cancelUpload(fileID);
	jQuery("#" + fileID).hide("slow");
	jQuery("#" + fileID).remove();
}

// called before the uploads start
function uploadStart(fileObj) {
	jQuery("#progressbar span").text(" ");
	jQuery("#progressbar div").text("0% - " + fileObj.name);
	return true;
}

// called during the upload progress
function uploadProgress(fileObj, bytesLoaded) {
	var length = Math.ceil((bytesLoaded / fileObj.size) * 385);
	var percent = Math.ceil((bytesLoaded / fileObj.size) * 100);
	jQuery("#progressbar span").css("width", length + "px");
	jQuery("#progressbar span").text(" ");
	jQuery("#progressbar div").text("Uploading: - " + fileObj.name + " (" + percent + "%)");
}

// called when the file is uploaded
function uploadComplete(fileObj) {
	jQuery("#" + fileObj.id).hide("slow");
	jQuery("#" + fileObj.id).remove();
	if ( swf_upload.getStats().files_queued == 0) {
		jQuery("#progressbar-wrap").hide()
		jQuery("#uploadimage_form").submit();
	}
}

// called when all files are uploaded
function uploadSuccess(fileObj, server_data) {
	// Show any error message
	if (server_data != 0){
		jQuery("#progressbar-wrap").append("<div><strong>ERROR</strong>: " + fileObj.name + " : " + server_data + "</div>");
	}
	// Upload the next file until queue is empty
	if ( swf_upload.getStats().files_queued > 0) {
		swf_upload.startUpload();
	} else {
		// server_data could be add as hidden field
		jQuery("#progressbar_message").html("<strong>Alle Dateien hochgeladen</strong>");
		jQuery('#uploadimage_form').prepend("<input type=\"hidden\" name=\"swf_callback\" value=\"" + server_data + "\">");				 
	}		
}
		
// called on error
function uploadError(fileObj, error_code, message) {
	var error_name = "";
	switch(error_code) {
		case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
			error_name = "HTTP ERROR";
		break;
		case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL:
			error_name = "MISSING UPLOAD URL";
		break;
		case SWFUpload.UPLOAD_ERROR.IO_ERROR:
			error_name = "IO FAILURE";
		break;
		case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
			error_name = "SECURITY ERROR";
		break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
			error_name = "UPLOAD LIMIT EXCEEDED";
		break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
			error_name = "UPLOAD FAILED";
		break;
		case SWFUpload.UPLOAD_ERROR.SPECIFIED_FILE_ID_NOT_FOUND:
			error_name = "SPECIFIED FILE ID NOT FOUND";
		break;
		case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:
			error_name = "FILE VALIDATION FAILED";
		break;
		case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
			error_name = "FILE CANCELLED";
			return;
		break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
			error_name = "FILE STOPPED";
		break;
		default:
			error_name = "UNKNOWN";
		break;
	}
	jQuery("#progressbar-wrap").append("<div><strong>ERROR " + error_name + " </strong>: " + fileObj.name + " : " + message + "</div>");
	jQuery("#" + fileObj.id).hide("slow");
	jQuery("#" + fileObj.id).remove();
	if ( swf_upload.getStats().files_queued > 0) {
		swf_upload.startUpload();
	} else {
		jQuery("#progressbar-wrap").hide()
		jQuery('#uploadimage_form').prepend("<input type=\"hidden\" name=\"swf_callback\" value=\"" + error_name + "\">");
		jQuery("#uploadimage_form").submit();
	}
}