var isWin = navigator.platform.indexOf('Win') === 0,
	crlf = /(\r?\n|\r)/g,
	whitespace = /(\r?\n|\r|\s+)/g,
	total = 0;

function $(id) {
	return document.getElementById(id);
}

function getSize(count) {
	var level = 0;

	while(count > 1024) {
		count = count/1024;
		level++;
	}

	// Round to 2 decimals
	count = Math.round(count*100)/100;

	level = (['', 'K', 'M', 'G', 'T'])[level];

	return '<strong>' + count + '</strong> ' + level + 'B';
}

function adjustTextareaHeight() {
	var height = 16 - Math.min(11, total) + 'em';

	var cssRules = document.styleSheets[0].cssRules || document.styleSheets[0].rules;
	if(cssRules) {
		for(var i=0; i<cssRules.length; i++) {
			if(/textarea/i.test(cssRules[i].selectorText)) {
				cssRules[i].style.height = height;
				break;
			}
		}
	}
}

var addButton = document.createElement('button');
	addButton.innerHTML = '&#x2714; Add new';
	addButton.onclick = function() {
		new byteTextarea();
		total++;

		adjustTextareaHeight();
		return false;
	};
document.body.insertBefore(addButton, $('copyright'));

var noWhitespaceCheckbox = $('nowhitespace');

function byteTextarea() {
	// Constructor
	var me = this;

	this.textarea = document.createElement('textarea');
	this.textarea.setAttribute('wrap', 'off');

	this.textarea.onkeyup = this.textarea.onclick = function(evt){
		me.compute();

		return false;
	};

	this.results = document.createElement('div');
	this.results.className = 'results';

	this.container = document.createElement('div');
	this.container.className = 'container';

	var label = document.createElement('label');
	label.appendChild(document.createTextNode('Paste your text here:'));
	label.appendChild(this.textarea);

	if(total > 0) {
		this.close = document.createElement('a');
		this.close.className = 'close';
		this.close.href = '#';
		this.close.innerHTML = '&times;';
		this.close.onclick = function() {
			if(!me.textarea.value || confirm('There is text here. Are you sure?')) {
				me.destroy();
			}
			return false;
		}
		this.container.appendChild(this.close);
	}
	this.container.appendChild(this.results);
	this.container.appendChild(label);
	document.body.insertBefore(this.container, addButton);

	this['compute' + (noWhitespaceCheckbox.checked? 'No' : '') + 'Whitespace']();
	this.textarea.focus();
}

byteTextarea.prototype = {
	computeWhitespace: function() {
		var countUnix = this.textarea.value.replace(crlf, '1').length,
			countWin = this.textarea.value.replace(crlf, '11').length,

			current = isWin? getSize(countWin) : getSize(countUnix),
			other = isWin? getSize(countUnix) + ' in Unix/MacOS' : getSize(countWin) + ' in Windows';

		this.results.innerHTML = current + ' <span class="shade">(' + other + ')</span>';
	},

	computeNoWhitespace: function() {
		this.results.innerHTML = getSize(this.textarea.value.replace(whitespace, '').length);
	},

	destroy: function() {
		this.textarea.onkeyup = this.textarea.onclick = this.close.onclick = null;
		document.body.removeChild(this.container);
		total--;
		adjustTextareaHeight();
	}
}

new byteTextarea();
total++;

(noWhitespaceCheckbox.onclick = function() {
	document.body.className = noWhitespaceCheckbox.checked? 'nowhitespace' : '';

	byteTextarea.prototype.compute = byteTextarea.prototype['compute' + (noWhitespaceCheckbox.checked? 'No' : '') + 'Whitespace'];

	var textareas = document.getElementsByTagName('textarea'), i = textareas.length;
	while(i--) {
		textareas[i].onclick();
	}
})();
