/* ===-----------=== 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 --=== */

var imgSelType;
var imgRange;

// -----------------=== 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";
  }

}

// -----------------=== applyText()
/* Aplica comandos Text.
                                                     by Labs - 02/2004 */
function applyImage(id_area, value){

  // ==== Caso haja valor, ele deverá estar no formato "val_part1|val_part2":
  if (value){
    values = value.split('|');
  } else {
    return false;
  }
  if (imgSelType == 'Control'  && imgRange && imgRange(0).tagName == 'IMG' && checkParent(imgRange(0).parentElement, id_area)){
    if (values[0]) imgRange(0).src    = values[0];
    if (values[1]) imgRange(0).align  = values[1];
    if (values[2]) imgRange(0).border = values[2];
    if (values[3]) imgRange(0).width  = values[3];
    if (values[4]) imgRange(0).height = values[4];
    return true;
    
  } else {
    resultado  = '<img src="' + values[0] + '"';
    resultado += (values[1] ? ' align="'  + values[1] + '"' : '' );
    resultado += (values[2] ? ' border="' + values[2] + '"' : '' );
    resultado += (values[3] ? ' width="'  + values[3] + '"' : '' );
    resultado += (values[4] ? ' height="' + values[4] + '"' : '' );
    resultado += '>';
  }
  
  if (_editor_state_ == 'html'){
      ( imgRange && checkParent(imgRange.parentElement(), id_area) ) ? 
                                       imgRange.pasteHTML(resultado) : 
                                       document.getElementById(id_area).innerHTML += resultado;
  } else {
      ( imgRange && checkParent(imgRange.parentElement(), id_area) ) ? 
                                           imgRange.text = resultado : 
                                           document.getElementById(id_area).innerHTML += resultado;
  }

  imgRange = null;
  imgSelType = "";
  document.selection.empty();
  return true;
}

function checkParent(obj, parent_id){
    
    while (obj){
        if (obj.getAttribute('id') && obj.getAttribute('id') == parent_id) return true;
        obj = obj.parentElement;
    }
    return false;
}
