﻿/// <reference path="~/Scripts/ByteStorm/_BSC_Interface.js" />
(function()
{
	BSC.F = function()
	{
		var localdata = {};

		var file_queued_handler = function(p1)
		{
			var file = p1;
		};

		var file_dialog_complete_handler = function(p1, p2)
		{
			var numFilesSelected = p1;
			var numFilesQueued = p2;
			try
			{
				if (numFilesSelected > 0)
					this.startUpload();
			} catch (ex) { this.debug(ex); }
		};

		var upload_start_handler = function(p1, p2)
		{
			// PostParams
			var postParams = {
				p1: null,
				p2: (BSC.D.P.GetData()).TicketID,
				p3: null,
				p4: null
			};
			this.setPostParams(postParams);
		};

		var upload_progress_handler = function(p1, p2, p3)
		{
			var file = p1;
			var bytesLoaded = p2;
			var totalBytes = p3;

			//			try
			//			{
			//				var percent = Math.min(Math.ceil((bytesLoaded / file.size) * 100), 100);
			//				//var progress = new FileProgress(file,  this.customSettings.upload_target);
			//				//progress.SetProgress(percent);
			//				//$("#profilecreate_"+file.id+"_pb").css({width:percent+"%"});
			//				$("#" + file.id + "_loader ").css("display", "block")
			//				if (percent === 100)
			//				{
			//					$("#profilecreate_" + file.id + "_st").html("Creating thumbnail...");
			//					//progress.SetStatus("Creating thumbnail...");
			//					//progress.ToggleCancel(false, this);
			//				} else
			//				{
			//					$("#profilecreate_" + file.id + "_st").html("Uploading...");
			//					//$("#profilecreate_"+file.id+"_pb").css({width:percent+"%"});
			//					//progress.SetStatus("Uploading...");
			//					//progress.ToggleCancel(true, this);
			//				}
			//			} catch (ex)
			//			{
			//				this.debug(ex);
			//			}
		};

		var upload_success_handler = function(p1, p2)
		{
			var file = p1;
			var data = p2;

			//$("#"++"_pictureid").val(data);
			//			try
			//			{
			//				$("#profilecreate_" + file.id + "_st").html("Thumbnail Created!<br/>" + data);
			//				$("#profilecreate_" + file.id + " dd td.s ul li.s a.crop").css({ display: 'block' });
			//				var img = $(document.createElement("img"));
			//				img.attr("src", BSC.PicturePath(data, "medium"));
			//				img.css({ display: 'none' });
			//				$("#profilecreate_" + file.id + "_p").html("");
			//				$("#profilecreate_" + file.id + "_p").append(img);
			//				img.fadeIn("fast", function() { });
			//			} catch (ex)
			//			{
			//				this.debug(ex);
			//			}


		};

		var upload_complete_handler = function(p1)
		{
			var file = p1;

			try
			{
				/*  I want the next upload to continue automatically so I'll call startUpload here */
				if (this.getStats().files_queued > 0)
				{
					this.startUpload();
				} else
				{
					//alert("All images received.");	
					// Trigger event
					//BSC.E.Trigger("BSC.F.UploadComplete", null);
				}
			} catch (ex)
			{
				this.debug(ex);
			}
		};

		var upload_error_handler = function(file, errorcode, message)
		{
			// file, bytesLoaded, totalBytes
			//	SWFUpload.UPLOAD_ERROR is a simple object that contains Upload Error code constants. It is generally used to determine which error code was sent in the uploadError event.
			//	* HTTP_ERROR - The file upload was attempted but the server did not return a 200 status code.
			//	* MISSING_UPLOAD_URL - The upload_url setting was not set.
			//	* IO_ERROR - Some kind of error occurred while reading or transmitting the file. This most commonly occurs when the server unexpectedly terminates the connection.
			//	* SECURITY_ERROR - The upload violates a security restriction. This error is rare.
			//	* UPLOAD_LIMIT_EXCEEDED - The user has attempted to upload more files than is allowed by the file_upload_limit setting.
			//	* UPLOAD_FAILED - The attempt to initiate the upload caused an error. This error is rare.
			//	* SPECIFIED_FILE_ID_NOT_FOUND - A file ID was passed to startUpload but that file ID could not be found.
			//	* FILE_VALIDATION_FAILED - False was returned from the uploadStart event
			//	* FILE_CANCELLED - cancelUpload was called
			//	* UPLOAD_STOPPED - stopUpload was called.

			//alert("eoor: " + message)
		};

		var file_queue_error_handler = function(file, errorcode, message)
		{
			// SWFUpload.QUEUE_ERROR is a simple object that contains Queue Error code constants. It is generally used to determine which error code was sent in the fileQueueError event.
			// * QUEUE_LIMIT_EXCEEDED - indicates that the user has attempted to queue more files that is allowed by the settings. Once files have been updated and removed from the queue the user is again allowed to queue additional files.
			// * FILE_EXCEEDS_SIZE_LIMIT - indicates the selected file is larger than is allwed by the file_size_limit.
			// * ZERO_BYTE_FILE - indicates that the selected file is empty. The Flash Player cannot handle empty files and the file is rejected. Windows Shortcut files may also trigger this error.
			// * INVALID_FILETYPE - The selected file's extension does not match a valid extension from the file_types setting. User's can circumvent the file_types restriction by manually entering a file name.

			//var dd = "<div style='width:200px; height:300px; background:red;'>" + message + "</div>";
			//BSC.UI.ShowWindowAlert(current, "", null, "Attention!", "You can only upload one file", localdata[current].data.callback, localdata[current].data.data);
		};

		//		var upload_start_handler = function(p1)
		//		{
		//			var file = p1;
		//			//alert("file.name: " + file.name);
		//		};

		var swfupload_loaded_handler = function()
		{
			//InitSWFUploader();
		};

		var settings = {
			upload_url: BSC.BASE_UPLOAD_PATH + "Upload/Photo",
			flash_url: "/Scripts/Libraries/swfupload/2.2.0.1/swfupload.swf",
			debug: false,
			file_size_limit: "6 MB", // 204800, 409600, 614400
			file_types_description: "Picture files",
			file_upload_limit: 0,
			file_queue_limit: 0,
			file_types: "*.jpg;*.gif;*.png;*.bmp",

			button_image_url: BSC.BASE_STATIC_PATH + "images/buttons/blue/flash_btn.png",
			button_placeholder_id: "swfuploader",
			button_width: 330,
			button_height: 27,
			button_text: '<span class="btnupl">Select file</span>',
			button_text_style: '.btnupl { margin-left:33px; font-family: Verdana; font-weight:bold; color:#ffffff; font-size: 11px; line-height:28px; text-align:left; }',
			button_action: SWFUpload.BUTTON_ACTION.SELECT_FILE,
			button_disabled: false,
			button_cursor: SWFUpload.CURSOR.HAND,
			button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
			button_text_top_padding: 5,

			// Event Handler Settings
			file_queued_handler: file_queued_handler,
			file_dialog_complete_handler: file_dialog_complete_handler,
			upload_start_handler: upload_start_handler,
			upload_progress_handler: upload_progress_handler,
			upload_success_handler: upload_success_handler,
			upload_complete_handler: upload_complete_handler,
			upload_error_handler: upload_error_handler,
			file_queue_error_handler: file_queue_error_handler,
			upload_start_handler: upload_start_handler,
			swfupload_loaded_handler: swfupload_loaded_handler
		};

		return {
			GetUpload: function(p0, p1, p2)
			{
				var guid = p0;
				var newSettings = p1;
				var data = p2;

				if (!localdata[guid]) localdata[guid] = {};

				//				var data = {
				//					photoalbumID: photoalbumID,
				//					data: data,
				//					callback: callback
				//				};

				if (localdata[guid].swfu)
					localdata[guid].swfu.destroy();

				var thisSettings = settings;
				for (k in newSettings)
				{
					thisSettings[k] = newSettings[k];
				}
				localdata[guid].swfu = new SWFUpload(settings);
			},
			DisposeUpload: function(p0)
			{
				var guid = p0;
				if (localdata[guid].swfu)
					localdata[guid].swfu.destroy();
			}
		};
	} ();
})();
