// wnditem_upload.js

var upload_infos = new List();
var _uploadIDCnt = 0;

//------------------------------------------------------------
function wnditm_upload_add(info, wnd)
	{
//	var div = wnditm_div_add(info, wnd);
//	var rect = new Rect().copyFrom(info.rect).setElem(div);
//	delete rect;
	var inputElem = $("<input type='file' name='uploadify' />")
						.attr("id","up"+_uploadIDCnt).appendTo(wnd);
	++_uploadIDCnt;
	var data = {};
	if (info.phpData) data = info.phpData;
	if (info.phpSetData) info.phpSetData(wnd, data);
	data.user_id = getuserid();
	data.infoID = upload_infos.add({info: info,wnd: wnd, data: data});
	if (info.php) var php = info.php;
	else var php = 'uploadify.php';
	inputElem.uploadify({
		'uploader'      : 'jquery/plug-ins/uploadify/uploadify.swf',
		'script'        : php,
		'cancelImg'     : 'cancel.png',
		'queueID'       : 'fileQueue',
		'auto'          : true,
		'multi'         : (info.multi != undefined),
		'folder'		: info.folder,
		'fileDataName'	: "Filedata",
		fileDesc		: info.fileDesc,
		hideButton		: true,
		sizeLimit		: 3000000000,
	//	buttonText		: "Upload",
	//	buttonImg		: "ui_images/uploadbut.jpg",
		wmode			: "transparent",
		onInit			: up_onInit,
		onSelect		: up_onSelect,
		onSelectOnce	: up_onSelectOnce,
		onCancel		: up_onCancel,
		onClearQueue	: up_onClearQueue,
		onQueueFull		: up_onQueueFull,
		onError			: up_onError,
		onOpen			: up_onOpen,
		onProgress		: up_onProgress,
		onComplete		: up_onComplete,
		onAllComplete	: up_onAllComplete,
		onCheck			: up_onCheck,
		scriptData		: data
		});
	return inputElem.attr("infoID", data.infoID);
	}

//------------------------------------------------------------
function up_onInit()
	{
	return true;
	}
//------------------------------------------------------------
function up_onSelect(event, queueID, fileObj)
	{
	
/*
fileObj.name – The name of the file 
fileObj.size – The size in bytes of the file 
fileObj.creationDate – The date the file was created 
fileObj.modificationDate – The last date the file was modified 
fileObj.type – The file extension beginning with a ‘.’ 
*/
	return true;
	}
//------------------------------------------------------------
function up_onSelectOnce(event, data)
	{
	wnditm_upload_updateSettings($(event.currentTarget));
	var infoID = $(event.currentTarget).attr("infoID");
	var o = upload_infos.get(infoID);
	if (o.info.onSelectOnce) o.info.onSelectOnce(o.wnd, o.info, data);
	return true;
	}
//------------------------------------------------------------
function up_onCancel(event, queueID, fileObj, data)
	{
	return true;
	}
//------------------------------------------------------------
function up_onClearQueue(event, data)
	{
	/*
		data.fileCount
		data.allBytesTotal
	*/
	return true;
	}
//------------------------------------------------------------
function up_onQueueFull(event, queueSizeLimit)
	{
	return true;
	}
//------------------------------------------------------------
function up_onError(event, queueID, fileObj, errorObj)
	{
	//	errorObj.type = type – Either ‘HTTP’, ‘IO’, or ‘Security’ 
	// errorObj.info = msg
	return true;
	}
//------------------------------------------------------------
function up_onProgress(event, queueID, fileObj, data)
	{
	var infoID = $(event.currentTarget).attr("infoID");
	var o = upload_infos.get(infoID);
	if (o.info.onProgress) o.info.onProgress(o.wnd, o.info, data);

		/*
	data.percentage – The current percentage completed for the upload 
	data.bytesLoaded – The current amount of bytes uploaded 
	data.allBytesLoaded – The current amount of bytes loaded for all files in the queue 
	data.speed – The current upload speed in KB/s 
*/
	return false;
	}
//------------------------------------------------------------
function up_onOpen(event, queueID, fileObj)
	{
	var infoID = $(event.currentTarget).attr("infoID");
	var o = upload_infos.get(infoID);
	if (o.info.onOpen) o.info.onOpen(o.wnd, o.info, fileObj);
	
	//	alert($(event.currentTarget).attr("id"))
	/*
	 Three arguments are sent to the function:
	 event: The event object.
	 queueID: The unique identifier of the file that was opened.
	 fileObj: An object containing details about the file that was selected.
	 •  name – The name of the file
	 •  size – The size in bytes of the file
	 •  creationDate – The date the file was created
	 •  modificationDate – The last date the file was modified
	 •  type – The file extension beginning with a '.'
	 */
	}

//------------------------------------------------------------
function up_onComplete(event, queueID, fileObj, data)
	{
	var obj = $.evalJSON(data);
	var o = upload_infos.get(obj.infoID);
	if (o.info.onCompleteFile) o.info.onCompleteFile(o.wnd, o.info, obj);
	$(event.currentTarget).uploadifySettings("scriptData", obj);
	return true;
	}
//------------------------------------------------------------
function up_onAllComplete(event, data)
	{
	var obj = $.evalJSON(data);
	var o = upload_infos.getremove(obj.infoID);
	upload_infos.remove(obj.wndID);
	if (o.info.onAllCompleteFile) o.info.onAllCompleteFile(o.wnd, o.info, obj);
	delete obj;
	return true;
	}
//------------------------------------------------------------
function up_onCheck(event, checkScript, fileQueue, folder, single)
	{
	return true;
	}
//------------------------------------------------------------
function wnditm_upload_updateSettings(uploadElem)
	{
	var phpData = uploadElem.uploadifySettings("scriptData");
	var o = upload_infos.get(phpData.infoID);
	if (o.info.phpSetData) o.info.phpSetData(o.wnd, o.data);
	uploadElem.uploadifySettings("scriptData", o.data);
	return true;
	}

