// jq_fotobox.js


//---------------------------------------------
$.fn.fotobox = function(o)
	{
	var fb = $(this).addClass("fotobox");
	if (o.doneUploadAll) fb.bind("uploadComplete", function(){o.doneUploadAll(fb.attr("lastfile"))});
	if (!o.uploadCmd) o.uploadCmd = "upload_photo";
	if (o.type_id) fb.attr({type_id: o.type_id});

	$(this).upload({
		type: "upload",
		php: "uploadify.php",
		vis: true,
		enabled: true,
		folder: "uploads",
		multi: (o.uploadOne == undefined)?true:undefined,
		fileDesc: "Bazooka",
		phpData: {
			command: o.uploadCmd,
			folder_name: "what-ever"
		},
		phpSetData: function(wnd, data){
			fotobox_upload_phpSetData(data, fb);
			if (o.src) data.src = o.src;
			},
		onSelectOnce: function(wnd, info, data){fotobox_upload_SelectOnce(info, data, fb);},
		onOpen: function(wnd, info, fileObj){fotobox_upload_Open(info, fileObj, fb);},
		onProgress: function(wnd, info, data){
			fotobox_upload_Progress(info, data, fb);},
		onCompleteFile: function(wnd, info, rec){
			if (o.completeFile) o.completeFile(rec);
			fotobox_upload_completeFile(info, rec, fb);},
		onAllCompleteFile: function(wnd, info, rec){
			fotobox_upload_allCompleteFile(info, rec, fb);}
	});
	
	fb.bind("incsize", function(){
		$(this).find(".iconbox").iconboxIncSize();
		})
	.bind("decsize",function(){
		$(this).find(".iconbox").iconboxDecSize();
		});
	if (!o.noicons) fb.append($(_div).iconbox(o).iconbox_folders());
	}

//------------------------------------------
function fotobox_upload_phpSetData(data, fb)
	{
	data.folder_name = "Untitled";
	data.visit_id = gUser.visit_id;
	data.folder_id = fb.find(".iconbox").attr("folder_id");
	if (fb.attr("type_id")) data.type_id = fb.attr("type_id");
	}

//------------------------------------------
function fotobox_upload_SelectOnce(info, data, fb)
	{
	var ib = fb.find(".iconbox");
	if (ib) {
		var folder_id = fb.find(".iconbox").attr("folder_id");
		if (folder_id==0) ib.empty().iconboxAddBack();
	}
	fb.attr({
			filesSelected:data.filesSelected,
			allBytesTotal:data.allBytesTotal,
			uploadedFiles:0,
			allBytesLoaded:0,
			currFile:"",
			currFileSize:0,
			bytesLoaded:0,
			speed:0
			})
	.removeAttr("inited")
	.removeAttr("allDone")

	var x=fb.attr("allBytesLoaded");
	fb.fotobox_upload_showProgress();
	}

//------------------------------------------
function fotobox_upload_Open(info, fileObj, fb)
	{
	fb.attr({currFile: fileObj.name, currFileSize: fileObj.size})
	.fotobox_upload_showProgress();
	}

//------------------------------------------
function fotobox_upload_Progress(info, data, fb)
	{
	fb.attr({bytesLoaded: data.bytesLoaded, allBytesLoaded: data.allBytesLoaded, speed: data.speed})
	.fotobox_upload_showProgress();
	}
	
//------------------------------------------
function fotobox_upload_completeFile(info, rec, fb)
	{
	var ib = fb.find(".iconbox");
	if (ib) ib.iconboxAdd(rec);
	var files = 1+parseInt(fb.attr("uploadedFiles"));
	fb.attr("uploadedFiles", files);
	if (files==parseInt(fb.attr("filesSelected")))
		{
		fb.attr("allDone", true);
		}
	fb.attr("lastfile",rec.dest);
	fb.fotobox_upload_showProgress();
	}
//------------------------------------------
function fotobox_upload_allCompleteFile(info, rec, fb)
	{
	fb.attr("allDone", true);
	fb.fotobox_upload_showProgress();
	}

//---------------------------------------------
$.fn.fotobox_upload_showProgress = function()
	{
	var fb = $(this);
	var totalKBs = getMemoryStr(fb.attr("allBytesTotal"));
	var dwndKBs = getMemoryStr(fb.attr("allBytesLoaded"));
	if (!fb.attr("inited")) 
		{
			//----- BEGIN UPLOADING ------
		var filesSel = parseInt(fb.attr("filesSelected"));
		if (filesSel>1) var plural = "s";
		else plural = "";
		fb.fotobox_prog_init("Uploading " + filesSel + " file"+plural+" (" + totalKBs + ")");
		fb.attr("inited",true);
		}
	else
		{	//----- CONTINUE UPLOADING ------
		if (!fb.attr("allDone"))
			{
			fb.fotobox_prog_update(fb.attr("allBytesTotal"), fb.attr("allBytesLoaded"), 
				"Uploading "+(parseInt(fb.attr("uploadedFiles"))+1)+" of "+fb.attr("filesSelected")+
				"\ \ \ ("+dwndKBs+" of "+totalKBs+")");
			}
		else //----- END UPLOADING ------
			{
			if (parseInt(fb.attr("uploadedFiles"))>1) var s = fb.attr("uploadedFiles")+" files have";
			else if (parseInt(fb.attr("uploadedFiles"))==1) var s = fb.attr("uploadedFiles")+" file has";
			else var s = "No files have";
			fb.fotobox_prog_end(s+" been uploaded.\ \ ("+dwndKBs+")")
			.trigger("uploadComplete");
			}
		}
	}

//---------------------------------------------
$.fn.fotobox_getmsgbar = function()
	{
	return $(".uploadStatus");
	}
//---------------------------------------------
$.fn.fotobox_setmsgbarTxt = function(txt)
	{
	$(this).fotobox_getmsgbar().html(txt);
	return $(this);
	}
//---------------------------------------------
$.fn.fotobox_prog_init = function(txt)
	{
	$(this).fotobox_setmsgbarTxt(txt);
	return $(this);
	}
//------------------------------------------------------------------
$.fn.fotobox_prog_update = function(max, val, txt)
	{
	//$(this).fotobox_getmsgbar().css("width", 
	//($(this).fotobox_getmsgbar().width() * val) / max);
	if (txt) $(this).fotobox_setmsgbarTxt(txt);
	return $(this);
	}
//------------------------------------------------------------------
$.fn.fotobox_prog_end = function(txt)
	{
	//$(this).children(".wnd_botprogbar").remove();
	if (txt) $(this).fotobox_setmsgbarTxt(txt);
	return $(this);
	}