/******************************************************************************
 *                                                                            *
 *   bbcode.lib.js, v 0.00 2007/07/25 - This is part of xBB library           *
 *   Copyright (C) 2006-2007  Dmitriy Skorobogatov  dima@pc.uz                *
 *                                                                            *
 *   This program is free software; you can redistribute it and/or modify     *
 *   it under the terms of the GNU General Public License as published by     *
 *   the Free Software Foundation; either version 2 of the License, or        *
 *   (at your option) any later version.                                      *
 *                                                                            *
 *   This program is distributed in the hope that it will be useful,          *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
 *   GNU General Public License for more details.                             *
 *                                                                            *
 *   You should have received a copy of the GNU General Public License        *
 *   along with this program; if not, write to the Free Software              *
 *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
 *                                                                            *
 ******************************************************************************/

/*
 * This is stripped version of original editor js file.
*/

function bbcode() {
    this.smiles = [
		['acute', 'rupor', 'kiss', 'angel', 'bad', 'beach', 'smoke', 'fingal'],
		['red', 'clap', 'super', 'comando', 'tease2', 'cool', 'crazy', 'dance'],
		['wall', 'devil', 'wtf', 'victory', 'yahoo', 'wub', 'drink', 'zzz'],
		['fool', 'friends', 'girl2', 'rose', 'ok', 'chortle', 'sos', 'sorry'],
		['stop', 'romance', 'tease', 'this', 'wow', 'smile', 'angry', 'yes'],
		['sad', 'scare', 'poet', 'pardon', 'preved', 'friday', 'read', 'lol'],
		['rolleyes', 'jeer', 'boks', 'privet', 'nohul', 'spider', 'cop', 'horse'],
		['cowboy', 'gold', 'music', 'no', 'bow', 'popcorn', 'suicide2', 'dunno'],
		['zombie', 'critic', 'laugh', 'defeat', '', '', '', ''],
	];
}

// Remember the current position.
function storeCaret(text) {
	// Only bother if it will be useful.
	if (typeof(text.createTextRange) != "undefined")
		text.caretPos = document.selection.createRange().duplicate();
}

// Surrounds the selected text with text1 and text2.
function surroundText(text1, text2, textarea) {
	textarea = xbb_textarea;
	// Can a text range be created?
	if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange) {
		var caretPos = textarea.caretPos, temp_length = caretPos.text.length;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text1 + caretPos.text + text2 + ' ' : text1 + caretPos.text + text2;

		if (temp_length == 0) {
			caretPos.moveStart("character", -text2.length);
			caretPos.moveEnd("character", -text2.length);
			caretPos.select();
		}
		else
			textarea.focus(caretPos);
	}
	// Mozilla text range wrap.
	else if (typeof(textarea.selectionStart) != "undefined") {
		var begin = textarea.value.substr(0, textarea.selectionStart);
		var selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd - textarea.selectionStart);
		var end = textarea.value.substr(textarea.selectionEnd);
		var newCursorPos = textarea.selectionStart;
		var scrollPos = textarea.scrollTop;

		textarea.value = begin + text1 + selection + text2 + end;

		if (textarea.setSelectionRange) {
			if (selection.length == 0)
				textarea.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length);
			else
				textarea.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length);
			textarea.focus();
		}
		textarea.scrollTop = scrollPos;
	}
	// Just put them on the end, then.
	else {
		textarea.value += text1 + text2;
		textarea.focus(textarea.value.length - 1);
	}
}

function tag_list(){
	var listvalue = "init";
	var thelist = "";

	while ( (listvalue != "") && (listvalue != null) )
	{
	listvalue = prompt(list_prompt, "");
	if ( (listvalue != "") && (listvalue != null) )
	{
	thelist = thelist+"[*]"+listvalue+"\n";
	}
	}

	if ( thelist != "" )
	{
	doinsert( "[list]\n" + thelist, "[/list]\n");
	}
}
