
function initialize_inputHelpers()
{
	//console.log( 'initializing inputHelpers' );

	// find bbcode-input-elements
	var tas = document.getElementsByTagName( 'textarea' );
	for( var i=0, ta; ta = tas[i]; i++ )
	{
		if( ta.name != 'body' || jQuery(ta).hasClass('bbbuttonsIgnore') )
		{
			//console.log( 'Skipping Textarea ', ta.name );
			continue;
		}
		
		//console.log( 'creating smiley-selector-list' );
		
		var b3 = new BBButtons( ta, 'bbcode', RS.maxLength );
			b3.addButton( new BBButton( 'Link', 'url', bbhandle_link, true ) );
			b3.addButton( new BBButton( 'Zitat', 'quote', bbhandle_quote ) );
			b3.addButton( new BBButton( 'Fett', 'b' ) );
			b3.addButton( new BBButton( 'Kursiv', 'i' ) );

		var sb = new BBButton('Smiley', '');
		sb.setGenerateHandler( bbhandle_smiley );
		sb.setCancelHandler( bbhandle_smiley_abort );
		b3.addButton( sb );

		var cc = document.createElement('li');
		cc.updateCount = bbhandle_characterCounter;
		b3.getButtonlist().insertBefore( cc, b3.getButtonlist().firstChild );
		
		b3.setCharCounter( cc );
		b3.handleChanges();
	}
	
	//console.log( 'initializing inputHelpers - DONE' );
}

var bbhandle_smiley = function( selectedText, bbb )
{
	var smileyList = jQuery.data( this, 'smileyList' );
		
	if( smileyList && smileyList.visible() )
	{
		smileyList.hide();
		return false;
	}
	else if( smileyList )
	{
		smileyList.show();
		return false;
	}
		
	// save a self-reference for use in closure
	var bbbutton = this;

	var onclickHandler = function( e )
	{
		// if no text was selected, get some random string to output
		if( !selectedText )
		{
			var d = new Date();
			selectedText = d.toGMTString();
		}

		// construct the code to return
		var s = this.alt;

		// insert the generated code into the input element
		bbb.insertText( s, s.length );

		// remove the smileyList
		smileyList.hide();
		e.stopPropagation();
		e.preventDefault();
	};
	
	smileyList = new AnyBox( Smilies.getSelector( onclickHandler ) );
	
	smileyList.showHandler( function(){
		var hider;
		hider = function(e)
		{
			smileyList.hide();
			jQuery( document ).unbind( 'click', hider);
			e.stopPropagation();
			e.preventDefault();
		};
		jQuery( document ).bind( 'click', hider );
		return true;
	} ); 
	
	smileyList.bind( this, 'click', null ).show();
	jQuery.data( this, 'smileyList', smileyList );

	return false;
}

var bbhandle_smiley_abort = function()
{
	var sl = jQuery.data( this, 'smileyList' );
	if( sl && sl.hide )
		sl.hide();
}

var bbhandle_quote = function( selectedText, bbb )
{
	var vars = {
		'tag' : this.bbTagName
	};

	for( i in this.bbAttributes )
	{
		vars[i] = this.bbAttributes[i];
	}

	vars.text = selectedText;
	
	var author = prompt( "Autor des Zitats:" );
	if( author )
		vars.author = author;

	var t = bbformat( this.bbFormat, vars, [
		{ 'format' : '[%(tag)=%(author)]%(text)[/%(tag)]', 'required' : ['author'] },
		{ 'format' : '[%(tag)]%(text)[/%(tag)]' }
		] );

	// if there was a selection normally return the constructed code
	if( selectedText )
		return t;

	// insert constructed code with the respective length and then fail the handler
	// to stop BBButtons executing processTag()
	bbb.insertText( t );
	return null;
};

var bbhandle_characterCounter = function( input, maxLength )
{
	if( !maxLength )
		return;

	if( maxLength < input.value.length )
	{
		input.value = input.value.substr( 0, maxLength );
		alert( 'Limit von '+ maxLength +' Zeichen erreicht' );
	}

	var ccMessage = (maxLength - input.value.length) + ' Zeichen übrig';

	if( this.tagName == 'INPUT')
		this.value = ccMessage;
	else
		this.innerHTML = ccMessage;
		
	this.style.visibility = ( (maxLength - input.value.length) < 100 ) ? 'visible' : 'hidden';	
};

// load when DOM ready
jQuery( initialize_inputHelpers );