/* Demo Note:  This demo uses a FileProgress class that handles the UI for displaying the file name and percent complete.
The FileProgress class is not part of SWFUpload.
*/
/* **********************
   Event Handlers
   These are my custom event handlers to make my
   web application behave the way I went when SWFUpload
   completes different tasks.  These aren't part of the SWFUpload
   package.  They are part of my application.  Without these none
   of the actions SWFUpload makes will show up in my application.
   ********************** */
function fileQueued(file) {
	try {
		$("#photothumbs").scrollTop(0);
		$("#photothumbs").prepend("<div class=\"thumbuploading\" id=\"uploading"+file.id+"\"><div class=\"status\">Wating...<br /><a href=\"javascript:g_uploader.cancelUpload('"+file.id+"')\">Cancel</a></div></div>");
	} catch (ex) {
		this.debug(ex);
	}
}
function fileQueueError(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;
		}
		switch (errorCode) {
		case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
			alert("파일이 너무 큽니다.");
			this.debug("Error Code: File too big, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
			alert("0바이트 크기 파일은 올릴 수 없습니다.");
			this.debug("Error Code: Zero byte file, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
			alert("올릴 수 없는 파일입니다.");
			this.debug("Error Code: Invalid File Type, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		default:
			if (file !== null) {
				alert("오류가 발생했습니다.");
			}
			this.debug("Error Code: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		}
	} catch (ex) {
        this.debug(ex);
    }
}

function fileDialogComplete(numFilesSelected, numFilesQueued) {
	try {
		if(numFilesSelected > 0) {
			$("#addphoto").css("display", "none");
		}
		/* I want auto start the upload and I can do that here */
		this.startUpload();
	} catch (ex)  {
        this.debug(ex);
	}
}

function uploadStart(file) {
	try {
		$("#uploading"+file.id+" > .status").html("<img src=\"../images/loading.gif\" alt=\"uploading...\" title=\"취소\" onclick=\"g_uploader.cancelUpload('"+file.id+"')\"/><div id=\"upstatus"+file.id+"\">0%</div>");
	}
	catch (ex) {}
	return true;
}
function uploadProgress(file, bytesLoaded, bytesTotal) {
	try {
		if(bytesTotal > 0)
			$("#upstatus"+file.id).html(parseInt(bytesLoaded * 100 / bytesTotal) + "%");
	} catch (ex) {
		this.debug(ex);
	}
}
function uploadSuccess(file, serverData) {
	try {
		if(serverData && serverData.indexOf("OK") == 0)
		{
			var info = serverData.split("|");
			//var topm = parseInt((150 - parseInt(info[4])) / 2);
			var thumbpath = "../files/storephoto/"+g_area+"/"+g_store+"/thumb/";
		 	var html = "<div id=\"photo"+info[1]+"\" class=\"thumb\" onclick=\"viewPhoto('"+info[1]+"','"+g_area+"','"+g_store+"','"+info[2]+"',true)\" style=\"background:url("+thumbpath+info[2]+") no-repeat center center\"></div>";

			$("#uploading"+file.id).replaceWith(html);
			
			$("#photo"+info[1]).mouseover(function(){
				$(this).addClass("hot");
			}).mouseout(function(){
				$(this).removeClass("hot");
			});
			
			var count = $("#photo .thumb").length;
			$("#photoview").html('');
			
			if(count == 1 && $("#photo .right").html().length < 10){
				$("#photo .buttons .right").html("<div id=\"addphoto\"></div>");
				g_uploader = new SWFUpload({
				flash_url: "../uploader/swfupload.swf",
				upload_url: "../uploader/uploadphoto.php",
				post_params: {uemail:g_member,store: g_store},
				file_size_limit: "2 MB",
				file_types: "*.jpg;*.png;*.gif",
				file_types_description: "이미지",
				file_upload_limit: 100,
				file_queue_limit: 0,
				// Button settings
				button_image_url: "../images/store/addphoto.gif",
				button_width: 98,
				button_height: 26,
				button_placeholder_id: "addphoto",			
				button_cursor: SWFUpload.CURSOR.HAND,
				// The event handler functions are defined in handlers.js
				file_queued_handler : fileQueued,
				file_queue_error_handler : fileQueueError,
				file_dialog_complete_handler : fileDialogComplete,
				upload_start_handler : uploadStart,
				upload_progress_handler : uploadProgress,
				upload_error_handler : uploadError,
				upload_success_handler : uploadSuccess,
				upload_complete_handler : uploadComplete,
				queue_complete_handler : queueComplete
				});
			}
			
			viewPhoto(info[1],g_area,g_store,info[2],true);
			$("#photo .sectiontitle .count").html(count);

		}else
		{
			alert(serverData);
			var rm = $("#uploading"+file.id).remove();
			rm = null;
		}
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadError(file, errorCode, message) {
	try {
		switch (errorCode) {
		case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
			alert("업로드 실패: " + message);
			this.debug("Error Code: HTTP Error, File name: " + file.name + ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
			alert("업로드에 실패했습니다.");
			this.debug("Error Code: Upload Failed, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.IO_ERROR:
			alert("Server (IO) Error");
			this.debug("Error Code: IO Error, File name: " + file.name + ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
			alert("Security Error");
			this.debug("Error Code: Security Error, File name: " + file.name + ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
			alert("파일 크기가 너무 큽니다.");
			this.debug("Error Code: Upload Limit Exceeded, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:
			alert("Failed Validation.  Upload skipped.");
			this.debug("Error Code: File Validation Failed, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
			// If there aren't any files left (they were all cancelled) disable the cancel button
			if (this.getStats().files_queued === 0) {
				$("#uploadbtn").css("display", "");
			}
			break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
			alert("업로드가 중단되었습니다.");
			break;
		default:
			alert("업로드 오류: " + errorCode);
			this.debug("Error Code: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		}
		var rm = $("#uploading"+file.id).remove();
		rm = null;

	} catch (ex) {
        this.debug(ex);
    }
}

function uploadComplete(file) {
	if (this.getStats().files_queued === 0) {
		$("#addphoto").css("display", "");
		//curvyCorners({tl:{radius:6},tr:{radius:6},bl:{radius:6},br:{radius:6},antiAlias:true},".storephotobox");		
	}
}
// This event comes from the Queue Plugin
function queueComplete(numFilesUploaded) {
	//var status = document.getElementById("divStatus");
	//status.innerHTML = numFilesUploaded + " file" + (numFilesUploaded === 1 ? "" : "s") + " uploaded.";
}
