/******************************************************************************
 *                                                                            *
 *   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 code is extracted from original editor iframe
*/

var bb = new bbcode();

// Вставляет begin и end вокруг выделения
function xbb_insertTags(begin, end) {
    surroundText(begin, end);
    return false;
}

// Вставка простейших тегов (таких как [b], [i], [u] и т.п.)
function xbb_insertSimpleTags(tag_name) {
    document.getElementById('hidden_div').style.display = 'none';
	xbb_insertTags('[' + tag_name + ']', '[/' + tag_name + ']');
}

// Вставка одинарных тегов (таких как [hr])
function xbb_insertSingleTag(tag_name) {
	xbb_insertTags('[' + tag_name + ']', '');
}

// Вставка смайликов
function xbb_insertSmile(smile) {
	document.getElementById('hidden_div').style.display = 'none';
	xbb_insertSingleTag(smile);
}

// Insert tags [url] and [img]
function xbb_insertLink(tag, text) {
	if ('none' != document.getElementById('hidden_div').style.display) {
        document.getElementById('hidden_div').style.display = 'none';
    }
	var url = prompt(text, "");
	if (! url) { return false; }
	xbb_insertTags('[' + tag + ']' + url + '[/' + tag + ']', '');
    return false;
}

function xbb_insertUrl(tag, prompt_url, prompt_title){
	if ('none' != document.getElementById('hidden_div').style.display) {
        document.getElementById('hidden_div').style.display = 'none';
    }

	var enterURL   = prompt(prompt_url, "");
	if (!enterURL) {return false;}

	var enterTITLE = prompt(prompt_title, "");
	if (!enterTITLE) {return false;}

	xbb_insertTags('['+tag+'='+enterURL+']'+enterTITLE+'[/'+tag+']', '');
	return false;
}

// Insert tag [size]
function xbb_insertSize(size) {
	begin = '[size=' + size + ']';
	end = '[/size]'
    document.getElementById('hidden_div').style.display = 'none';
	xbb_insertTags(begin, end);
	return false;
}

// Insert tag [color]
function xbb_insertColor(color) {
	begin = '[color=' + color + ']';
	end = '[/color]'
    document.getElementById('hidden_div').style.display = 'none';
	xbb_insertTags(begin, end);
	return false;
}

// Определение координат и размеров элемента
function xbb_getCoords(element) {
    var left = element.offsetLeft;
    var top = element.offsetTop;
    for (var parent = element.offsetParent; parent; parent = parent.offsetParent) {
        left += parent.offsetLeft - parent.scrollLeft;
        top += parent.offsetTop - parent.scrollTop
    }
    return {
    	left: left,
    	top: top,
    	width: element.offsetWidth,
    	height: element.offsetHeight
    };
}

// Выводит список смайлов
function xbb_smilesList() {
    var div = document.getElementById('hidden_div');
	if ('none' != div.style.display) {
        div.style.display = 'none';
        return false;
    }
    var coords = xbb_getCoords(document.getElementById('img_smile'));
    div.style.left = coords['left'] + 'px';
    div.style.top = coords['top'] + coords['height'] + 'px';
    var html = '<table border="0" cellpadding="0" cellspacing="1">';
    for (var i = 0; bb.smiles[i]; ++i) {
        html += '<tr>';
        for (var j = 0; bb.smiles[i][j]; ++j) {
            html += '<td><a href="#" '
                + 'onclick="xbb_insertSmile(\'' + bb.smiles[i][j]
                + '\');return false;"><img alt="' + bb.smiles[i][j]
                + '" src="/i//smiles/' + bb.smiles[i][j]
                + '.gif" border="0" /></a></td>';
        }
        html += '</tr>';
    }
    html += '</table>';
    div.innerHTML = html;
    div.style.display = '';
    return false;
}

