(function (root) {

	var Module = function () {

		this.canvas;
		this.context;

		this.radius = 0;
		this.segmentSize = 1;
		this.segmentColor = '#6a6a6a';
	}

	function init (canvasID, borderWidth, borderColor) {

		// canvas prep
		this.canvas = document.getElementById(canvasID);
		this.context = this.canvas.getContext('2d');

		// canvas styling
		this.segmentSize = borderWidth || this.segmentSize;
		this.context.lineWidth = this.segmentSize;
		this.context.strokeStyle = borderColor || this.segmentColor;

		// geometry prep
		this.radius = (parseInt(this.canvas.width) / 2) - this.segmentSize;
		this.PI2 = Math.PI * 2;
		this.quadrant = Math.PI / 2;
		this.currentPercent = 0;
		this.endPercent = 110; // make sure the gap is filled
		this.centerX = this.canvas.width / 2;
 		this.centerY = this.canvas.height / 2;
	}

	function draw (_perc) {
		this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
		this.context.beginPath();
		this.context.arc(this.centerX, this.centerY, this.radius, -(this.quadrant), ((this.PI2) * _perc) - this.quadrant, false);
		this.context.stroke();
	}

	function animateIn (callback) {

		this.currentPercent++;

		if (this.currentPercent <= this.endPercent) {
			requestAnimationFrame(function () {
				draw.apply(this, [this.currentPercent / this.endPercent]);
				animateIn.apply(this, [callback]);
			}.bind(this));
		} else {
			if (callback) callback();
		}
	}

	function animateOut (callback) {

		this.currentPercent--;

		if (this.currentPercent >= 0) {
			requestAnimationFrame(function () {
				draw.apply(this, [this.currentPercent / this.endPercent]);
				animateOut.apply(this, [callback]);
			}.bind(this));
		} else {
			if (callback) callback();
		}
	}

	function show () {
		// nothing now
	}

	function hide () {
		// nothing now
	}

	Module.prototype.init = init;
	Module.prototype.show = show;
	Module.prototype.hide = hide;
	Module.prototype.animateIn = animateIn;
	Module.prototype.animateOut = animateOut;
	Module.prototype.draw = draw;

	root.LoadingCircle = Module;

})(UI);