// JavaScript Document
var command = "";
IE  = window.ActiveXObject ? true : false;
MOZ = window.sidebar       ? true : false;
function aplicarColor(HexColor){	  
	document.getElementById('HTML_Editor').contentWindow.document.execCommand("forecolor",false,HexColor);	
	document.getElementById('paleta').style.visibility='hidden';
}
function getTexteselect(){
	e = document.getElementById("HTML_Editor");
   if (MOZ){
      var sel = e.contentWindow.getSelection();
   }else{
      var sel = e.contentWindow.document.selection.createRange().text;
   }   
   return sel;
}


function getIframe(){
   e = document.getElementById("HTML_Editor");  
   return e;
}


function InsertarCodigo(code,e){
   if(MOZ){
      insertNodeAtSelection(e.contentWindow, code)
    } else{
      var insert_pos = e.contentWindow.document.selection.createRange();
      insert_pos.pasteHTML(code);
   }
}

function liengroupe(url){
   var e = getIframe();
   var sel = getTexteselect();   
   if (MOZ){
      lien = e.contentWindow.document.createElement("a"); 
      texte = e.contentWindow.document.createTextNode(sel);
      lien.appendChild(texte);   
      lien.setAttribute("class", "groupe");
      lien.setAttribute("href", url);      
   }else {
      var lien = "<a href=\"" + url + "\" class=\"groupe\">" + sel + "</a>";      
   }   
   inserercode(lien, e);
}


function setClasse(nomClasse){
   var e = getIframe();
   var sel = getTexteselect();  
   if (MOZ){
      span = e.contentWindow.document.createElement("span"); 
      texte = e.contentWindow.document.createTextNode(sel);
      span.appendChild(texte);   
      span.setAttribute("class", nomClasse);      
   }else{
      var span = "<span class=\"" + nomClasse + "\">" + sel + "</a>";      
   }   
   inserercode(span, e);
}

function insertNodeAtSelection(win, insertNode){	
 /*
 // get current selection     
   if (MOZ){
      var sel = e.contentWindow.getSelection();
   }else{
      var sel = e.contentWindow.document.selection.text;
   }
   // get the first range of the selection
      // (there's almost always only one range)
      var range = sel.getRangeAt(0);
      // deselect everything
      sel.removeAllRanges();
      // remove content of current selection from document
      range.deleteContents();
      // get location of current selection
      var container = range.startContainer;
      var pos = range.startOffset;
      // make a new range for the new selection
      range=document.createRange();
      if (container.nodeType==3 && insertNode.nodeType==3) {
        // if we insert text in a textnode, do optimized insertion
        container.insertData(pos, insertNode.nodeValue);

        // put cursor after inserted text
        range.setEnd(container, pos+insertNode.length);
        range.setStart(container, pos+insertNode.length);

      } else {
        var afterNode;
        if (container.nodeType==3) {

          // when inserting into a textnode
          // we create 2 new textnodes
          // and put the insertNode in between

          var textNode = container;
          container = textNode.parentNode;
          var text = textNode.nodeValue;

          // text before the split
          var textBefore = text.substr(0,pos);
          // text after the split
          var textAfter = text.substr(pos);

          var beforeNode = document.createTextNode(textBefore);
          afterNode = document.createTextNode(textAfter);

          // insert the 3 new nodes before the old one
          container.insertBefore(afterNode, textNode);
          container.insertBefore(insertNode, afterNode);
          container.insertBefore(beforeNode, insertNode);

          // remove the old node
          container.removeChild(textNode);

        } else {

          // else simply insert the node
          afterNode = container.childNodes[pos];
          container.insertBefore(insertNode, afterNode);
        }

        range.setEnd(afterNode, 0);
        range.setStart(afterNode, 0);
      }

      sel.addRange(range);*/
  };

function getOffsetTop(elm) {
	/*
  var mOffsetTop = elm.offsetTop;
  var mOffsetParent = elm.offsetParent;
  while(mOffsetParent){
    mOffsetTop += mOffsetParent.offsetTop;
    mOffsetParent = mOffsetParent.offsetParent;
  } 
  return mOffsetTop;
  */
}

function getOffsetLeft(elm) {
	
  var mOffsetLeft = elm.offsetLeft;
  var mOffsetParent = elm.offsetParent;
  while(mOffsetParent){
    mOffsetLeft += mOffsetParent.offsetLeft;
    mOffsetParent = mOffsetParent.offsetParent;
  } 
  return mOffsetLeft;
 
}
function PasteImage(){
	alert ('pegar');
	 e=document.getElementById('HTML_Editor'); 
	
	 e.contentWindow.document.execCommand('paste', false, '&lt;img src=""&gt;');	
	}
function insertImage(dir){ 	
	 insertNode=document.createElement("img");	
	 insertNode.src=dir;
	 e=document.getElementById('HTML_Editor'); 
	 if (e.contentWindow.getSelection) // Mozilla y Safari
		var sel = e.contentWindow.getSelection();
	 else if(e.contentWindow.document.getSelection) // Explorer, Mozilla, Opera 8 y Netscape
		var sel = e.contentWindow.document.getSelection();
	 else 
	 	var sel = e.contentWindow.document.selection; //IE 5 e IE 6
   if (MOZ){   
	  var range = sel.getRangeAt(0);
	  sel.removeAllRanges();     
      range.deleteContents();	  
	  var container = range.startContainer;
	  var pos = range.startOffset; 
   }else{	  
	  return;	
   } 	
   
   var afterNode;
      if (container.nodeType==3) { // SI ES UN NODO DEL TIPO 3(texto), SE HACE UN "SPLIT" EN DOS Y EN EL MEDIO SE INSERTA EL NODO NUEVO    
          var textNode = container;
          container = textNode.parentNode;
          var text = textNode.nodeValue;		 
          var PrimerParte= text.substr(0,pos);         
          var SegundaParte = text.substr(pos);
		  alert(PrimerParte+'       '+SegundaParte);
          NuevoPrimerNodo = document.createTextNode(PrimerParte);
          NuevoSegundoNodo = document.createTextNode(SegundaParte);
          container.insertBefore(NuevoSegundoNodo, textNode);
          container.insertBefore(NuevoPrimerNodo, NuevoSegundoNodo);
          container.insertBefore(insertNode, NuevoSegundoNodo);
          container.removeChild(textNode);
     } else {
          afterNode = container.childNodes[pos];
          container.insertBefore(insertNode, afterNode);
    }
}
function Run(ActionEditor){
  if (ActionEditor == "forecolor") {    
      document.getElementById('paleta').style.visibility='visible';
	  return;
  }	
  if (ActionEditor == "createlink") {
    var szURL = prompt("Enter a URL:", "http://");
    if ((szURL != null) && (szURL != "")) {
      document.getElementById('HTML_Editor').contentWindow.document.execCommand("CreateLink",false,szURL);
    }
  }
  if (ActionEditor == "createimage") {
    imagePath = prompt('Enter Image URL:', 'http://');
    if ((imagePath != null) && (imagePath != "")) {
      document.getElementById('HTML_Editor').contentWindow.document.execCommand('InsertImage', false, imagePath);
    }
  } 
 
  if (ActionEditor == "createtable") {
    e = document.getElementById("HTML_Editor");
    rowstext = prompt("enter rows");
    colstext = prompt("enter cols");
    rows = parseInt(rowstext);
    cols = parseInt(colstext);
    if ((rows > 0) && (cols > 0)) {
      table = e.contentWindow.document.createElement("table");
      table.setAttribute("border", "1");
      table.setAttribute("cellpadding", "2");
      table.setAttribute("cellspacing", "2");
      tbody = e.contentWindow.document.createElement("tbody");
      for (var i=0; i < rows; i++) {
        tr =e.contentWindow.document.createElement("tr");
        for (var j=0; j < cols; j++) {
          td =e.contentWindow.document.createElement("td");
          br =e.contentWindow.document.createElement("br");
          td.appendChild(br);
          tr.appendChild(td);
        }
        tbody.appendChild(tr);
      }
      table.appendChild(tbody);     
      insertNodeAtSelection(e.contentWindow, table);
    }
  } else {
    document.getElementById('HTML_Editor').contentWindow.document.execCommand(ActionEditor, false, null);
  }
}

function Font(valor){ 
  /* First one is always a label */
  if (valor != "") {   
    document.getElementById('HTML_Editor').contentWindow.document.execCommand('fontname', false, valor);
    document.getElementById('Fuente').selectedIndex = 0;
  }
  document.getElementById("HTML_Editor").contentWindow.focus();
}
function FontSize(valor){ 
  /* First one is always a label */
  if (valor != "") {   
    document.getElementById('HTML_Editor').contentWindow.document.execCommand('fontsize', false, valor);
    document.getElementById('FuenteSize').selectedIndex = 0;
  }
  document.getElementById("HTML_Editor").contentWindow.focus();
}


function dismisscolorpalette(){
  document.getElementById("colorpalette").style.visibility="hidden";
}

function InitEditor() {  
  document.getElementById('HTML_Editor').contentWindow.document.designMode = "on";
  
  try {	
    document.getElementById('HTML_Editor').contentWindow.document.execCommand("undo", false, null);
  }  catch (e) {
	  document.getElementById('TextEditorFlag').value='0';
	  document.getElementById('tb_Editor').style.display='none';
	  document.getElementById('tb_TextArea').style.display='';
  }/*
  if (document.addEventListener) {
    document.addEventListener("mousedown", dismisscolorpalette, true);
    document.getElementById("HTML_Editor").contentWindow.document.addEventListener("mousedown", dismisscolorpalette, true);
    document.addEventListener("keypress", dismisscolorpalette, true);
    document.getElementById("HTML_Editor").contentWindow.document.addEventListener("keypress", dismisscolorpalette, true);
  } else if (document.attachEvent) {
    document.attachEvent("mousedown", dismisscolorpalette, true);
    document.getElementById("HTML_Editor").contentWindow.document.attachEvent("mousedown", dismisscolorpalette, true);
    document.attachEvent("keypress", dismisscolorpalette, true);
    document.getElementById("HTML_Editor").contentWindow.document.attachEvent("keypress", dismisscolorpalette, true);
  }*/
  
}
