/**
* bbstyle
*/
function fck_bbstyle( bbnumber )
{	
	if ( oEditor.EditMode != 0 )
	{
		return;
	}		
	if ( bbnumber != -1 )
	{
		fck_bbfontstyle( bbtags[bbnumber], bbtags[bbnumber+1] ) ;
	} 
	else 
	{
		fck_insert_text( '[*]' ) ;
		document.forms[form_name].elements[text_name].focus() ;
	}
}

function fck_getRangeObject( selectionObject ) 
{
	if ( selectionObject.getRangeAt )
		return selectionObject.getRangeAt( 0 ) ;
	else 
	{ // Safari!
		var range = document.createRange() ;
		range.setStart( selectionObject.anchorNode,selectionObject.anchorOffset ) ;
		range.setEnd( selectionObject.focusNode,selectionObject.focusOffset ) ;
		return range;
	}
}

/**
* Apply bbcodes
*/
function fck_bbfontstyle( bbopen, bbclose )
{	
	var oEditor = FCKeditorAPI.GetInstance( text_name ) ;
	
	if ( oEditor.EditMode != 0 )
	{
		return;
	}	
	
	theSelection = false;
		
	var newText = bbopen + '_FCKREPLACETEXT' + bbclose ;
	newText = oEditor.DataProcessor.ConvertDataToHtmlTags( newText ) ;
	
	if ( ( clientVer >= 4 ) && is_ie && is_win )
	{
		theSelection = oEditor.EditorDocument.selection.createRange().text ;
	}
	else
	{		
		theSelection = oEditor.EditorDocument.getSelection() ;		
	}
	
	newText = newText.replace( '_FCKREPLACETEXT', theSelection ) ;
	oEditor.InsertHtml( newText ) ;
	
	oEditor.Focus();
	return;
}

function fck_insert_smiley( smiley_code, smiley_img, smiley_width, smiley_height )
{
	var oEditor ;
	
	if ( typeof FCKeditorAPI == 'undefined' ) 
	{
		if ( window.opener.FCKeditorAPI )
			oEditor = window.opener.FCKeditorAPI.GetInstance( text_name ) ;
		else
		{
			insert_text( smiley_code, true, true) ;
			return ;
		}	
	}
	else 
	{
		oEditor = FCKeditorAPI.GetInstance( text_name )
	}
	
	if ( oEditor.EditMode == 0 )
	{
		oEditor.InsertHtml( '<img _fckfakelement="true" src="' + smiley_img + '" alt="' + smiley_code + '" class="_fake_smiley"/>' );
	}
}

function fck_insert_text( text, spaces, popup )
{
	var oEditor = FCKeditorAPI.GetInstance( text_name ) ;
	if ( oEditor.EditMode != 0 )
	{
		return;
	}	
	var textarea = oEditor.EditorDocument.getElementById( "fckcontent" ) ;
	
	if ( spaces ) 
	{
		text = ' ' + text + ' ' ;
	}
	
	if ( !isNaN( textarea.selectionStart ) )
	{
		var sel_start = textarea.selectionStart ;
		var sel_end = textarea.selectionEnd ;

		mozWrap( textarea, text, '' ) ;
		textarea.selectionStart = sel_start + text.length;
		textarea.selectionEnd = sel_end + text.length;
	}
	else if ( textarea.createTextRange && textarea.caretPos )
	{
		if ( baseHeight != textarea.caretPos.boundingHeight ) 
		{
			textarea.focus() ;
			storeCaret(textarea) ;
		}

		var caret_pos = textarea.caretPos ;
		caret_pos.text = caret_pos.text.charAt(caret_pos.text.length - 1) == ' ' ? caret_pos.text + text + ' ' : caret_pos.text + text ;
	}
	else
	{
		textarea.innerHTML = textarea.innerHTML + text ; 
	}
	if ( !popup )
	{
		textarea.focus() ;
	}	
}

/**
*Add inline attachment at position
*/
function fck_attach_inline( index, filename )
{	
	fck_insert_text( '<img alt="' + filename + '" src="' + _attachment_array[index] + '" class="attach-image" title="_fck_attachment_' + index + '"/>' );
	var oEditor = FCKeditorAPI.GetInstance(text_name) ;
	if ( oEditor.EditMode != 0 )
	{
		return;
	}			
	var textarea = oEditor.EditorDocument.getElementById("fckcontent") ;
	textarea.focus() ;
}

/**
* Add quote text to message
*/
function fck_addquote(post_id, username)
{
	var oEditor = FCKeditorAPI.GetInstance( text_name ) ;
	if ( oEditor.EditMode != 0 )
	{
		return ;
	}
	var message_name = 'encoded_' + post_id;
	var theSelection = '';
	var divarea = false;

	if (document.all)
	{
		divarea = document.all[message_name];
	}
	else
	{
		divarea = document.getElementById(message_name);
	}

	// Get text selection - not only the post content :(
	if (window.getSelection)
	{
		theSelection = window.getSelection().toString();
	}
	else if (document.getSelection)
	{
		theSelection = document.getSelection();
	}
	else if (document.selection)
	{
		theSelection = document.selection.createRange().text;
	}

	if (theSelection == '' || typeof theSelection == 'undefined' || theSelection == null)
	{
		if (divarea.innerHTML)
		{
			theSelection = divarea.innerHTML ;
		}
		else if (document.all)
		{
			theSelection = divarea.innerText;
		}
		else if (divarea.textContent)
		{
			theSelection = divarea.textContent;
		}
		else if (divarea.firstChild.nodeValue)
		{
			theSelection = divarea.firstChild.nodeValue;
		}
	}
	else
	{
		theSelection = theSelection.replace(/&/ig, '&amp;');
		theSelection = theSelection.replace(/</ig, '&lt;');
		theSelection = theSelection.replace(/>/ig, '&gt;');
		theSelection = theSelection.replace(/ /ig, '&nbsp;');
		theSelection = theSelection.replace(/\n/ig, '<br/>');		
	}

	if (theSelection)
	{
		oEditor.InsertHtml( '<blockquote title="' + username + '"><div>' + theSelection + '</div></blockquote><br/>' );
	}

	return;
}
