function ProgressWidget(wrapper, scaleMax)
{
	this.wrapper=$(wrapper);
	this.value=0;
	this.percent=0;
	this.bar = document.createElement("div");
	this.bar.className='progress-bar';
	if (!this.wrapper.className || this.wrapper.className == "")
		this.wrapper.className = "progress-widget";
	this.wrapper.appendChild(this.bar);
	this.wrap = wrapper;
	this.scaleMax = scaleMax || 100;
}

ProgressWidget.prototype = {
	setPercent: function(percent) {
		this.value=(percent*this.scaleMax)/100;
		this.percent=percent;
		this.update();
	},
	setValue: function(value) {
		this.value=value;
		var percent=(value*100)/this.scaleMax;
		this.percent = Math.round(percent);
	},
	setScale: function(max) {
		this.scaleMax=max;
	},
	setProgress: function(value, max) {
		this.scaleMax = max;
		this.setValue(value);
		this.update();
	},
	update: function() {
		var pct=this.percent+"%";
		this.bar.innerHTML=pct;
		this.bar.style.width = pct;
	}
};
var failedCount=0;
var bg_swf;
var progressListenerFunction=false;
var moreToCome=false;
var uploadInProgress=false;
function initProgressListener() {
	try {
		if (progressListenerFunction != false) return;

		progressListenerFunction = function(id) {
			//if (moreToCome) {
			
				setTimeout("bg_swf.resume()",100);
			//}
		};
		Event.observeCustom("imageProcessed",progressListenerFunction);
	} catch(err) {alert(err);}
}


function hideFlashDemo(hidden) {
	uploadInProgress=hidden;
	var flashdemo = $("flashdemo");
	if (!flashdemo)
		return;
	if (hidden)
		flashdemo.innerHTML="";
	else
		makeTag();
}

function swfGetVar(name) {
	return bg_swf.getVar(name);
}
function swfResumeUpload() {
	try {
		bg_swf.resume();
	} catch(err) {
		alert(err);
	}
}
function showUploadDialog(isRetry)
{
	if (!isRetry)
	{
		setTimeout("showUploadDialog('first')",500);
		return;
	}	
	hideFlashDemo(true);
	failedCount=0
	initProgressListener();
	try {
		$("statusTxt").innerHTML="Choose your photos...";
		fadeStatusPanel(true,1);
	} catch(err) {
		alert(err.message);
	}

	var dialogShown=false;
	try {
		bg_swf  = document.getElementById("uploader");
		bg_swf.showDialog();
		var dialogShown=true;
	} catch(err) {
		bg_swf = document["uploader"];
		bg_swf.showDialog();
		var dialogShown=true;
	}
	if (!dialogShown && isRetry!='retry')
	{
		setTimeout("showUploadDialog('retry')",1000);
	}

}
function jsUploadCancel()
{
	$("statusTxt").innerHTML = "Canceled";
	fadeStatusPanel(false,3);
	hideFlashDemo(false);
}

function jsUploadOpen(filename, filesize)
{
	$("statusTxt").innerHTML="open " + filename + ":" + filesize;
	progressBar.setProgress(0,filesize);
	fadeStatusPanel(true,4);
}

function jsUploadProgress(filename, bytesLoaded, bytesTotal, fileArrayID, fileArrayTotal)
{

	try {
		if (!bytesTotal)
			return;
		progressBar.setProgress(bytesLoaded,bytesTotal);
		$("statusTxt").innerHTML = "File (" + fileArrayID + " of " + fileArrayTotal + ") progress: " + progressBar.percent + "%"; // copied " + bytesLoaded + " of " + bytesTotal + "<br/>" + filename;
	} catch(err) {}
}
/*
function jsUploadProgress(filename, bytesLoaded, bytesTotal, fileArrayID, fileArrayTotal)
{
	alert("progress: " + bytesLoaded);
	try {
		progressBar.setProgress(bytesLoaded,bytesTotal);
		$("statusTxt").innerHTML = "File (" + fileArrayID + " of " + fileArrayTotal + ") progress: " + bytesLoaded + " of " + bytesTotal + " bytes (" + progressBar.percent + "%)"; // copied " + bytesLoaded + " of " + bytesTotal + "<br/>" + filename;
	} catch(err) {alert(err); }
}*/

function jsAfterUploadComplete(fileNum, fileCount) {
	//progressBar.setPercent(0);
	$("statusTxt").innerHTML = "starting file #" + (fileNum+1) + " of " + fileCount;
	//if (fileNum > 0 && fileNum < fileCount){
	//	bg_swf.resume();
	//}
}


function jsUploadDone() {

}
function jsUploadComplete( filename, fileNum, fileCount)
{
	progressBar.setPercent(100);

	try {
		$("statusTxt").innerHTML = "File (" + fileNum + " of " + fileCount + ") upload complete";
		if (fileNum > 0 && fileNum < fileCount)
		{
			moreToCome = true;
			setTimeout("jsAfterUploadComplete(" + fileNum + "," + fileCount +")",90);
		} else {
			moreToCome=false;
			hideFlashDemo(false);
		}
	} catch(err) { alert(err); }

	try {
		var num = sidOffset+fileNum;
		var id = sidPrefix + "." + num;
		processImage(id);
	
		if (fileNum == fileCount)
		{
			moreToCome=false;
			sidOffset+= fileCount;
			if (failedCount < 1) {
				fadeStatusPanel(false,2);
				hideFlashDemo(false);
			} else {
				$("statusTxt").innerHTML = "<img src='/lib/icons/error.png' align='absmiddle'/> " + failedCount + " of " + fileCount + " files skipped.";
			}
		}

	} catch(err) { alert(err); }
}
function jsUploadHTTPError(filename, httpError)
{
	$("statusTxt").innerHTML = "(" + filename + ") http error: " + httpError;
}
function jsUploadIOError(filename)
{
	$("statusTxt").innerHTML = filename+": io error while reading file";
}
function jsUploadSizeError(filename, bytesTotal, fileNum, fileCount)
{
	sidOffset-=1;
	failedCount+=1;
	$("statusTxt").innerHTML = "<img src='/lib/icons/error.png' align='absmiddle'/> " + filename+" is over 2MB file size limit!";
	bg_swf.resume();

}
function jsUploadGetVarReply(message) {
	alert(message);
}
function jsSecurityError(filename, errorString)
{
	$("statusTxt").innerHTML = "("+filename+") Security error: " + errorString;
}
function showCompletedThumb(fileNum,title) {

	var num = sidOffset+fileNum;
	var id = sidPrefix + "." + num;

	if (!title)
		title = "image_" + num;
	else
	{
		var idx = title.lastIndexOf(".");
		if (idx > -1) {
			title = title.substr(0,idx);
		}
	}
	var thumb = thumbs.add(id,id,title, "selected");
	thumb.onclick = function() { selectThumb(this); };
	return;

	var container = document.createElement('div');
	container.className='thumbcontainer';
	container.onclick = function() { selectThumb(this); };
	var img = document.createElement('img');
	img.className='lightboxThumb';
	img.src="view.php?type=thumb&id="+id;
	img.title=title;
	img.alt=title;

	container.appendChild(img);

	var caption = document.createElement('input');
	caption.setAttribute("type","text");
	caption.className='caption';
	caption.value = title;
	caption.onchange = function() { this.parentNode.title=this.value; };
	caption.title='Photo Caption';

	var thumb = document.createElement('div');
	thumb.className='lightboxThumbFrame';
	thumb.appendChild(container);
	thumb.appendChild(caption);
	thumb.id=id;
	thumbs.insertBefore(thumb,thumbs.firstChild);
}

function fadeStatusPanel(visible,timer) {
		if (!timer)
			timer = 2;
		panel=$("uploadStatusPanel");
		if (!panel)
			return;
		if (Effect)
		{
			var doit;
			if (visible)	doit = function() {Effect.Appear(panel,{duration:1}); }
			else			doit = function() {Effect.Fade(panel,{duration:1}); }
			setTimeout(doit, timer*1000 );
		}
		else
		{
			if (visible)
				panel.style.display='';
			else
				panel.style.display='none';
		}
}

function dlux_log(msg) {
	try {
		if (console)
			console.log(msg);
	} catch(err) {
	}
}
