/hacks/XSpinnerCGA/XSpinnerCGA.js
/* Copyright 2011 The Dominion of Awesome
 * See COPYING for licensing information */
enyo.kind({
	name: "XSpinnerCGA",
	kind: "Hack",
	style: "background-color: black",
	pack: "center",
	align: "center",
	components: [
		{style: "border: 2px solid green; padding: 4px", components: [
			{name: "terminal", nodeTag: "pre", style: "margin: 0; font-family: PCSenior; font-size: 16px"}
		]}
	],
	create: function() {
		this.inherited(arguments);
		this.w = 40;
		this.h = 24;
		this.delay = 20;
		this.rate = 360.0 / 1000.0;
		this.cw = this.w / 2;
		this.ch = this.h / 2;
	},
	rendered: function() {
		this.terminal = this.$.terminal.hasNode();
		this.blinker = document.getElementById('spinner_blinker');
		this.blink_active = true;
		this.spinner();
	},
	start: function() {
		if (!this.timer) {
			this.timer = setInterval(this.spinner.bind(this), this.delay);
			this.blinkerTimer = setInterval(this.blink.bind(this), 500);
		}
	},
	stop: function() {
		clearInterval(this.timer);
		clearInterval(this.blinkerTimer);
		this.timer = null;
		this.blinkerTimer = null;
	},
	spinner: function() {
		// Get date
		var ms = (new Date()).getMilliseconds();
		var r = ms * this.rate;
		var a = Math.tan((r / 180.0) * Math.PI);
		var b = Math.tan(((r+90) / 180.0) * Math.PI);

		var t = '';
		for (var j=0; j < this.h; j++) {
			for (var i=0; i < this.w; i++) {
				if ((r > 90 && r <= 180) || (r > 270 && r < 360) || r == 0) {
					if ((j-this.ch) * 2 > a * (i-this.cw) && (j-this.ch) * 2 < b * (i-this.cw) ||
					    (j-this.ch) * 2 < a * (i-this.cw) && (j-this.ch) * 2 > b * (i-this.cw) ) {
						t += '*';
					} else {
						t += ' ';
					}
				} else {
					if ((j-this.ch) * 2 > a * (i-this.cw) && (j-this.ch) * 2 > b * (i-this.cw) ||
					    (j-this.ch) * 2 < a * (i-this.cw) && (j-this.ch) * 2 < b * (i-this.cw) ) {
						t += '*';
					} else {
						t += ' ';
					}
				}
			}
			t += "\n";
		}
		this.terminal.innerHTML = t;
	},
	blink: function() {
		if (this.blink_active)
			this.blinker.style.color = '#00FF00';
		else
			this.blinker.style.color = 'black';
		this.blink_active = !this.blink_active;
	}
});