/* ===-----------=== Komanche 2.0 ===-----------===

           Copyright:     © 2004, CBA Consult ltda.
            Coded by:  Luiz Antonio B. Silva [Labs]
                      Todos os direitos reservados.

                http:         www.cbaconsult.com.br
                mail:   comercial@cbaconsult.com.br

   Editor DHTML.

-------------=== Vs. 1.0 - Fev/2004 | By Labs --=== */


// -----------------=== apply()
/* Aplica comandos.
                                                     by Labs - 02/2004 */
function apply(nome, what, interf, value){
  var div_html = document.getElementById("html_" + nome);
  var txt_area = document.getElementById("area_" + nome);

  if (div_html.style.display == 'block'){
      applyDHTML(what, interf, value);
      txt_area.innerText = div_html.innerHTML;
  } else {
     if (what == 'InsertImage'){
        alert('Este comando só funciona em modo layout.');
        return;
     }
     applyText(what, value);
    // div_html.innerHTML = txt_area.innerText;
  }

}


// -----------------=== applyDHTML()
/* Aplica comandos DHTML.
                                                     by Labs - 02/2004 */
function applyDHTML(what, interf, value){
    var selType   = document.selection.type;
	var textRange = document.selection.createRange();

	var msgSel    = "Você deve selecionar algo na página para executar esta operação.";
    var sel = document.selection;
    if (sel != null) {
        var textRange = sel.createRange();
        if (textRange != null){
            textRange.execCommand(what, interf, value);
        }
    }
    return true;

	// ==== Seleção inválida:
	if (textRange == null || textRange == "undefined"){
	    alert(msgSel);
		return false;
	}

	// ==== Cores:
	if (what == "Cor"){
	    if (textRange.text != ""){
          textRange.execCommand("ForeColor", false, value);
          textRange.select();
			    return true;
      }
		return false;
	}

  // ==== Estilos / Hiperlinks / Alinhamentos:
  if (textRange.text != "")
      textRange.execCommand(what, interf, value);
}


// -----------------=== applyText()
/* Aplica comandos Text.
                                                     by Labs - 02/2004 */
function applyText(what, value){
    var selType   = document.selection.type;
	var txtRange  = document.selection.createRange();
	var msgSel    = "Você deve selecionar algo na página para executar esta operação.";

	// ==== Seleção inválida:
	if (txtRange == null || txtRange == "undefined" || txtRange.text == ""){
	    alert(msgSel);
		return false;
	}

  // ==== Caso haja valor, ele deverá estar no formato "val_part1|val_part2":
	if (value){
      values = value.split('|');
  }

  switch (what){
      case 'JustifyRight'  : txtRange.text = '<p align=right>'  + txtRange.text + '</p>';
                             break;

      case 'JustifyCenter' : txtRange.text = '<p align=center>' + txtRange.text + '</p>';
                             break;

      case 'JustifyLeft'   : txtRange.text = '<p align=left>'   + txtRange.text + '</p>';
                             break;

      case 'Underline'     : txtRange.text = '<u>' + txtRange.text + '</u>';
                             break;

      case 'Italic'        : txtRange.text = '<i>' + txtRange.text + '</i>';
                             break;

      case 'Bold'          : txtRange.text = '<b>' + txtRange.text + '</b>';
                             break;

      case 'CreateLink'    : txtRange.text = '<a href="">' + txtRange.text + '</a>';
                             break;

      case 'custom'        : if (_editor_state_ == 'html'){
                                 txtRange.pasteHTML(values[0] + txtRange.htmlText + values[1]);
                             } else {
                                 txtRange.text = values[0] + txtRange.text + values[1];
                             }
                             break;
  }
  txtRange = null;
  document.selection.empty();
  return true;
}


// -----------------=== moveSource()
/* Move o conteudo do div p/ o textarea.
                                                     by Labs - 02/2004 */
function moveSource(nome){
  var div_html = document.getElementById("html_" + nome);
  var txt_area = document.getElementById("area_" + nome);

  if (div_html && txt_area){
      txt_area.innerText = div_html.innerHTML;
  }
}


// -----------------=== swapHTML()
/* Alterna modo HTML/Texto puro.
                                                     by Labs - 02/2004 */
var _editor_state_ = "html";
function swapHTML(nome){
  var div_html = document.getElementById("html_" + nome);
  var txt_area = document.getElementById("area_" + nome);
 // var btn_lay  = document.getElementById("btn_lay_" + nome);
 // var btn_cod  = document.getElementById("btn_cod_" + nome);

  // Muda para código
  if (div_html.style.display == 'block'){
      txt_area.innerText     = div_html.innerHTML;
   //   div_html.style.display = 'none';
   //   txt_area.style.display = 'block';
   //   btn_lay.style.display  = 'block';
   //   btn_cod.style.display  = 'none';
   //   _editor_state_ = "text";

  // Muda para layout
  } else {
      div_html.innerHTML     = txt_area.innerText;
    //  div_html.style.display = 'block';
    //  txt_area.style.display = 'none';
    //  btn_lay.style.display  = 'none';
    //  btn_cod.style.display  = 'block';
    //  _editor_state_ = "html";
  }

}