/*
	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 FileProgress(file, targetID) {
	this.fileProgressID = file.id;

	this.opacity = 100;
	this.height = 0;


	this.fileProgressWrapper = document.getElementById(this.fileProgressID);
	if (!this.fileProgressWrapper) {
		this.fileProgressWrapper = document.createElement("div");
		this.fileProgressWrapper.className = "progressWrapper";
		this.fileProgressWrapper.id = this.fileProgressID;

		this.fileProgressElement = document.createElement("div");
		this.fileProgressElement.className = "progressContainer";

		var fileProgressElement1 = document.createElement("div");
		fileProgressElement1.className = "slist";

		var progressNameP = document.createElement("p");
		progressNameP.appendChild(document.createTextNode(file.name));

		var progressTextWrapper = document.createElement("div");
		progressTextWrapper.className = "fname";
		progressTextWrapper.appendChild(progressNameP);
		progressTextWrapper.appendChild(progressNameP);

		var progressSizeP = document.createElement("p");
		progressSizeP.appendChild(document.createTextNode(size_format(file.size)));

		var progressSizeWrapper = document.createElement("div");
		progressSizeWrapper.className = "size";
		progressSizeWrapper.appendChild(progressSizeP);

		var progressClear = document.createElement("div");
		progressClear.className = "clear";
		progressClear.innerHTML = "";

		var progressCancel = document.createElement("a");
		progressCancel.href = "#";
		progressCancel.className = "progressCancel";
		progressCancel.appendChild(document.createTextNode("Удалить"));

		var progressCancelP = document.createElement("p");
		progressCancelP.className = "del";
        progressCancelP.style.visibility = "hidden";
		progressCancelP.appendChild(progressCancel);

		var progressCancelWrapper = document.createElement("div");
		progressCancelWrapper.className = "action";
		progressCancelWrapper.appendChild(progressCancelP);


		var progressBar = document.createElement("div");
		progressBar.className = "progressBarInProgress";

		var leftCorner = document.createElement("div");
		leftCorner.className = "corners left";

		var rightCorner = document.createElement("div");
		rightCorner.className = "corners right";

		var progressBarWrapper = document.createElement("div")
		progressBarWrapper.className = "progressBarWrapper";
		progressBarWrapper.appendChild(progressBar);
		progressBarWrapper.appendChild(leftCorner);
		progressBarWrapper.appendChild(rightCorner);

		var progressStatus = document.createElement("div");
		progressStatus.className = "progressBarStatus";
		progressStatus.innerHTML = "";

		progressTextWrapper.appendChild(progressBarWrapper);
        progressTextWrapper.appendChild(progressStatus);


		fileProgressElement1.appendChild(progressTextWrapper);
		fileProgressElement1.appendChild(progressSizeWrapper);
		fileProgressElement1.appendChild(progressCancelWrapper);
		fileProgressElement1.appendChild(progressClear);

        this.fileProgressElement.appendChild(fileProgressElement1);

		this.fileProgressWrapper.appendChild(this.fileProgressElement);

		document.getElementById(targetID).appendChild(this.fileProgressWrapper);
	} else {
		this.fileProgressElement = this.fileProgressWrapper.firstChild;
		this.reset();
	}

	this.height = this.fileProgressWrapper.offsetHeight;
	this.setTimer(null);


}

FileProgress.prototype.setTimer = function (timer) {
	this.fileProgressElement["FP_TIMER"] = timer;
};
FileProgress.prototype.getTimer = function (timer) {
	return this.fileProgressElement["FP_TIMER"] || null;
};

FileProgress.prototype.reset = function () {
	this.fileProgressElement.className = "progressContainer";

	this.fileProgressElement.childNodes[0].childNodes[0].childNodes[2].innerHTML = "";
	this.fileProgressElement.childNodes[0].childNodes[0].childNodes[2].className = "progressBarStatus";

	this.fileProgressElement.childNodes[0].childNodes[0].childNodes[1].childNodes[0].className = "progressBarInProgress";
	this.fileProgressElement.childNodes[0].childNodes[0].childNodes[1].childNodes[0].style.width = "0%";

	this.appear();
};

FileProgress.prototype.setProgress = function (percentage) {
	this.fileProgressElement.className = "progressContainer green";
	this.fileProgressElement.childNodes[0].childNodes[0].childNodes[1].childNodes[0].className = "progressBarInProgress";
	this.fileProgressElement.childNodes[0].childNodes[0].childNodes[1].childNodes[0].style.width = percentage + "%";

	this.appear();
};
FileProgress.prototype.setComplete = function () {
	this.fileProgressElement.className = "progressContainer";
	this.fileProgressElement.childNodes[0].childNodes[0].childNodes[1].childNodes[0].className = "progressBarComplete";
	this.fileProgressElement.childNodes[0].childNodes[0].childNodes[1].childNodes[0].style.width = "";

	var oSelf = this;
//	this.setTimer(setTimeout(function () {
//		oSelf.disappear();
//	}, 10000));
};
FileProgress.prototype.setError = function () {
	this.fileProgressElement.className = "progressContainer red";
	this.fileProgressElement.childNodes[0].childNodes[0].childNodes[1].childNodes[0].className = "progressBarError";
	this.fileProgressElement.childNodes[0].childNodes[0].childNodes[1].childNodes[0].style.width = "";

	var oSelf = this;
	this.setTimer(setTimeout(function () {
		oSelf.disappear();
	}, 3000));
};
FileProgress.prototype.setCancelled = function () {
	this.fileProgressElement.className = "progressContainer";
	this.fileProgressElement.childNodes[0].childNodes[0].childNodes[1].childNodes[0].className = "progressBarError";
	this.fileProgressElement.childNodes[0].childNodes[0].childNodes[1].childNodes[0].style.width = "";

	this.disappear();
};
FileProgress.prototype.setStatus = function (status) {
	this.fileProgressElement.childNodes[0].childNodes[0].childNodes[2].innerHTML = status;
};

// Show/Hide the cancel button
FileProgress.prototype.toggleCancel = function (show, swfUploadInstance) {

	this.fileProgressElement.childNodes[0].childNodes[2].childNodes[0].style.visibility = show ? "visible" : "hidden";
	if (swfUploadInstance) {
		var fileID = this.fileProgressID;
		this.fileProgressElement.childNodes[0].childNodes[2].childNodes[0].onclick = function () {
			swfUploadInstance.cancelUpload(fileID);
			return false;
		};
	}
};

FileProgress.prototype.appear = function () {
	if (this.getTimer() !== null) {
		clearTimeout(this.getTimer());
		this.setTimer(null);
	}

	if (this.fileProgressWrapper.filters) {
		try {
			this.fileProgressWrapper.filters.item("DXImageTransform.Microsoft.Alpha").opacity = 100;
		} 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=100)";
		}
	} else {
		this.fileProgressWrapper.style.opacity = 1;
	}

	this.fileProgressWrapper.style.height = "";

	this.height = this.fileProgressWrapper.offsetHeight;
	this.opacity = 100;
	this.fileProgressWrapper.style.display = "";

};

// Fades out and clips away the FileProgress box.
FileProgress.prototype.disappear = function () {
	var reduceOpacityBy = 15;
	var reduceHeightBy = 4;
	var rate = 10;	// 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;
		this.setTimer(setTimeout(function () {
			oSelf.disappear();
		}, rate));
	} else {
		this.fileProgressWrapper.style.display = "none";
		this.setTimer(null);
	}
};


function size_format (filesize) {
	if (filesize >= 1073741824) {
	     filesize = number_format(filesize / 1073741824, 2, '.', '') + ' Gb';
	} else {
		if (filesize >= 1048576) {
     		filesize = number_format(filesize / 1048576, 2, '.', '') + ' Mb';
   	} else {
			if (filesize >= 1024) {
    		filesize = number_format(filesize / 1024, 0) + ' Kb';
  		} else {
    		filesize = number_format(filesize, 0) + ' bytes';
			};
 		};
	};
  return filesize;
};

function number_format( number, decimals, dec_point, thousands_sep ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://crestidg.com)
    // +     bugfix by: Benjamin Lupton
    // +     bugfix by: Allan Jensen (http://www.winternet.no)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // *     example 1: number_format(1234.5678, 2, '.', '');
    // *     returns 1: 1234.57

    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
    var d = dec_point == undefined ? "," : dec_point;
    var t = thousands_sep == undefined ? "." : thousands_sep, s = n < 0 ? "-" : "";
    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;

    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}