
var AEEditor = function(menuClass, menuMaximizedClass, editableClass, editableMaximizedClass,
	lang, icons, iconWidth, iconHeight, dlgWnd, browseonclick)
{
	this.menuClass = menuClass;
	this.menuMaximizedClass = menuMaximizedClass;
	this.editableClass = editableClass;
	this.editableMaximizedClass = editableMaximizedClass;
	this.lang = lang;
	this.icons = icons;
	this.iconWidth = iconWidth;
	this.iconHeight = iconHeight;
	this.dlgWnd = dlgWnd;
	this.browseonclick = browseonclick;

	this.createLinkAElement = null;
	this.insertImageElement = null;
	this.insertTableElement = null;
	this.blockFormatElement = null;
	this.helpElement = null;
}



AEEditor.prototype.getCreateLinkAElement = function()
{
	if (!this.createLinkAElement)
	{
		this.createLinkAElement = document.createElement('form');
		this.createLinkAElement.AEEditor = this;
		this.createLinkAElement.onsubmit = function()
		{
			this.AEEditor.createLinkA(this.elements['href'].value, this.elements['icon'].value);
			this.AEEditor.dlgWnd.close();
			this.reset();
			return false;
		}
		this.createLinkAElement.onreset = function()
		{
			this.AEEditor.dlgWnd.close();
			return true;
		}
		this.createLinkAElement.innerHTML = '<table style="width: 100%"><tr><td>'
			+this.lang['TextHref']+'</td><td><input name="href" type="text" /><input type="button" onclick="'
			+this.browseonclick+'" value="'
			+this.lang['TextBrowse']+'..." /></td></tr><tr><td>'
			+this.lang['TextIcon']
			+'</td><td><select name="icon" onchange="this.form.getElementsByTagName(\'img\')[0].src = this.value">\
<option value="auto">'
			+this.lang['TextIconAuto']+'</option><option value="none">'
			+this.lang['TextIconNone']+'</option><option value="'
			+this.icons['FileArchive']+'">'
			+this.lang['TextIconArchive']+'</option><option value="'
			+this.icons['FileText']+'">'
			+this.lang['TextIconText']+'</option><option value="'
			+this.icons['FileImage']+'">'
			+this.lang['TextIconImage']+'</option><option value="'
			+this.icons['FileVideo']+'">'
			+this.lang['TextIconVideo']+'</option><option value="'
			+this.icons['FileMusic']+'">'
			+this.lang['TextIconMusic']+'</option><option value="'
			+this.icons['FileUnknown']+'">'
			+this.lang['TextIconUnknown']+'</option><option value="'
			+this.icons['FileAcrobat']+'">'
			+this.lang['TextIconAcrobat']+'</option><option value="'
			+this.icons['FileFlash']+'">'
			+this.lang['TextIconFlash']+'</option><option value="'
			+this.icons['FileCode']+'">'
			+this.lang['TextIconCode']+'</option></select> <img src="" alt="" style="vertical-align: middle" /></td></tr>\
<tr><td colspan="2" align="right"><input type="submit" value="'
			+this.lang['TextOK']+'" /><input type="reset" value="'
			+this.lang['TextCancel']+'" /></td></tr></table>';
	}
	return this.createLinkAElement;
}



AEEditor.prototype.getInsertImageElement = function()
{
	if (!this.insertImageElement)
	{
		this.insertImageElement = document.createElement('form');
		this.insertImageElement.AEEditor = this;
		this.insertImageElement.onsubmit = function()
		{
			this.AEEditor.insertImage(this.elements['src'].value, this.elements['alt'].value,
				this.elements['width'].value, this.elements['height'].value);
			this.AEEditor.dlgWnd.close();
			this.reset();
			return false;
		}
		this.insertImageElement.onreset = function()
		{
			this.AEEditor.dlgWnd.close();
			return true;
		}
		this.insertImageElement.innerHTML = '<table style="width: 100%"><tr><td>'
			+this.lang['TextHref']+'</td><td><input name="src" type="text" /><input type="button" onclick="'
			+this.browseonclick+'" value="'
			+this.lang['TextBrowse']+'..." /></td></tr><tr><td>'
			+this.lang['TextAlt']+'</td><td><input name="alt" type="text" /></td></tr><tr><td>'
			+this.lang['TextWidth']+'</td><td><input name="width" type="text" /></td></tr><tr><td>'
			+this.lang['TextHeight']+'</td><td><input name="height" type="text" /></td></tr>\
<tr><td colspan="2" align="right"><input type="submit" value="'
			+this.lang['TextOK']+'" /><input type="reset" value="'
			+this.lang['TextCancel']+'" /></td></tr></table>';
	}
	return this.insertImageElement;
}



AEEditor.prototype.getInsertTableElement = function()
{
	if (!this.insertTableElement)
	{
		this.insertTableElement = document.createElement('form');
		this.insertTableElement.AEEditor = this;
		this.insertTableElement.onsubmit = function()
		{
			this.AEEditor.insertTable(this.elements['cellspacing'].value, this.elements['cellpadding'].value,
				this.elements['width'].value, this.elements['border'].value,
				this.elements['cols'].value, this.elements['rows'].value);
			this.AEEditor.dlgWnd.close();
			this.reset();
			return false;
		}
		this.insertTableElement.onreset = function()
		{
			this.AEEditor.dlgWnd.close();
			return true;
		}
		this.insertTableElement.innerHTML = '<table style="width: 100%"><tr><td>'
			+this.lang['TextCellspacing']+'</td><td><input name="cellspacing" type="text" size="4" /></td></tr><tr><td>'
			+this.lang['TextCellpadding']+'</td><td><input name="cellpadding" type="text" size="4" /></td></tr><tr><td>'
			+this.lang['TextWidth']+'</td><td><input name="width" type="text" size="4" value="100%" /></td></tr><tr><td>'
			+this.lang['TextBorder']+'</td><td><input name="border" type="text" size="4" /></td></tr><tr><td>'
			+this.lang['TextCols']+'</td><td><input name="cols" type="text" size="4" value="2" /></td></tr><tr><td>'
			+this.lang['TextRows']+'</td><td><input name="rows" type="text" size="4" value="2" /></td></tr>\
<tr><td colspan="2" align="right"><input type="submit" value="'
			+this.lang['TextOK']+'" /><input type="reset" value="'
			+this.lang['TextCancel']+'" /></td></tr></table>';
	}
	return this.insertTableElement;
}



AEEditor.prototype.getBlockFormatElement = function()
{
	if (!this.blockFormatElement)
	{
		this.blockFormatElement = document.createElement('form');
		this.blockFormatElement.AEEditor = this;
		this.blockFormatElement.onsubmit = function()
		{
			this.AEEditor.blockFormat(this.elements['tag'].value);
			this.AEEditor.dlgWnd.close();
			this.reset();
			return false;
		}
		this.blockFormatElement.onreset = function()
		{
			this.AEEditor.dlgWnd.close();
			return true;
		}
		this.blockFormatElement.innerHTML = '<table style="width: 100%"><tr><td valign="top">'
			+this.lang['TextFormat']+'</td><td><select name="tag" size="10"><option value="h1" title="h1">'
			+this.lang['TextFormatHeader']+' 1</option><option value="h2" title="h2">'
			+this.lang['TextFormatHeader']+' 2</option><option value="h3" title="h3">'
			+this.lang['TextFormatHeader']+' 3</option><option value="h4" title="h4">'
			+this.lang['TextFormatHeader']+' 4</option><option value="h5" title="h5">'
			+this.lang['TextFormatHeader']+' 5</option><option value="h6" title="h6">'
			+this.lang['TextFormatHeader']+' 6</option><option value="small" title="small">'
			+this.lang['TextFormatSmall']+'</option><option value="big" title="big">'
			+this.lang['TextFormatBig']+'</option><option value="pre" title="pre">'
			+this.lang['TextFormatPre']+'</option><option value="tt" title="tt">'
			+this.lang['TextFormatTt']+'</option></select></td></tr>\
<tr><td colspan="2" align="right"><input type="submit" value="'
			+this.lang['TextOK']+'" /><input type="reset" value="'
			+this.lang['TextCancel']+'" /></td></tr></table>';
	}
	return this.blockFormatElement;
}



AEEditor.prototype.getHelpElement = function()
{
	if (!this.helpElement)
	{
		this.helpElement = document.createElement('form');
		this.helpElement.AEEditor = this;
		this.helpElement.onsubmit = function()
		{
			this.AEEditor.dlgWnd.close();
			return false;
		}
		this.helpElement.innerHTML = '<table style="width: 100%"><tr><td>'
			+this.lang['TextHelp']+'</td></tr><tr><td colspan="2" align="right"><input type="submit" value="'
			+this.lang['TextOK']+'" /></td></tr></table>';
	}
	return this.helpElement;
}



AEEditor.prototype.add = function(id, btns)
{
	var target = document.getElementById(id);
	target.style.display = 'none';
	var editable = document.createElement('div');
	target.parentNode.insertBefore(editable, target);
	editable.className = this.editableClass;
	editable.style.width = target.style.width;
	editable.style.maxHeight = target.style.height;
	editable.innerHTML = target.value;
	editable.contentEditable = true;
	editable.AEEditor_target = target;
	editable.onblur = function()
	{
		this.AEEditor_target.value = this.innerHTML;
	}

	btns = btns.split("|");
	var editor = document.createElement('div');
	editable.parentNode.insertBefore(editor, editable);
	editor.className = this.menuClass;
	for (var i=0; i<btns.length; i++)
	{
		var b;
		if (i == 'br')
		{
			b = document.createElement('br');
		}
		else
		{
			b = document.createElement('img');
			b.title = this.lang[btns[i]];
			b.src = this.icons[btns[i]];
			b.width = this.iconWidth;
			b.height = this.iconHeight;
			b.AEEditor = this;
			b.AEEditor_format = btns[i];
			b.AEEditor_editable = editable;
			b.onclick = function()
			{
				this.AEEditor.format(this);
			}
		}
		editor.appendChild(b);
	}
	
	if (document.createRange) // Gecko
		document.execCommand('useCSS', false, true);
}



AEEditor.prototype.getInnerText = function(o)
{
	if (document.createRange) // Gecko
	{
		var r = document.createRange();
		r.selectNodeContents(o)
		return r;
	}
	else // IE
	{
		return o.innerText;
	}
}



AEEditor.prototype.getSelectedText = function()
{
	if (window.getSelection) // Gecko
	{
		return window.getSelection();
	}
	else // IE
	{
		if (document.selection.type=='Control')
			return false;
		var r = document.selection.createRange();
		return r.text;
	}
}



AEEditor.prototype.pasteHTML = function(html)
{
	try // Gecko
	{
		document.execCommand('InsertHTML', false, html);
	}
	catch (e) // IE
	{
		if (document.selection.type=='Control')
			return false;
		var r = document.selection.createRange();
		r.pasteHTML(html);
	}
}



AEEditor.prototype.storeSelection = function()
{
	if (window.getSelection) // Gecko
	{
		var s = window.getSelection();
		this.storedSelection = s.getRangeAt(0);
	}
	else // IE
	{
		this.storedSelection = document.selection.createRange();
	}
}



AEEditor.prototype.restoreSelection = function()
{
	if (window.getSelection) // Gecko
	{
		var s = window.getSelection();
		s.removeAllRanges();
		if (this.storedSelection)
			s.addRange(this.storedSelection);
	}
	else // IE
	{
		document.selection.empty();
		if (this.storedSelection)
			this.storedSelection.select();
	}
}



AEEditor.prototype.createLinkA = function(href, iconType)
{
	this.restoreSelection();
	var text = this.getSelectedText();
	if (text=='')
		text = href;

	var iconHtml;
	if (iconType == 'auto')
	{
		var ext = href.replace(/^.*\.(\w+)$/, '$1').toLowerCase();
		switch (ext)
		{
			case 'zip': case 'rar': case 'gz': iconType = 'FileCompress'; break;
			case 'txt': case 'rtf': case 'doc': case 'log': case 'ini': iconType = 'FileText'; break;
			case 'jpg': case 'jpeg': case 'gif': case 'png': iconType = 'FileImage'; break;
			case 'mpg': case 'mpeg': case 'wmv': case 'avi': case 'mp4': case 'mkv': iconType = 'FileVideo'; break;
			case 'mp3': case 'wma': case 'ogg': case 'wav': iconType = 'FileMusic'; break;
			case 'pdf': iconType = 'FileAcrobat'; break;
			case 'swf': case 'flv': iconType = 'FileFlash'; break;
			case 'htm': case 'html': case 'css': case 'js': case 'php': iconType = 'FileCode'; break;
			default: iconType = 'none'; break;
		}
	}

	if (this.icons[iconType])
		iconHtml = '<img src="'+this.icons[iconType]+'" style="vertical-align: middle;" />&nbsp;';
	else
		iconHtml = '';

	this.pasteHTML(iconHtml + '<a href="'+href+'">'+text+'</a>');
}



AEEditor.prototype.insertImage = function(src, alt, width, height)
{
	this.restoreSelection();

	if (src == '')
		return;

	var html = '<img src="'+src+'" alt="'+alt+'"';
	if (width != '')
		html += ' width="'+width+'"';
	if (height != '')
		html += ' height="'+height+'"';
	html += ' />';

	this.pasteHTML(html);
}



AEEditor.prototype.insertTable = function(cellspacing, cellpadding, width, border, cols, rows)
{
	this.restoreSelection();

	var html = '<table';
	if (cellspacing != '')
		html += ' cellspacing="'+cellspacing+'"';
	if (cellpadding != '')
		html += ' cellpadding="'+cellpadding+'"';
	if (width != '')
		html += ' width="'+width+'"';
	if (border != '')
		html += ' border="'+border+'"';
	html += '>';
	for (var i=0; i<rows; i++)
	{
		html += '<tr>';
		for (var j=0; j<cols; j++)
			html += '<td>&nbsp;</td>';
		html += '</tr>';
	}
	html += '</table>';

	this.pasteHTML(html);
}



AEEditor.prototype.blockFormat = function(tag)
{
	this.restoreSelection();
	var text = this.getSelectedText();
	if (text=='')
		return;

	this.pasteHTML('<'+tag+'>' + text + '</'+tag+'>');
}



AEEditor.prototype.format = function(btn)
{
	this.storeSelection();
	var nf;
	var m = btn.parentNode;
	var o = btn.AEEditor_editable;
	switch (btn.AEEditor_format)
	{
		case 'Html':
			nf = 'Text';
			btn.AEEditor_format = nf;
			btn.src = this.icons[nf];
			btn.title = this.lang[nf];
			var tn = document.createTextNode(o.innerHTML);
			o.innerHTML = '';
			o.appendChild(tn);
			break;
		case 'Text':
			nf = 'Html';
			btn.AEEditor_format = nf;
			btn.src = this.icons[nf];
			btn.title = this.lang[nf];
			o.innerHTML = this.getInnerText(o);
			break;
		case 'Maximize':
			nf = 'Minimize';
			btn.AEEditor_format = nf;
			btn.src = this.icons[nf];
			btn.title = this.lang[nf];
			btn.AEEditor_oldWidth = o.style.width;
			btn.AEEditor_oldHeight = o.style.maxHeight;

            m.className = this.menuMaximizedClass;
			m.style.position = 'fixed';
			m.style.top = '0px';
			m.style.left = '0px';

			o.className = this.editableMaximizedClass;
			o.style.position = 'fixed';
			o.style.top = m.offsetHeight + 'px';
			o.style.left = '0px';
			o.style.width = (document.documentElement.clientWidth - 20) + 'px';
			o.style.height = (document.documentElement.clientHeight - m.offsetHeight - 20) + 'px';
			o.style.maxHeight = '';
			break;
		case 'Minimize':
			nf = 'Maximize';
			btn.AEEditor_format = nf;
			btn.src = this.icons[nf];
			btn.title = this.lang[nf];

            m.className = this.menuClass;
			m.style.position = 'static';

			o.className = this.editableClass;
			o.style.position = 'static';
			o.style.width = btn.AEEditor_oldWidth;
			o.style.maxHeight = btn.AEEditor_oldHeight;
            o.style.height = '';
			break;
		case 'CreateLink':
            try
			{
				document.execCommand(btn.AEEditor_format, false, window.prompt(this.lang['TextCreateLink'], 'http://'));
			}
			catch (e) {}
			break;
		case 'CreateLinkA':
			this.dlgWnd.showOverlay();
			this.dlgWnd.showModalbox();
			this.dlgWnd.modalbox.appendChild(this.getCreateLinkAElement());
			break;
		case 'InsertImage':
			this.dlgWnd.showOverlay();
			this.dlgWnd.showModalbox();
			this.dlgWnd.modalbox.appendChild(this.getInsertImageElement());
			break;
		case 'InsertTable':
			this.dlgWnd.showOverlay();
			this.dlgWnd.showModalbox();
			this.dlgWnd.modalbox.appendChild(this.getInsertTableElement());
			break;
		case 'BlockFormat':
			this.dlgWnd.showOverlay();
			this.dlgWnd.showModalbox();
			this.dlgWnd.modalbox.appendChild(this.getBlockFormatElement());
			break;
		case 'Help':
			this.dlgWnd.showOverlay();
			this.dlgWnd.showModalbox();
			this.dlgWnd.modalbox.appendChild(this.getHelpElement());
			break;
		default:
			try
			{
				document.execCommand(btn.AEEditor_format, false, null);
			}
			catch (e) {}
			break;
	}
	o.onblur();
}
