window.addEvent('domready', function() {
	var overlay = new Element('div').setStyles({
		position: 'absolute',
		zIndex: 9998,
		left: 0,
		top: 0,
		width: '100%',
		height: '100%',
		background: '#000'
	}).fade('hide').inject($$('body')[0]);
	
	overlay.get('tween').setOptions({link: 'ignore', duration: 150});
	
	var bigHolder = new Element('div').setStyles({
		position: 'fixed',
		zIndex: 9999,
		width: '600px',
		height: '400px',
		left: '50%',
		top: '50%',
		marginLeft: '-300px',
		marginTop: '-200px',
		border: '15px solid #FFF'
	}).fade('hide').inject(overlay, 'after');
	
	var bigLabel = new Element('div', {
		text: 'Type your text here. Press <tab> or click outside the box to close'
	}).setStyles({
		background: '#FFF',
		position: 'absolute',
		left: '0px',
		width: '100%',
		top: '0px',
		height: '40px'
	}).inject(bigHolder, 'inside');
	
	var bigTextBox = new Element('textarea').setStyles({
		position: 'absolute',
		left: '0px',
		width: '100%',
		top: '40px',
		height: '360px'
	}).inject(bigHolder, 'inside');
	
	bigHolder.get('tween').setOptions({link: 'ignore', duration: 150});
	
	var currentTextArea = null;
	
	var showBigTextBox = function(textarea) {
		if (textarea === currentTextArea) {
			return;
		}
		currentTextArea = textarea;
		bigTextBox.set('value', textarea.get('value'));
		overlay.fade(0.75);
		bigHolder.fade('in');
		(function(){bigTextBox.focus();}).delay(50);
	};
	
	var hideBigTextBox = function() {
		if (currentTextArea) {
			currentTextArea.set('value', bigTextBox.get('value'));
		}
		overlay.fade('out');
		bigHolder.fade('out');
		if (currentTextArea) {
			bigTextBox.blur();
			currentTextArea.focus();
			currentTextArea = null;
		}
	}
	
	overlay.addEvent('click', hideBigTextBox);
	bigTextBox.addEvent('blur', hideBigTextBox);
	
	$$('.bigedit textarea').each(function(textarea) {
		textarea.addEvent('focus', function() {
			showBigTextBox(textarea);
		});
		textarea.addEvent('click', function() {
			showBigTextBox(textarea);
		});
	});
});
