
/*
 * This file provides integration of HTMLArea client-side RTE (available from www.dynarch.com)
 * to be integrated as RTE into toxA.CMS, available from http://cms.toxa.de .
 */


HTMLAreaBind.globallyDisabled = false;


HTMLAreaBind.allEditors   = new Array();
HTMLAreaBind.initialized  = false;
HTMLAreaBind.filterConfig = false;




function HTMLAreaBind() {
}


HTMLAreaBind.requireDynUI = function() {

	if ( typeof DynUI == "undefined" ) {

		alert( "ERROR: HTMLAreaBind requires DynUI!\n\nPlease link dynui.js before htmlarea_bind.js!" );

		return false;

	}


	return true;

}







HTMLAreaBind.getEditorOnTextareaIdx = function( ta ) {

	var i;


	for ( i = 0; i < HTMLAreaBind.allEditors.length; i++ )
		if ( ( typeof HTMLAreaBind.allEditors[i] == "object" ) &&
					( HTMLAreaBind.allEditors[i]._textArea == ta ) )
			return i;


	return false;

}
HTMLAreaBind.getEditorOnTextarea = function( ta ) {

	var i = HTMLAreaBind.getEditorOnTextareaIdx( ta );
	if ( ( typeof i == "number" ) && ( i >= 0 ) )
		return HTMLAreaBind.allEditors[i];


	return false;

}

HTMLAreaBind.getTextareaOnEditorIdx = function( editor ) {

	var i;


	for ( i = 0; i < HTMLAreaBind.allEditors.length; i++ )
		if ( ( typeof HTMLAreaBind.allEditors[i] == "object" ) &&
						( HTMLAreaBind.allEditors[i] == editor ) )
			return i;


	return false;

}
HTMLAreaBind.getTextareaOnEditor = function( editor ) {

	var i = HTMLAreaBind.getTextareaOnEditorIdx( editor );
	if ( ( typeof i == "number" ) && ( i >= 0 ) )
		return HTMLAreaBind.allEditors[i]._textArea;


	return false;

}


HTMLAreaBind.textareaEnabled = function( textarea ) {
	return ( HTMLAreaBind.getEditorOnTextarea( textarea ) != false );
}

HTMLAreaBind.getNextEditorIdx = function() {
	return HTMLAreaBind.allEditors.length;
}



HTMLAreaBind.configure = function() {

	var config = new HTMLArea.Config();

//	config.width  = "520px";
//	config.height = "300px";

	config.sizeIncludesToolbar = false;
	config.statusBar = false;

	config.toolbar = [
		[
			'fontname', 'space', 'fontsize', 'separator',
			'createlink', 'inserttable', 'htmlmode', 'separator',
			'about'
		],
		[
			'bold', 'italic', 'underline', 'separator',
			'forecolor', 'separator',
			'justifyleft', 'justifycenter', 'justifyright', 'justifyfull', 'separator',
			'orderedlist', 'unorderedlist', 'outdent', 'indent'
		]
	];


	if ( typeof HTMLAreaBind.filterConfig == "function" )
		config = HTMLAreaBind.filterConfig( config );


	return config;

}



/*
 * Include call this method in your document-onload-handler so HTMLArea
 * will be started on every textarea marked to use RTE capabilities.
 */

HTMLAreaBind.use = function() {

	if ( HTMLAreaBind.globallyDisabled )
		/* some easy switch to disable all RTE editors on page */
		return true;


	var textareas = document.getElementsByTagName( "TEXTAREA" );
	if ( !textareas || !textareas.length )
		return false;


	HTMLAreaBind.init( HTMLAreaBind.all );


	return true;

}


/*
 * Initializes HTMLArea, filters multiple requests for initializing HTMLArea.
 */

HTMLAreaBind.init = function( initialSetupCB ) {

	if ( HTMLAreaBind.globallyDisabled )
		/* some easy switch to disable all RTE editors on page */
		return true;


	if ( !HTMLAreaBind.initialized ) {

		HTMLArea.init();

		if ( typeof initialSetupCB == "function" )
			HTMLArea.onload = initialSetupCB;


		HTMLAreaBind.initialized = true;

	}
}


/*
 * Replaces document's all textareas, that are rendered (visible or hidden)
 * by HTMLArea, paying attention to whether each found textarea is enabled to
 * get extended by client-side RTE capabilities.
 */

HTMLAreaBind.all = function() {

	var i, j, config = HTMLAreaBind.configure();


	/*
	 * 1. find all textareas that aren't disabled
	 * 2. create editor instance on each found
	 */

	var tas = document.getElementsByTagName( "TEXTAREA" );
	for ( i = tas.length - 1; i >= 0; i-- )
		if ( tas[i].id != 'disable_rte' )
			/* exclude this textarea if it is not rendered at
			   the moment (having no dimensions) */
			if ( ( tas[i].offsetWidth > 0 ) && ( tas[i].offsetHeight > 0 ) ) {

				if ( !HTMLAreaBind.textareaEnabled( tas[i] ) ) {

					j = HTMLAreaBind.getNextEditorIdx();

					HTMLAreaBind.allEditors[j] = new HTMLArea( tas[i], config );
					HTMLAreaBind.allEditors[j].generate();

				}
			}


	return true;

}


HTMLAreaBind.single = function( item, alt_config ) {

	if ( HTMLAreaBind.globallyDisabled )
		/* some easy switch to disable all RTE editors on page */
		return true;

	if ( !HTMLAreaBind.requireDynUI() )
		return false;


	return DynUI.iterateTreeDown( item, HTMLAreaBind.textareaFCB, HTMLAreaBind.singleCB, alt_config );

}
HTMLAreaBind.textareaFCB = function( item, dummy ) {
	return ( ( item.nodeType == 1 ) && ( item.nodeName == "TEXTAREA" ) &&
			 ( item.id != 'disable_rte' ) && ( item.offsetWidth >= 0 ) &&
			 ( item.offsetHeight >= 0 ) );
}
HTMLAreaBind.singleCB = function( item, alt_config ) {

	var j, config;


	HTMLAreaBind.init();


	if ( !HTMLAreaBind.textareaEnabled( item ) ) {

		if ( typeof alt_config == "function" )
			config = alt_config();
		else if ( ( typeof alt_config == "object" ) && ( alt_config.length > 0 ) )
			config = alt_config;
		else
			config = HTMLAreaBind.configure();


		j = HTMLAreaBind.getNextEditorIdx();

		HTMLAreaBind.allEditors[j] = new HTMLArea( item, config );
		HTMLAreaBind.allEditors[j].generate();

	}


	return true;

}


HTMLAreaBind.reattach = function( item, alt_config ) {

	if ( HTMLAreaBind.globallyDisabled )
		/* some easy switch to disable all RTE editors on page */
		return true;

	if ( !HTMLAreaBind.requireDynUI() )
		return false;


	return DynUI.iterateTreeDown( item, HTMLAreaBind.textareaFCB, HTMLAreaBind.reattachCB, alt_config );

}
HTMLAreaBind.reattachCB = function( item, dummy ) {

	var idx, j, config;


	idx = HTMLAreaBind.getEditorOnTextareaIdx( item );
	if ( typeof idx != "number" )
		return false;


	HTMLAreaBind.init();


	with ( HTMLAreaBind.allEditors[idx] ) {

		if ( _editMode == "textmode" )
			setMode( "wysiwyg" );

		_textArea.value = getHTML();

		/* trim code */
		_textArea.value = _textArea.value.replace( /^\s+/, '' );
		_textArea.value = _textArea.value.replace( /\s+$/, '' );

		_htmlArea.parentNode.removeChild( _htmlArea );

		/* in wysiwyg mode textarea isn't rendered, so get it
		   rendered while hidden, since generate using its
		   dimensions to fit dimensions of htmlarea */
		_textArea.style.visibility = "hidden";
		_textArea.style.display = "block";

		generate();

		_textArea.style.visibility = "visible";

	}


	return true;

}
HTMLAreaBind.reattachAfterMove = function( item, index, alt_config ) {
	return HTMLAreaBind.reattach( item, alt_config );
}



HTMLAreaBind.exportContentsAll = function() {

	if ( HTMLAreaBind.globallyDisabled )
		/* some easy switch to disable all RTE editors on page */
		return true;


	var i;


	for ( i = 0; i < HTMLAreaBind.allEditors.length; i++ )
		with ( HTMLAreaBind.allEditors[i] )
			_textArea.value = getHTML();



	return true;

}

