/*
	A simple class for displaying file information and progress
	Note: This is a demonstration only and not part of SWFUpload.
	Note: Some have had problems adapting this class in IE7. It may not be suitable for your application.
*/

// Constructor
// file is a SWFUpload file object
// targetID is the HTML element id attribute that the FileProgress HTML structure will be added to.
// Instantiating a new FileProgress object with an existing file will reuse/update the existing DOM elements
function photoProgress(file, targetID, serverData) {
	this.fileProgressID = file.id;
	this.serverData = serverData;

	this.opacity = 100;
	this.height = 0;

	this.fileProgressWrapper = document.getElementById(this.fileProgressID);
	if (!this.fileProgressWrapper) {
		this.fileProgressWrapper = document.createElement("div");
		this.fileProgressWrapper.className = "photo-item";
		this.fileProgressWrapper.id = this.fileProgressID;
		this.fileProgressWrapper.style.width = 0;
	
		this.fileProgressWrapper.innerHTML = '<a class="x" href="javascript:;"></a><div class="photo"><div class="prog-status"></div><div class="prog-proc"></div><div class="prog-proc-line"><div style="width: 0;"></div></div></div>';
		
		//document.getElementById(targetID).appendChild(this.fileProgressWrapper);
		var $photoPlace = $('#'+targetID).find('div.photo-item:last');
		if (!$photoPlace.length)
			$( '#'+targetID ).children(':first').before(this.fileProgressWrapper);
		else
			$('#'+targetID).find('div.photo-item:last').after(this.fileProgressWrapper);
		$(this.fileProgressWrapper).animate({'width' : 43});
	} else {
		this.fileProgressElement = this.fileProgressWrapper.firstChild;
	}

	this.height = this.fileProgressWrapper.offsetHeight;

}
photoProgress.prototype.setProgress = function (percentage) {
	$("div.prog-proc", this.fileProgressWrapper).html( percentage + '%' );
	$("div.prog-proc-line div", this.fileProgressWrapper).css( "width", percentage + '%' );
};
photoProgress.prototype.setComplete = function () {
	this.fileProgressWrapper.onmouseover = function() {
		this.className = 'photo-item photo-item-hover';
	}
	this.fileProgressWrapper.onmouseout = function() {
		this.className = 'photo-item';
	}

	if (this.serverData) {
		eval( this.serverData );
	} else {
		this.setError();
	}		

	
	/*var oSelf = this;
	setTimeout(function () {
		oSelf.disappear();
	}, 2000);
	*/
};
photoProgress.prototype.setError = function ( msg ) {
	var oSelf = this;
	if (typeof msg == 'undefined') msg = SWFUpload.msg['Server (IO) Error'];
	this.setStatus( msg );
	$("div.prog-proc", this.fileProgressWrapper).fadeOut();
	$("div.prog-proc-line div", this.fileProgressWrapper).fadeOut();
	setTimeout(function () {
		$(oSelf.fileProgressWrapper).animate({width: 0, height: 0}, 'slow', false, function() { $(this).hide(); });
	}, 2000);
};
photoProgress.prototype.setCancelled = function () {
	var oSelf = this;
	setTimeout(function () {
		oSelf.disappear();
	}, 2000);
};
photoProgress.prototype.setStatus = function (status, uploadStart) {
	//$("div.prog-status", this.fileProgressWrapper).html( status );
};

// Show/Hide the cancel button
photoProgress.prototype.toggleCancel = function (show, swfUploadInstance) {

	/*
	this.fileProgressElement.childNodes[0].style.visibility = show ? "visible" : "hidden";
	if (swfUploadInstance) {
		var fileID = this.fileProgressID;
		this.fileProgressElement.childNodes[0].onclick = function () {
			swfUploadInstance.cancelUpload(fileID);
			return false;
		};
	}
	*/
};

// Fades out and clips away the FileProgress box.
photoProgress.prototype.disappear = function () {

	var reduceOpacityBy = 15;
	var reduceHeightBy = 4;
	var rate = 30;	// 15 fps

	if (this.opacity > 0) {
		this.opacity -= reduceOpacityBy;
		if (this.opacity < 0) {
			this.opacity = 0;
		}

		if (this.fileProgressWrapper.filters) {
			try {
				this.fileProgressWrapper.filters.item("DXImageTransform.Microsoft.Alpha").opacity = this.opacity;
			} catch (e) {
				// If it is not set initially, the browser will throw an error.  This will set it if it is not set yet.
				this.fileProgressWrapper.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + this.opacity + ")";
			}
		} else {
			this.fileProgressWrapper.style.opacity = this.opacity / 100;
		}
	}

	if (this.height > 0) {
		this.height -= reduceHeightBy;
		if (this.height < 0) {
			this.height = 0;
		}

		this.fileProgressWrapper.style.height = this.height + "px";
	}

	if (this.height > 0 || this.opacity > 0) {
		var oSelf = this;
		setTimeout(function () {
			oSelf.disappear();
		}, rate);
	} else {
		this.fileProgressWrapper.style.display = "none";
	}
};

