$(document).ready(function() {

	var campo_texto = document.getElementById("sobremim");
	var campo_texto2 = document.getElementById("equipamento");
	var selecao;
	var selecao_inicio;
	var selecao_fim
	var texto;

	$(".btt_negrito").click(function() {
		selecao = altera_selecao("[b]", "[/b]");
		campo_texto.value = selecao;
	});

	$(".btt_italico").click(function() {
		selecao = altera_selecao("[i]", "[/i]");
		campo_texto.value = selecao;
	});

	$(".btt_sublinhado").click(function() {
		selecao = altera_selecao("[u]", "[/u]");
		campo_texto.value = selecao;
	});

	$(".btt_tachado").click(function() {
		selecao = altera_selecao("[s]", "[/s]");
		campo_texto.value = selecao;
	});

	$(".btt_img").click(function() {
		selecao = altera_selecao("[img]", "[/img]");
		campo_texto.value = selecao;
	});

	$(".btt_url").click(function() {
		selecao = altera_selecao('[url="', '"][/url]');
		campo_texto.value = selecao;
	});

	$(".btt_quote").click(function() {
		selecao = altera_selecao("[quote]", "[/quote]");
		campo_texto.value = selecao;
	});

	$(".btt_youtube").click(function() {
		selecao = altera_selecao("[youtube]", "[/youtube]");
		campo_texto.value = selecao;
	});



	$(".bt_negrito").click(function() {
		selecao = altera_seleca("[b]", "[/b]");
		campo_texto2.value = selecao;
	});

	$(".bt_italico").click(function() {
		selecao = altera_seleca("[i]", "[/i]");
		campo_texto2.value = selecao;
	});

	$(".bt_sublinhado").click(function() {
		selecao = altera_seleca("[u]", "[/u]");
		campo_texto2.value = selecao;
	});

	$(".bt_tachado").click(function() {
		selecao = altera_seleca("[s]", "[/s]");
		campo_texto2.value = selecao;
	});

	$(".bt_img").click(function() {
		selecao = altera_seleca("[img]", "[/img]");
		campo_texto2.value = selecao;
	});

	$(".bt_url").click(function() {
		selecao = altera_seleca('[url="', '"][/url]');
		campo_texto2.value = selecao;
	});

	$(".bt_quote").click(function() {
		selecao = altera_seleca("[quote]", "[/quote]");
		campo_texto2.value = selecao;
	});

	$(".bt_youtube").click(function() {
		selecao = altera_seleca("[youtube]", "[/youtube]");
		campo_texto2.value = selecao;
	});

	function altera_selecao(tag_inicio, tag_fim) {

		texto = campo_texto.value;

		if (campo_texto.selectionEnd) {
			selecao_inicio = campo_texto.selectionStart;
			selecao_fim = campo_texto.selectionEnd;
		} else if (document.selection) {
			// IE - Com bug, necessita correção
            var txtarea = campo_texto;
			var range = document.selection.createRange();
            var stored_range = range.duplicate();
            stored_range.moveToElementText(range);
            stored_range.setEndPoint('EndToEnd', range);
            txtarea.selectionStart = stored_range.text.length - range.text.length;
            txtarea.selectionEnd = txtarea.selectionStart + range.text.length;

            selecao_inicio = txtarea.selectionStart;
            selecao_fim = txtarea.selectionEnd;

		}

		novo_texto = texto.substring(0, selecao_inicio);
		novo_texto += tag_inicio + texto.substring(selecao_inicio, selecao_fim) + tag_fim;
		novo_texto += texto.substring(selecao_fim, texto.length);

		$("#reverter_acao").attr("disabled", false);

        return novo_texto;

	}

	function altera_seleca(tag_inicio, tag_fim) {

		texto = campo_texto2.value;

		if (campo_texto.selectionEnd) {
			selecao_inicio = campo_texto2.selectionStart;
			selecao_fim = campo_texto2.selectionEnd;
		} else if (document.selection) {
			// IE - Com bug, necessita correção
            var txtarea = campo_texto2;
			var range = document.selection.createRange();
            var stored_range = range.duplicate();
            stored_range.moveToElementText(range);
            stored_range.setEndPoint('EndToEnd', range);
            txtarea.selectionStart = stored_range.text.length - range.text.length;
            txtarea.selectionEnd = txtarea.selectionStart + range.text.length;

            selecao_inicio = txtarea.selectionStart;
            selecao_fim = txtarea.selectionEnd;

		}

		novo_texto = texto.substring(0, selecao_inicio);
		novo_texto += tag_inicio + texto.substring(selecao_inicio, selecao_fim) + tag_fim;
		novo_texto += texto.substring(selecao_fim, texto.length);

		$("#reverter_acao").attr("disabled", false);

        return novo_texto;

	}

});

