function fileQueued(file) {
	if(file.name != "")
	{
		if(dojo.byId(file.name)){
			dojo.byId(file.name).innerHTML = 'Size: <strong>'+Math.ceil(file.size*.001)+' KB</strong>';
		}
		else {
			dojo.byId("pages").innerHTML += ''+
			'<div class="box" style="width:180px;float:left;margin:5px;border:1px solid #555555">'+
				'<div class="chapitretitre">'+
					'<table align="center" width="100%" cellpadding="0" cellspacing="0" border="0"><tr>'+
						'<td align="center">^_^</td>'+
						'<td width="29"><div class="btn_delete2" onclick=\'removePage("'+file.name+'")\'"></div></td>'+
					'</tr></table>'+
				'</div>'+
				'<div class="chapitreimg" id="'+file.name+'">'+
					'<table align="center" width="100%" cellpadding="0" cellspacing="0" border="0">'+
						'<tr><td style="height:239px;background-color:#000;cursor:pointer">Size: <strong>'+Math.ceil(file.size*.001)+' KB</strong></td></tr>'+
					'</table>'+
				'</div>'+
			'</div>';
		}
	}
}

function fileDialogComplete(numFilesSelected, numFilesQueued) {
	try {
		if (numFilesQueued > 0)
			this.startUpload();
	} catch (ex) {
		this.debug(ex);
	}
}
function uploadStart(file) {
	try {
		if(dojo.byId(file.name))
			dojo.byId(file.name).innerHTML = 'Uploading...';
		return true;
	} catch (ex) {
		this.debug(ex);
		return false;
	}
}

function uploadProgress(file, bytesLoaded) {
	try {
		var percent = Math.ceil((bytesLoaded / file.size) * 100);
		if(dojo.byId(file.name))
			dojo.byId(file.name).innerHTML = 'Uploading... '+percent+'%';
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadSuccess(file, serverData) {
	try {
		if(dojo.byId(file.name))
		{
			data = dojo.fromJson(serverData);
			var node = dojo.byId(file.name);
			node.parentNode.innerHTML = ''+
				'<div class="chapitretitre">'+
					'<table align="center" width="100%" cellpadding="0" cellspacing="0" border="0"><tr>'+
						'<td align="center">'+data.nom+'</td>'+
						'<td width="29"><div class="btn_delete2" onclick=\'removePage("'+file.name+'")\'"></div></td>'+
					'</tr></table>'+
				'</div>'+
				'<div class="chapitreok" id="'+file.name+'">'+
					'<table align="center" width="100%" cellpadding="0" cellspacing="0" border="0">'+
						'<tr><td style="height:239px;background-color:#000;"><img style="border:none;max-width:180px;width:180px" src="'+data.file+'"/></td></tr>'+
					'</table>'+
				'</div>';
		}
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadComplete(file) {
	try {
		if (this.getStats().files_queued > 0) {
			this.startUpload();
		}
	} catch (ex) {
		this.debug(ex);
	}
}

function removeFx(id){
	var node = dojo.byId(id);
	if(node) {
		var fx = dojo.fadeOut({node: node.parentNode,duration:2000});
		dojo.connect(fx,"onEnd",function(){
			node.parentNode.parentNode.removeChild(node.parentNode);
		});
		fx.play();
	}
}

function uploadError(file, errorCode, message) {
	var errorName = "";
	try {
		switch (errorCode) {
			case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
			case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
			case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
			default:
				errorName = "Error";
				break;
		}
		removeFx(file.name);
	} catch (ex) {
		this.debug(ex);
	}
}

function bonusSuccess(file, serverData) {
	try {
		if(dojo.byId(file.name))
		{
			data = dojo.fromJson(serverData);
			var node = dojo.byId(file.name);
			node.parentNode.innerHTML = ''+
			'<div class="chapitretitre">'+
				'<table align="center" width="100%" cellpadding="0" cellspacing="0" border="0"><tr>'+
					'<td align="center">&nbsp;&nbsp;&nbsp;'+data.titre+'</td>'+
					'<td width="29"><div class="btn_delete2" onclick=\'removeBonus("'+data.id+'")\'"></div></td>'+
				'</tr></table>'+
			'</div>'+
			'<div class="chapitreimg" id="'+data.id+'">'+
				'<table align="center" width="100%" cellpadding="0" cellspacing="0" border="0">'+
					'<tr><td style="height:239px;background-color:#000;"><img style="border:none;max-width:180px" src="'+data.file+'"/></td></tr>'+
				'</table>'+
			'</div>';
		}
	} catch (ex) {
		this.debug(ex);
	}
}

function removeBonus(id){
	dojo.xhrPost({
		url: "bonus-delete-image.php",
		content: {id:id,manga:dojo.byId("idmanga").value},
		handleas: "json",
		load: function(data,args){
			data = dojo.fromJson(data);
			if(data.remove)
			{
				var node = dojo.byId(id);
				node.parentNode.parentNode.removeChild(node.parentNode);
			}
			else
				alert("Delete Error"+ id+" !!!");
		},
		error: function(data){
			alert("Server HS !!!"+data);
		}
	});
}

function initUploadBonus(opt){
	var addImage = new SWFUpload({
		upload_url: opt.url,
		flash_url : "js/swfupload/swfupload.swf",
		post_params: {sid:sid,manga:dojo.byId("idmanga").value},

		file_size_limit : "6 MB",
		file_types : ["*.jpg;","*.gif","*.png","*.jpg;*.gif;*.png"],
		file_types_description : ["Jpeg File","GIF File","PNG File","All Images"],
		file_upload_limit : "0",

		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 : bonusSuccess,
		upload_complete_handler : uploadComplete,
		
		button_placeholder_id : opt.btn,
		button_text: '<span class="button">' + opt.text + '</span>',
		button_text_style: ".button { font-size: 16; text-align:center;color:#FFFFFF;font-weight:bold}",
		button_width: 150,
		button_height: 18,
		button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
		button_cursor: SWFUpload.CURSOR.HAND,

		debug: false
	});
}

function avatarStart(file) {
	try {
		if(dojo.byId("transAvatar"))
			dojo.byId("transAvatar").innerHTML = '0%';
		return true;
	} catch (ex) {
		this.debug(ex);
		return false;
	}
}

function avatarProgress(file, bytesLoaded) {
	try {
		var percent = Math.ceil((bytesLoaded / file.size) * 100);
		if(dojo.byId("transAvatar"))
			dojo.byId("transAvatar").innerHTML = percent+'%';
	} catch (ex) {
		this.debug(ex);
	}
}

function avatarError(file, errorCode, message) {
	var errorName = "";
	try {
		switch (errorCode) {
			case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
			case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
			case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
			default:
				errorName = '<span class="warning">Error</span>';
				break;
		}
		if(dojo.byId("transAvatar"))
			dojo.byId("transAvatar").innerHTML = errorName;
	} catch (ex) {
		this.debug(ex);
	}
}


function avatarSuccess(file, serverData) {
	try {
		data = dojo.fromJson(serverData);
		if(dojo.byId("transAvatar"))
			dojo.byId("transAvatar").innerHTML = 'Complete';
		if(dojo.byId("imgprofil"))
			dojo.byId("imgprofil").innerHTML = '<div style="width:100px;height:100px;background:url('+data.file+')"></div>';
	} catch (ex) {
		this.debug(ex);
	}
}

function miniatureSuccess(file, serverData) {
	try {
		data = dojo.fromJson(serverData);
		if(dojo.byId("transAvatar"))
			dojo.byId("transAvatar").innerHTML = 'Complete';
		if(dojo.byId("miniature"))
			dojo.byId("miniature").innerHTML = '<img src="'+data.file+'"/>';
	} catch (ex) {
		this.debug(ex);
	}
}

function initUploadAvatar(opt){
	var addAvatar = new SWFUpload({
		upload_url: opt.url,
		flash_url : "js/swfupload/swfupload.swf",
		post_params: {sid:sid},

		file_size_limit : "2 MB",
		file_types : ["*.jpg;","*.png","*.jpg;*.png"],
		file_types_description : ["Jpeg File","PNG File","All Images"],
		file_upload_limit : "0",

		file_queue_error_handler : fileQueueError,
		file_dialog_complete_handler : fileDialogComplete,

		upload_start_handler : avatarStart,
		upload_progress_handler : avatarProgress,
		upload_error_handler : avatarError,
		upload_success_handler : avatarSuccess,
		upload_complete_handler : uploadComplete,
		
		button_placeholder_id : opt.btn,
		button_text: '<span class="button">' + opt.text + '</span>',
		button_text_style: ".button { text-align:center;color:#555555;font-size:16px;font-weight:bold;height:25px;line-height:25px;}",
		button_width: 170,
		button_height: 25,
		button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
		button_cursor: SWFUpload.CURSOR.HAND,

		debug: false
	});
}

function initUploadPage(opt){
	var addAvatar = new SWFUpload({
		upload_url: opt.url,
		flash_url : "js/swfupload/swfupload.swf",
		post_params: {sid:sid},

		file_size_limit : "2 MB",
		file_types : ["*.jpg;","*.png","*.jpg;*.png"],
		file_types_description : ["Jpeg File","PNG File","All Images"],
		file_upload_limit : "0",

		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,

		
		button_placeholder_id : opt.btn,
		button_image_url : "js/swfupload/add.png",
		button_width: 24,
		button_height: 24,
		button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
		button_cursor: SWFUpload.CURSOR.HAND,

		debug: false
	});
}

function initUploadMiniature(opt){
	var addMiniature = new SWFUpload({
		upload_url: opt.url,
		flash_url : "js/swfupload/swfupload.swf",
		post_params: {sid:sid,id:opt.id},

		file_size_limit : "2 MB",
		file_types : ["*.jpg;","*.png","*.jpg;*.png"],
		file_types_description : ["Jpeg File","PNG File","All Images"],
		file_upload_limit : "0",

		file_queue_error_handler : fileQueueError,
		file_dialog_complete_handler : fileDialogComplete,

		upload_start_handler : avatarStart,
		upload_progress_handler : avatarProgress,
		upload_error_handler : avatarError,
		upload_success_handler : miniatureSuccess,
		upload_complete_handler : uploadComplete,
		
		button_placeholder_id : opt.btn,
		button_text: '<span class="button">' + opt.text + '</span>',
		button_text_style: ".button { text-align:center;color:#555555;font-size:16px;font-weight:bold;height:25px;line-height:25px;}",
		button_width: 170,
		button_height: 25,
		button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
		button_cursor: SWFUpload.CURSOR.HAND,

		debug: false
	});
}