if( typeof D == 'undefined' ){
	var D = {};
}
if( typeof xGlobal == 'undefined' ){
	xGlobal = {};
}
/*
params
	par ={
		cssPath(option):[string]
		onChange(option):[function],
		maxTextLength(option):[int],
		value(option)[string],
		headings:[false|true],
		gallery::[false|{
			size(option):[icon|small|middle|large]
		}]
	}
method,	properties
	value() iegūst vai uzseto vērtību
	textLength() 
	insertHTML( html )
*/
var TEXTAREA = D.Textarea = function( par ){
	var this_ = this;
	if( typeof par == 'undefined' ){
		par = {};
	}
	this.onChange = par.onChange || function(){};	
	this.onChange2 = par.onChange2 || function(){};	
	if( par.maxTextLength ){
		this.maxTextLength = par.maxTextLength;
	}
	this._ = {
		value:par.value || ''
	};
	this.node = mkE( {
		tag:'div',
		className:'textarea' + ( par.className ? ' ' + par.className : '' ),
		prop:{
			drTextarea:this
		}
	} );	
	this.iconRoot = pimg + '/img/mail/textarea/';
	this.cssPath = par.cssPath || false;
	var menu = [];
	if( par.headings ){
		var m = [
			{
				t:'MENU_SELECT',
				k:'format',
				values:[
					{					
						caption:xGlobal[ 'Heading 1' ] || 'Heading 1',
						listCaption:'<h1>' + ( xGlobal[ 'Heading 1' ] || 'Heading 1' ) + '</h1>',
						value:'h1'
					},
					{					
						caption:xGlobal[ 'Heading 2' ] || 'Heading 2',
						listCaption:'<h2>' + ( xGlobal[ 'Heading 2' ] || 'Heading 2' ) + '</h2>',
						value:'h2'
					},
					{					
						caption:xGlobal[ 'Heading 3' ] || 'Heading 3',
						listCaption:'<h3>' + ( xGlobal[ 'Heading 3' ] || 'Heading 3' ) + '</h3>',
						value:'h3'
					},
					{					
						caption:xGlobal[ 'Paragraph' ] || 'Paragraph',
						listCaption:'<p>' + ( xGlobal[ 'Paragraph' ] || 'Paragraph' ) + '</p>',
						value:'p'
					}
				],
				onChange:function( v ){
					this_.focus();
					if( this_._.sRange ){
						this_._.sRange.select();
					}
					this_._.iframe.contentWindow.document.execCommand( 'formatblock', false, '<' + v + '>');
					this_.checkSelection();
				}
			},
			'seperator'
		];
		menu = menu.concat( m );
	}	
	var m = [
		{
			t:'MENU_ICON',
			k:'bold',
			caption:xGlobal[ 'Bold' ] || 'Bold',
			icon:'bold.png',			
			onclick:function( e ){
				this_.focus();
				if( this_._.sRange ){
					this_._.sRange.select();
				}
				this_._.iframe.contentWindow.document.execCommand( 'bold', false, null );
				this_.checkSelection();
				return false;
			}
		},
		{
			t:'MENU_ICON',
			k:'italic',
			caption:xGlobal[ 'Italic' ] || 'Italic',
			icon:'italic.png',
			onclick:function(){
				this_.focus();
				if( this_._.sRange ){
					this_._.sRange.select();
				}
				this_._.iframe.contentWindow.document.execCommand( 'italic', false, null );
				this_.checkSelection();
			}
		},
		{
			t:'MENU_ICON',
			k:'underline',
			caption:xGlobal[ 'Underline' ] || 'Underline',
			icon:'underline.png',
			onclick:function(){	
				this_.focus();
				if( this_._.sRange ){
					this_._.sRange.select();
				}
				this_._.iframe.contentWindow.document.execCommand( 'underline', false, null );
				this_.checkSelection();
			}
		}		
	];
	if( par.strikeThrough !== false ){
		m.push( {
			t:'MENU_ICON',
			k:'strikethrough',
			caption:xGlobal[ 'Strikethrough' ] || 'Strikethrough',
			icon:'strikethrough.gif',
			onclick:function(){	
				this_.focus();
				if( this_._.sRange ){
					this_._.sRange.select();
				}
				this_._.iframe.contentWindow.document.execCommand( 'strikethrough', false, null );
				this_.checkSelection();
			}
		} );
	}
	menu = menu.concat( m );
	if( ! $.browser.msie ){
		menu.push( {
			t:'MENU_ICON',
			k:'blockquote',
			caption:xGlobal[ 'blockquote' ] || 'blockquote',
			icon:'quote.gif',
			onclick:function(){
				this_.focus();
				if( this_._.sRange ){
					this_._.sRange.select();
				}
				if( this.checked() ){
					var selNode = this_.selectedNode();
					var node = selNode;
					if( node && node.tagName.toLowerCase() != 'blockquote' ){
						while( node = node.parentNode ){
							if( node.tagName.toLowerCase() == 'blockquote' ){
								break;
							}
						}						
					}
					if( node ){
						var parent = node.parentNode;
						var next = node.nextSibling;
						var childs = [];
						for( var i = 0; i < node.childNodes.length; i ++ ){
							childs.push( node.childNodes[ i ] );
						}
						removeNode( node );
						if( next ){
							for( var i = 0; i < childs.length; i ++ ){
								D.insertBefore( childs[ i ], next );
							}
						} else {
							for( var i = 0; i < childs.length; i ++ ){
								parent.appendChild( childs[ i ] );
							}
						}
					}
				} else {
					this_._.iframe.contentWindow.document.execCommand( 'formatblock', false, '<blockquote>' );
				}
				this_.checkSelection();
			}			
		} );
	}
	menu.push( 'seperator' );
	m = [
		{
			t:'MENU_ICON',
			k:'justifyleft',
			caption:xGlobal[ 'Leftalign' ] || 'Leftalign',
			icon:'justifyleft.png',
			onclick:function(){
				this_.focus();
				if( this_._.sRange ){
					this_._.sRange.select();
				}
				this_._.iframe.contentWindow.document.execCommand( 'justifyleft', false, null );
				this_.checkSelection();
			}			
		},
		{
			t:'MENU_ICON',
			k:'justifycenter',
			caption:xGlobal[ 'Justifycenter' ] || 'Justifycenter',
			icon:'justifycenter.png',
			onclick:function(){
				this_.focus();
				if( this_._.sRange ){
					this_._.sRange.select();
				}
				this_._.iframe.contentWindow.document.execCommand( 'justifycenter', false, null );
				this_.checkSelection();
			}			
		},
		{
			t:'MENU_ICON',
			k:'justifyright',
			caption:xGlobal[ 'Justifyright' ] || 'Justifyright',
			icon:'justifyright.png',
			onclick:function(){
				this_.focus();
				if( this_._.sRange ){
					this_._.sRange.select();
				}
				this_._.iframe.contentWindow.document.execCommand( 'justifyright', false, null );
				this_.checkSelection();
			}			
		},
		{
			t:'MENU_ICON',
			k:'justifyfull',
			caption:xGlobal[ 'Justifyfull' ] || 'Justifyfull',
			icon:'justifyfull.png',
			onclick:function(){
				this_.focus();
				if( this_._.sRange ){
					this_._.sRange.select();
				}
				this_._.iframe.contentWindow.document.execCommand( 'justifyfull', false, null );
				this_.checkSelection();
			}		
		},
		'seperator'
	];
	menu = menu.concat( m );
	menu.push( {
		t:'MENU_ICON',
		k:'removeformat',
		caption:xGlobal[ 'Remove format' ] || 'Remove format',
		icon:'rm_format.gif',
		onclick:function(){
			this_.focus();
			if( this_._.sRange ){
				this_._.sRange.select();
			}
			this_._.iframe.contentWindow.document.execCommand( 'removeformat', false, null );
			this_.checkSelection();
		}			
	} );
	if( par.tables ){
		menu.push( 'seperator' );
		menu.push( {
			t:'MENU_ICON',
			k:'insertTable',
			caption:xGlobal[ 'Textarea Insert table' ] || 'Insert table',
			icon:'table.png',
			onclick:function( e ){
				if( this_._.insertTableDiv ){
					if( removeNode( this_._.insertTableDiv ) ){
						return;
					}
				}
				this_.insertTable( e );
				this_._.insertTableDiv.style.left = TEXTAREA.findPosX( this.node, this_.node ) + 15 + 'px';
			}			
		} );
	}	
	if( par.smiley ){
		menu.push( {
			t:'MENU_ICON',
			k:'smiley',
			caption:xGlobal[ 'Insert smiley' ] || 'Insert smiley',
			className:'smileyIcon',
			onclick:function( e, p ){				
				if( this_._.insertSmileyDiv ){
					if( removeNode( this_._.insertSmileyDiv ) ){
						return;
					}
				}
				this_.insertSmiley( e );
				this_._.insertSmileyDiv.style.left = TEXTAREA.findPosX( this.node, this_.node ) + 15 + 'px';
			}
		} );
	}
	if( par.split ){
		menu.push( {
			t:'seperator',
			k:'split',
			caption:xGlobal[ 'textarea split' ] || 'Split',
			className:'cutIcon',
			onclick:function( e, p ){
				this_.insertHTML( '<div class="TextareaSplit">&nbsp;</div><br />' );
			}
		} );
	}		
	if( par.gallery ){
		menu.push( 'seperator' );
		menu.push( {
			t:'MENU_ICON',
			k:'gallery',
			caption:xGlobal[ 'Insert images from gallery' ] || 'Insert images',
			className:'galleryIcon',
			onclick:function( e, p ){
				D.Textarea.tmp = p.textarea;
				InfoBox.iframe( '/gallery/textarea.php?' + ( par.gallery.size ? 'size=' + par.gallery.size : '' ) + ( par.gallery.uid ? '&uid=' + par.gallery.uid : '' ), { width:600, height:460 } );
			}
		} );
		// +++ bilžu opcijas
		menu.push( {
			t:'MENU_ICON',
			k:'imgNormal',
			caption:xGlobal[ 'TextareaImgNormal' ] || 'Textarea imgNormal',
			className:'imgFloatNoneIcon',
			onclick:function( e, p ){
				if( this_.activeImg ){
					this_.activeImg.className = '';
					this_.imgOptions();
				}
			}
		} );
		menu.push( {
			t:'MENU_ICON',
			k:'imgFloatLeft',
			caption:xGlobal[ 'TextareaImgFloatLeft' ] || 'Textarea imgFloatLeft',
			className:'imgFloatLeftIcon',
			onclick:function( e, p ){
				if( this_.activeImg ){
					this_.activeImg.className = 'TextareaImgFloatLeft';
					this_.imgOptions();
				}
			}
		} );
		menu.push( {
			t:'MENU_ICON',
			k:'imgFloatRight',
			caption:xGlobal[ 'TextareaImgFloatRight' ] || 'Textarea imgFloatRight',
			className:'imgFloatRightIcon',
			onclick:function( e, p ){
				if( this_.activeImg ){
					this_.activeImg.className = 'TextareaImgFloatRight';
					this_.imgOptions();
				}
			}
		} );		
		// --- bilžu opcijas
	}
	this._.menu = document.createElement( 'div' );
	this._.menu.className = 'menu';
	this._.menu.onselectstart = function(){ return false; };
	this.node.appendChild( this._.menu );
	this._.textLengthDiv = document.createElement( 'div' );
	this._.textLengthDiv.className = 'text_length';	
	this._.menu.appendChild( this._.textLengthDiv );
	this._.iframe = document.createElement( 'iframe' );
	this._.iframe.frameBorder = '0';	
	this.node.appendChild( this._.iframe );
	this.menu_icons = {};
	for( var k in menu ){
		if( menu[ k ] === 'seperator' ){
			new TEXTAREA.MENU_SEP( {
				textarea:this
			} );
		} else {			
			if( menu[ k ].t == 'MENU_ICON' ){
				this.menu_icons[ menu[ k ].k ] = new TEXTAREA.MENU_ICON( {
					textarea:this,
					caption:menu[ k ].caption,
					icon:( menu[ k ].icon ? ( menu[ k ].icon.indexOf( '/' ) == -1 ? this.iconRoot + menu[ k ].icon : menu[ k ].icon ) : false ),
					className:menu[ k ].className || false,
					onclick:menu[ k ].onclick || false
				} );			
			} else if( menu[ k ].t == 'MENU_SELECT' ){
				this.menu_icons[ menu[ k ].k ] = new TEXTAREA.MENU_SELECT( {
					textarea:this,
					onChange:menu[ k ].onChange
				} );
				this.menu_icons[ menu[ k ].k ].setValues( menu[ k ].values );
			}
		}
	}
	if( this.menu_icons.imgNormal ){
		this.menu_icons.imgNormal.hide();
	}
	if( this.menu_icons.imgFloatLeft ){
		this.menu_icons.imgFloatLeft.hide();
	}
	if( this.menu_icons.imgFloatLeft ){
		this.menu_icons.imgFloatRight.hide();
	}
}; // var TEXTAREA = function

TEXTAREA.prototype.replace = function( textareaId ){
	if( window.BrowserDetect ){		
		if( BrowserDetect.browser == 'Firefox' && BrowserDetect.version < 3 ){
			return false;
		}
		if( array_search( BrowserDetect.browser, [ 'Firefox', 'Chrome', 'Safari', 'Opera', 'Netscape', 'Explorer' ] ) === false ){
			return false;
		}
	}
	if( navigator.userAgent ){
		var disabled = [ 'iPhone', 'iPod', 'iPad', 'Mobile', 'Mini' ];
		for( var k in disabled ){
			if( navigator.userAgent.indexOf( disabled[ k ] ) != -1 ){
				return false;
			}
		}
	}
	var textarea = document.getElementById( textareaId );
	if( ! textarea ){
		D.console.warn( 'Nav elements #' + textareaId );
		return false;
	}
	if( textarea.form ){
		if( $.browser.msie ){
			mkE( {
				tag:'<input name="drTextarea" value="1" type="hidden" />'
			} ).append( textarea.form );
		} else {
			mkE( {
				tag:'input',
				prop:{
					name:'drTextarea',
					value:1,
					type:'hidden'
				}
			} ).append( textarea.form );
		}
	}
	textarea.style.display = 'none';
	this.value( textarea.value );
	this.append( textarea.parentNode );
	D.insertAfter( this.node, textarea );
	this.onChange2 = function( html ){
		textarea.value = html;
	};
};

TEXTAREA.sp = function( e ){
	if( ! e ){
		e = window.event;
	}
	e.cancelBubble = true;
	if( e.stopPropagation ){
		e.stopPropagation();
	}
}; // TEXTAREA.sp = function

TEXTAREA.prototype.focus = function(){
	try{
		this._.iframe.focus();	
		this._.iframe.contentWindow.focus();		
	} catch(e){}
	this.checkSelection();
};

TEXTAREA.prototype.append = function( node ){
	var this_ = this;
	var selection = function( e ){
		if( ! e ){
			e = window.event;
		}
		this_.activeImg = false;
		this_.imgOptions();
		var target = ( e.target || e.srcElement );
		if( target ){
			switch( target.tagName.toLowerCase() ){
				case 'img':
					if( empty( target.getAttribute( 'pid' ) ) ){
						break;
					}
					this_.activeImg = target;
					this_.imgOptions();
					break;
			}
		}		
		if( this_.onCtrlEnter && D.checkCtrlEnter( e ) ){
			this_.onCtrlEnter();
		}
		if( document.all && !window.opera ){
			this_._.sRange = this_._.iframe.contentWindow.document.selection.createRange();
		}	
		this_.checkSelection();
	};
	this.node.append( node );	
	var style = '';
	if( this.cssPath ){
		if( this.cssPath.push ){
			for( var k in this.cssPath ){
				style += '<style type="text/css">@import url(' + this.cssPath[ k ] + ');</style>';
			}
		} else {
			style += '<style type="text/css">@import url(' + this.cssPath + ');</style>';
		}
	}
	var addEvents = function(){
		try{			
			if( document.all && !window.opera ){
				this_._.iframe.contentWindow.document.attachEvent( 'onkeyup', selection );
				this_._.iframe.contentWindow.document.attachEvent( 'onclick', selection );
				this_._.iframe.contentWindow.document.attachEvent( 'onmouseup', selection );
			} else {
				this_._.iframe.contentWindow.document.execCommand( 'useCSS', false, null );
				this_._.iframe.contentWindow.document.addEventListener( 'keyup', selection, true );
				this_._.iframe.contentWindow.document.addEventListener( 'click', selection, true );
				this_._.iframe.contentWindow.document.addEventListener( 'mouseup', selection, true );
				//this_._.iframe.contentWindow.document.addEventListener( 'contextmenu', function(){ alert(1); }, true );
			}
			if( this_._.value ){
				this_._.iframe.contentWindow.document.body.innerHTML = this_._.value;
			}
		} catch( e ){
			return false;
		}
		setInterval( function(){
			if( ! this_._.iframe.contentWindow ){
				return false;
			}
			var html = this_._.iframe.contentWindow.document.body.innerHTML;
			if( this_._.value != html && this_.onChange ){
				this_.onChange( html );
				this_.onChange2( html );
				this_._.value = html;
			}
		}, 100 );
		if( this_.onReady ){
			this_.onReady();
		}
		return true;
	};
	var checkReady = function(){
		if( ! this_._.iframe.contentWindow || ! this_._.iframe.contentWindow.document || ! this_._.iframe.contentWindow.document.body ){
			return false;
		}
		this_._.iframe.contentWindow.document.open();
		this_._.iframe.contentWindow.document.write( '<html><head>' + style + '</head><body id="TextareaBody" style="text-align:left;">' + this_._.value + '</body></html>' );
		this_._.iframe.contentWindow.document.close();
		this_._.iframe.contentWindow.document.designMode = 'on';
		var interval = setInterval( function(){
			if( addEvents() ){
				clearInterval( interval );
			}
		}, 100 );
		return true;
	};
	var interval = setInterval( function(){
		if( checkReady() ){
			clearInterval( interval );
		}
	}, 100 );
	return this;
}; // TEXTAREA.prototype.append = function

TEXTAREA.prototype.selectedNode = function(){
	if( window.getSelection ){
		var focusNode = this._.iframe.contentWindow.window.getSelection().focusNode; //anchorNode
	} else {
		var focusNode = this._.iframe.contentWindow.document.selection.createRange().parentElement();		
	}
	if( ! focusNode ){
		return false;
	}
	var re = ( focusNode.nodeType == 3 ? focusNode.parentNode : focusNode );
	if( re === document.body	){
		re = false;
	}
	return re;
}; // TEXTAREA.prototype.selectedNode = function

TEXTAREA.prototype.checkSelection = function(){	
	var selNode = this.selectedNode();
	if( ! selNode ){
		return;
	}
	if( this.menu_icons.bold ){
		this.menu_icons.bold.checked( String( getStyle( selNode, 'fontWeight' ) ).indexOf( 'bold' ) != -1 || getStyle( selNode, 'fontWeight' ) > 400 );	
	}
	if( this.menu_icons.italic ){
		this.menu_icons.italic.checked( String( getStyle( selNode, 'fontStyle' ) ).indexOf( 'italic' ) != -1 );
	}
	if( this.menu_icons.underline ){
		this.menu_icons.underline.checked( String( getStyle( selNode, 'textDecoration' ) ).indexOf( 'underline' ) != -1 );	
	}
	if( this.menu_icons.strikethrough ){
		this.menu_icons.strikethrough.checked( String( getStyle( selNode, 'textDecoration' ) ).indexOf( 'line-through' ) != -1);	
	}
	if( this.menu_icons.justifyleft ){
		this.menu_icons.justifyleft.checked( getStyle( selNode, 'textAlign' ) == 'left' || selNode.align == 'left' );
	}
	if( this.menu_icons.justifycenter ){
		this.menu_icons.justifycenter.checked( selNode.tagName == 'center' || getStyle( selNode, 'textAlign' ).indexOf( 'center' ) != -1 || selNode.align == 'center' );
	}
	if( this.menu_icons.justifyright ){
		this.menu_icons.justifyright.checked( getStyle( selNode, 'textAlign' ).indexOf( 'right' ) != -1 || selNode.align == 'right' );
	}
	if( this.menu_icons.justifyfull ){
		this.menu_icons.justifyfull.checked( getStyle( selNode, 'textAlign' ).indexOf( 'justify' ) != -1 || selNode.align == 'justify' );
	}	
	var pn = selNode;
	if( this.menu_icons.format ){
		this.menu_icons.format.value( 'p' );	
	}
	if( this.menu_icons.blockquote ){
		this.menu_icons.blockquote.checked( pn.tagName.toLowerCase() == 'blockquote' );
	}
	while( pn.parentNode ){
		if( pn.tagName ){
			if( this.menu_icons.format && ( pn.tagName.toLowerCase() == 'h1' || pn.tagName.toLowerCase() == 'h2' || pn.tagName.toLowerCase() == 'h3'  ) ){
				this.menu_icons.format.value( pn.tagName.toLowerCase() );
			}
			if( this.menu_icons.bold && ( pn.tagName.toLowerCase() == 'b' || pn.tagName.toLowerCase() == 'strong' ) ){
				this.menu_icons.bold.checked( true );
			}
			if( this.menu_icons.italic && pn.tagName.toLowerCase() == 'i' ){
				this.menu_icons.italic.checked( true );
			}
			if( this.menu_icons.underline && pn.tagName.toLowerCase() == 'u' ){
				this.menu_icons.underline.checked( true );
			}
			if( this.menu_icons.strikethrough && ( pn.tagName.toLowerCase() == 's' || pn.tagName.toLowerCase() == 'strike' ) ){
				this.menu_icons.strikethrough.checked( true );
			}
			if( this.menu_icons.blockquote && pn.tagName.toLowerCase() == 'blockquote' ){
				this.menu_icons.blockquote.checked( true );
			}
		}
		pn = pn.parentNode;
	}		
	var html = this._.iframe.contentWindow.document.body.innerHTML;	
	if( this._.value != html ){
		this.onChange( html );
		this.onChange2( html );
	}
	if( this.maxTextLength ){
		this._.textLengthDiv.innerHTML = ( this.maxTextLength - this.textLength() < 0 ? 0 : this.maxTextLength - this.textLength() );
		if( this.maxTextLength < this.textLength() ){
			this._.iframe.contentWindow.document.body.innerHTML = this._.value || '';
		}
	}	
	this._.value = html; 	
}; // TEXTAREA.prototype.checkSelection = function

TEXTAREA.prototype.value = function( v ){
	if( typeof v != 'undefined' ){
		this._.value = v;
		try{
			this._.iframe.contentWindow.document.body.innerHTML = v;
		} catch(e){}
	}
	return this._.value;
};

TEXTAREA.nodeTextLength = function( node ){
	var re = 0;
	for( var i = 0; i < node.childNodes.length; i ++ ){
		switch( node.childNodes[ i ].nodeType ){
			case 1:
				if( node.childNodes[ i ].tagName.toLowerCase() == 'img' ) re += node.childNodes[ i ].alt.length || 2
				if( node.childNodes[ i ].tagName.toLowerCase() == 'br' || node.childNodes[ i ].tagName.toLowerCase() == 'p' ) re ++; 
				re += TEXTAREA.nodeTextLength( node.childNodes[ i ] );
				break;
			case 3:
				re += node.childNodes[ i ].nodeValue.length;
		}
	}
	return re;
}; // TEXTAREA.nodeTextLength = function

TEXTAREA.prototype.textLength = function(){	
	return TEXTAREA.nodeTextLength( this._.iframe.contentWindow.document.body );
}; // TEXTAREA.prototype.textLength = function

TEXTAREA.prototype.insertHTML = function( html ){
	this.focus();
	if( document.all ){		
		oRng = ( this._.sRange ? this._.sRange : this._.iframe.contentWindow.document.selection.createRange() );
		oRng.select();
		oRng.collapse( false );
		oRng.pasteHTML( html );		
	} else {
		this._.iframe.contentWindow.document.execCommand( 'insertHTML', false, html );
	}
	this.checkSelection( true );
	return this;
}; // TEXTAREA.prototype.insertHTML = function

TEXTAREA.prototype.toString = function(){
	return 'www.draugiem.lv TEXTAREA';
}; // TEXTAREA.prototype.toString = function

TEXTAREA.prototype.insertTable = function( e ){
	var this_ = this;
	if( this._.insertTableDiv ){
		this._.insertTableDiv.remove();
	}
	var cols, rows, withHeadings;
	if( typeof xGlobal == 'undefined' ){
		xGlobal = {};
	}
	this._.insertTableDiv = mkE( {
		tag:'div',
		className:'TextareaPopup TextareaInsertTable',
		els:[
			mkE( {
				tag:'a',
				className:'cancelIcon cancelIconSize',
				prop:{
					href:'',
					onclick:function(){
						this_._.insertTableDiv.remove();
						return false;
					}
				}
			} ),
			mkE( {	
				tag:'h6',
				text:xGlobal[ 'Textarea Insert table' ] || 'Insert table'
			} ),
			mkE( {
				tag:'table',
				els:[
					mkE( {
						tag:'tbody',
						els:[
							mkE( {
								tag:'tr',
								els:[
									mkE( {
										tag:'td',
										text:xGlobal[ 'Textarea With headings' ] || 'With headings'
									} ),
									mkE( {
										tag:'td',
										els:[
											withHeadings = mkE( {
												tag:'input',
												prop:{
													type:'checkbox'
												}
											} )
										]
									} )
								]
							} ), // tr
							mkE( {
								tag:'tr',
								els:[
									mkE( {
										tag:'td',
										text:( xGlobal[ 'Textarea Columns' ] || 'Columns' ) + ':'
									} ),
									mkE( {
										tag:'td',
										els:[
											cols = mkE( {
												tag:'input',
												prop:{
													maxLength:2,
													value:2
												}
											} )
										]
									} )
								]
							} ), // tr
							mkE( {
								tag:'tr',
								els:[
									mkE( {
										tag:'td',
										text:( xGlobal[ 'Textarea Rows' ] || 'Rows' ) + ':'
									} ),
									mkE( {
										tag:'td',
										els:[
											rows = mkE( {
												tag:'input',
												prop:{
													maxLength:3,
													value:2
												}
											} )
										]
									} )
								]
							} ) // tr
						]
					} ) // tbody
				]
			} ) // table
		]
	} ).append( this.node );
	var buttons = mkE( {
		tag:'center'
	} ).append( this._.insertTableDiv );
	T.submitButton( {
		caption:xGlobal[ 'Textarea Insert' ] || 'Insert',
		size:'small',
		type:'link',
		color:'link',
		onclick:function(){
			this_._.insertTableDiv.remove();
			this_.focus();
			var html = '<br /><table cellspacing="0" class="simpleTable" width="100%">';
			if( ! ( Number( rows.value ) && Number( cols.value ) ) ){
				return false;
			}
			if( withHeadings.checked ){
				html += '<tr>';
				for( var j = 0; j < Number( cols.value ); j ++ ){
					html += '<th>' + ( xGlobal[ 'Textarea heading' ] || 'heading' ) + String( j + 1 ) + '</th>';
				}
				html += '</tr>';
			}
			for( var i = 0; i < Number( rows.value ); i ++ ){
				html += '<tr class="' + ( i % 2 ? 'nd' : '' ) + '">';
				for( var j = 0; j < Number( cols.value ); j ++ ){
					if( i == 0 && ! withHeadings.checked || j == 0 ){
						html += '<td>' + ( xGlobal[ 'Textarea text' ] || 'text' ) + '</td>';
					} else {
						html += '<td></td>';
					}
				}
				html += '</tr>';
			}
			html += '</table><br />';
			this_.insertHTML( html );
			return false;
		}
	} ).append( buttons );
}; // TEXTAREA.prototype.insertTable

TEXTAREA.prototype.insertSmiley = function( e ){
	var this_ = this;
	if( this._.insertSmileyDiv ){
		this._.insertSmileyDiv.remove();
	}
	this._.insertSmileyDiv = mkE( {
		tag:'div',
		className:'TextareaPopup TextareaInsertSmiley',
		els:[
			mkE( {
				tag:'a',
				className:'cancelIcon cancelIconSize',
				prop:{
					href:'',
					onclick:function(){
						this_._.insertSmileyDiv.remove();
						return false;
					}
				}
			} ),
			this._.insertSmileyContent = mkE( {
				tag:'div',
				els:[
					mkE( {
						tag:'img',
						prop:{
							src:pimg + 'img/load.gif'
						}
					} )
				]
			} )
		]
	} ).append( this.node );	
	if( this._.insertSmileyData ){
		TEXTAREA.insertSmileyRe( this._.insertSmileyData, this );
	} else {
		var rpc = new RPC( '/rq/app.php' );
		rpc.send(
			'getSmiles',
			{},
			TEXTAREA.insertSmileyRe,
			this
		);
	}
}; // TEXTAREA.prototype.insertSmiley

TEXTAREA.prototype.imgOptions = function(){
	if( ! this.menu_icons.imgFloatLeft || ! this.menu_icons.imgFloatRight || ! this.menu_icons.imgNormal ){
		return;
	}
	if( this.activeImg ){
		this.menu_icons.imgFloatLeft.checked( existsClassName( this.activeImg, 'TextareaImgFloatLeft' ) );
		this.menu_icons.imgFloatRight.checked( existsClassName( this.activeImg, 'TextareaImgFloatRight' ) );
		this.menu_icons.imgNormal.checked( ! existsClassName( this.activeImg, 'TextareaImgFloatLeft' ) && ! existsClassName( this.activeImg, 'TextareaImgFloatRight' ) );
		this.menu_icons.imgFloatLeft.show();
		this.menu_icons.imgFloatRight.show();
		this.menu_icons.imgNormal.show();
	} else {
		this.menu_icons.imgFloatLeft.hide();
		this.menu_icons.imgFloatRight.hide();
		this.menu_icons.imgNormal.hide();
	}
}

TEXTAREA.insertSmileyRe = function( re, par ){	
	this_ = par;
	clearNode( this_._.insertSmileyContent );
	this_._.insertSmileyData = re;
	var i = 0, tr, tbody, row;
	mkE( {
		tag:'table',
		els:[
			tbody = mkE( {
				tag:'tbody'
			} )
		]
	} ).append( this_._.insertSmileyContent );	
	for( var k in this_._.insertSmileyData.smiles ){
		row = this_._.insertSmileyData.smiles[ k ];
		if( i % 7 == 0 ){
			tr = mkE( {
				tag:'tr'
			} ).append( tbody );
		}
		mkE( {
			tag:'td',
			els:[
				mkE( {
					tag:'img',
					prop:{
						title:row[ 0 ],
						src:row[ 1 ],
						onclick:function(){
							this_._.insertSmileyDiv.remove();
							this_.focus();
							this_.insertHTML( '<img src="' + this.src + '" />' );
						}
					}
				} )
			]
		} ).append( tr );
		i ++;
	}
}

TEXTAREA.MENU_ICON = function( par ){
	var this_ = this;
	this._ = {
		textarea:par.textarea
	};
	this.onclick = par.onclick;
	this.node = mkE( {
		tag:'div',
		className:'menu_icon',
		prop:{
			title:par.caption || '',
			onmouseover:TEXTAREA.MENU_ICON.icon_onmouseover,
			onmouseout:TEXTAREA.MENU_ICON.icon_onmouseout,
			onclick:function( e ){
				if( ! e ){
					e = window.event;
				}
				if( this_.onclick ){
					this_.onclick( e, par );
				}
			}
		}
	} );
	var icon = mkE( {
		tag:'div',
		className:'img' + ( par.className ? ' ' + par.className : '' )
	} ).append( this.node );
	if( par.icon ){
		icon.style.backgroundImage = "url('" + par.icon  + "')";
	}
	this.append();
}; // TEXTAREA.MENU_ICON = function

TEXTAREA.MENU_ICON.prototype.append = function(){
	this._.textarea._.menu.appendChild( this.node );
	return this;
}; // TEXTAREA.MENU_ICON.prototype.append = function

TEXTAREA.MENU_ICON.prototype.remove = function(){
	removeNode( this.node );
}; // TEXTAREA.MENU_ICON.prototype.remove = function

TEXTAREA.MENU_ICON.prototype.checked = function( v ){
	if( typeof v != 'undefined' ){
		if( v ){
			addClassName( this.node, 'checked' );
		} else {
			removeClassName( this.node, 'checked' );
		}
	}
	return existsClassName( this.node, 'checked' );
}; // TEXTAREA.MENU_ICON.prototype.checked = function

TEXTAREA.MENU_ICON.prototype.hide = function(){
	this.node.style.display = 'none';
};

TEXTAREA.MENU_ICON.prototype.show = function(){
	this.node.style.display = '';
};

TEXTAREA.MENU_ICON.icon_onmouseover = function(){
	addClassName( this, 'hover' );
}; // TEXTAREA.MENU_ICON.icon_onmouseover = function

TEXTAREA.MENU_ICON.icon_onmouseout = function(){
	removeClassName( this, 'hover' );
}; // TEXTAREA.MENU_ICON.icon_onmouseout = function

TEXTAREA.MENU_ICON.prototype.toString = function(){
	return 'www.draugiem.lv TEXTAREA MENU_ICON';
}; //TEXTAREA.MENU_ICON.prototype.toString = function

TEXTAREA.MENU_SEP = function( par ){
	this._ = {
		textarea:par.textarea
	};
	this.node = document.createElement( 'div' );
	this.node.className = 'menu_sep';
	this._.img = document.createElement( 'img' );
	this._.img.src = this._.textarea.iconRoot + 'seperator.gif';
	this.node.appendChild( this._.img );
	this.append();
}; // TEXTAREA.MENU_SEP = function

TEXTAREA.MENU_SEP.prototype.append = function(){
	this._.textarea._.menu.appendChild( this.node );
	return this;
}; // TEXTAREA.MENU_SEP.prototype.append = function

TEXTAREA.MENU_SEP.prototype.toString = function(){
	return 'www.draugiem.lv TEXTAREA MENU_SEP';
}; // TEXTAREA.MENU_SEP.prototype.toString = function

TEXTAREA.MENU_SELECT = function( par ){
	var MENU_SELECT = this;
	this._ = {
		textarea:par.textarea,
		list_items:[]
	};
	this.node = document.createElement( 'div' );
	this.node.className = 'menu_select';
	this._.input = document.createElement( 'input' );
	this._.input.onfocus = function(){
		var outermost = document.getElementById( 'outermost' );
		( outermost ? outermost : document.body ).appendChild( MENU_SELECT._.value_list );
		MENU_SELECT._.value_list.style.minWidth = MENU_SELECT.node.offsetWidth - 2 + 'px';
		MENU_SELECT._.value_list.style.top = TEXTAREA.findPosY( MENU_SELECT.node ) + MENU_SELECT.node.offsetHeight + 'px';
		MENU_SELECT._.value_list.style.left = TEXTAREA.findPosX( MENU_SELECT.node ) + 'px';
		if( MENU_SELECT._.remove_interval ) clearTimeout( MENU_SELECT._.remove_interval );				
	};
	this._.input.onblur = function(){
		MENU_SELECT._.remove_interval = setTimeout( function(){
			removeNode( MENU_SELECT._.value_list );
		}, 200 );
	};
	this.node.appendChild( this._.input );
	this._.value_div = document.createElement( 'div' );
	this._.value_div.className = 'value';	
	this.node.appendChild( this._.value_div );
	this._.arrow = document.createElement( 'div' );
	this._.arrow.className = 'arrow';
	this._.arrow.onclick = function(){		
		MENU_SELECT._.input.focus();
	};
	this.node.appendChild( this._.arrow );
	this._.value_list = document.createElement( 'div' );
	this._.value_list.className = 'TEXTAREA_MENU_SELECT_value_list';
	this._.value_list.innerHTML = '<h1>Header 1</h1>';
	if( par.onChange ){
		this.onChange = par.onChange;
	}
	this.append();
}; // TEXTAREA.MENU_SELECT = function

TEXTAREA.MENU_SELECT.prototype.append = function(){
	this._.textarea._.menu.appendChild( this.node );
	return this;
}; // TEXTAREA.MENU_SELECT.prototype.append = function

TEXTAREA.MENU_SELECT.prototype.toString = function(){
	return 'www.draugiem.lv TEXTAREA MENU_SELECT =' + this.value();
}; // TEXTAREA.MENU_SELECT.prototype.toString = function

TEXTAREA.MENU_SELECT.prototype.setValues = function( values ){
	clearNode( this._.value_list );
	this._.list_items = [];
	for( var i = 0; i < values.length; i ++ ){
		this._.list_items.push(
			new TEXTAREA.MENU_SELECT.LIST_ITEM( {
				MENU_SELECT:this,
				caption:values[ i ].caption,
				listCaption:values[ i ].listCaption || values[ i ].caption,
				value:values[ i ].value
			} )
		);
	}
}; // TEXTAREA.MENU_SELECT.prototype.setValues

TEXTAREA.MENU_SELECT.prototype.value = function( v ){	
	if( typeof v != 'undefined' ){
		this._.value_div.innerHTML = '';
		this._.value = false;
		for( var i = 0; i < this._.list_items.length; i ++ ){
			if( this._.list_items[ i ].value == v ){
				this._.value_div.innerHTML = this._.list_items[ i ].caption;
				this._.value = this._.list_items[ i ].value;
				break;
			}
		}
	}
	return this._.value;
}; // TEXTAREA.MENU_SELECT.prototype.value = function

TEXTAREA.MENU_SELECT.LIST_ITEM = function( par ){
	var LIST_ITEM = this;
	this._ = {
		MENU_SELECT:par.MENU_SELECT
	};
	this.value = par.value;	
	this.caption = par.caption;
	this.node = document.createElement( 'div' );
	this.node.className = 'list_item';
	this.node.innerHTML = par.listCaption; 
	this.node.onmouseover = function(){
		addClassName( this, 'hover' );
	};
	this.node.onmouseout = function(){
		removeClassName( this, 'hover' );
	};
	this.node.onclick = function(){
		removeClassName( this, 'hover' );
		LIST_ITEM._.MENU_SELECT._.value = LIST_ITEM.value;
		LIST_ITEM._.MENU_SELECT._.value_div.innerHTML = LIST_ITEM.caption;
		if( LIST_ITEM._.MENU_SELECT.onChange ){
			LIST_ITEM._.MENU_SELECT.onChange( LIST_ITEM._.MENU_SELECT._.value );
		}
	};
	this._.MENU_SELECT._.value_list.appendChild( this.node );
}; // TEXTAREA.MENU_SELECT.LIST_ITEM = function

TEXTAREA.findPosX = function( obj, parent ){
	var curleft = 0;
	if( obj.offsetParent ){
		while ( obj.offsetParent && obj.offsetParent != parent ){
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	} else if( obj.x ){
		curleft += obj.x;
	}
	return curleft;
}; // TEXTAREA.findPosX
 
TEXTAREA.findPosY = function( obj, parent ){
	var curtop = 0;
	if ( obj.offsetParent ) {
		while ( obj.offsetParent && obj.offsetParent != parent ){
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	} else if ( obj.y ){
		curtop += obj.y;
	}
	return curtop;
}; // TEXTAREA.findPosY