function append( txtArea, insertable ){
	txtArea.value += 'string' == typeof insertable ? insertable : insertable( '' );
}

function applyHeader( txtArea, selector ){
	if( 0 == selector.value.length ) return;
	insertAtCursor( txtArea, function( sel ){ return '\n' + selector.value + ' ' + sel } )
	selector.down( 'option' ).selected = true;
}

var thumbFunction = function( wikiTag, sugg, withCK, text, elem, source ){
	if( !elem.down( 'img' ) ) return;
	var name = text.substring( 0, text.indexOf( ' : ' ) );
	new Ajax.Request( imgs4PoiUrl, {
		parameters:{ 'id':elem.readAttribute( 'compositeKey' ).replace( /\/.*/, '' ) }, 
		onSuccess:function( e ){
			var ids = eval( "("+e.responseText+")" );
			sugg.container.update( name );
			sugg.active = true;
			ids.each( function( id, index ){
				var img = new Element( 'img', { src:imgUrl + '/' + id, 'className':'galery' } );
				img.observe( 'click', function( e ){
					insertAtCursor( editorTextArea, function( sel ){ 
						return ' [' + wikiTag + '|' + id + '|' + 
						( withCK ? elem.readAttribute( 'compositeKey' ) + '|' : '' ) + name + '] '; 
					} );
					sugg.active = false;
					sugg.hideSugg();
				} );
				sugg.container.insert( img );
				if( 0 == index % 2 ) sugg.container.insert( new Element( 'br' ) );
			} );
		}
	} );
}

var wikiEditorInitialized = false;

function initWikiEditor(){
	if( wikiEditorInitialized ) return;
	if( null == $( editorTextArea ) ){
		if( 0 == $$( 'textarea' ).size() ) return;
		editorTextArea = $$( 'textarea' )[ 0 ];
	}
	editorTextArea = $( editorTextArea );
	if( null != $( 'poiSuggest' ) && 'off' == $( 'poiSuggest' ).readAttribute( 'autocomplete' ) ) return;
	if( null != editorTextArea.selectionStart ) editorTextArea.observe( 'select', function(e){ scrollTop = e.element().scrollTop } );
	var s = new Suggestion( $( 'poiSuggest' ) );
	s.onSelect = function( original, element, source ){
		insertAtCursor( editorTextArea, function( sel ){ 
			return ' [poi|' + element.readAttribute( 'compositeKey' ) + '|' + original.substring( 0, original.indexOf( ' : ' )) + '] '; 
		} );
		s.active = false;
		s.hideSugg();
	};
	var sPoiThumb = new Suggestion( $( 'poiThumbSuggest' ) );
	var sThumb = new Suggestion( $( 'thumbSuggest' ) );
	sPoiThumb.onSelect = thumbFunction.curry( 'poiThumb', sPoiThumb, true );
	sThumb.onSelect = thumbFunction.curry( 'thumb', sThumb, false );
	if( $( 'uploadForm' ) ){
		$( 'fileInput' ).observe( 'change', function( e ){
			$( 'uploadSpinner' ).show();
			$( 'uploadForm' ).submit();
		} );
		$( 'uploadForm' ).target = 'receivingFrame';
		$( 'receivingFrame' ).observe( 'load', function(){
			if( !$F( 'fileInput' ) ) return
			try{
				$( 'uploadSpinner' ).hide();
				var data = eval( "(" + frames[ 'receivingFrame' ].window.document.body.innerHTML + ")" );
				if( data.error ) alert( data.error );
				else if( confirm( imageLoadedLabel + data.size + '\n' + insertImageInPostLabel ) ) 
					insertAtCursor( editorTextArea, function( sel ){
						return ' [img|' + data.url + ( data.thumb ? '|' + data.thumb : '' ) + '] '; 
					}, false );
			}catch( exc ){}
		} );
	}
	wikiEditorInitialized = true;
}

document.observe( 'dom:loaded', initWikiEditor );

function insertImage( txtArea, msg, msgImage ){
    var url = window.prompt( msg );
    if( !url ) return null;
    var thumb = window.prompt( msgImage );
    if( !thumb ) thumb = url;
	insertAtCursor( txtArea, function( sel ){
		return url ? ' [img|' + url + '|' + thumb + ']' : null; 
	} );
}

function insertInnerLink( txtArea, link, name ){
	insertAtCursor( txtArea, function( sel ){ return ' [poiPage|' + link + '|' + name + ']' } );
}

function insertVideo( txtArea, msg ){
	var url = window.prompt( msg );
	insertAtCursor( txtArea, function( sel ){
		return url ? ' [youtube|' + url.substr( url.indexOf( '=' ) + 1 ) + ']' : null; 
	} );
}

function insertURL( txtArea, msgUrl, msgText ){
    var url = window.prompt( msgUrl );
	if( !url ) return null;
	var text = window.prompt( msgText );
	insertAtCursor( txtArea, function( sel ){
		if( !text || 0 == text.length ){
			text = 20 > url.length ? url : url.substr( 0, 12 ) + '..' + url.substr( url.length - 5 ); 
		}
		return ' [' + url + '|' + text + ']';
	} );
}