/**
 * rte.js
 * Simple javascript file to create an iframe and set it up for
 * rich text editing.
 */

// Attach onload event - the simple way
window.onload = function(){
  // The first method
  var iframe = document.createElement("iframe");
  document.getElementsByTagName("body")[0].appendChild(iframe);
  var w3c = iframe.contentDocument !== undefined ? true : false;
  var idoc = w3c ? iframe.contentDocument
                 : iframe.contentWindow.document;
  idoc.open();
  idoc.close();
  idoc.designMode = w3c ? "on" : "On";

  addEvents(iframe);

  var clear = document.getElementById("clear");
  clear.onclick = function(){
    document.getElementById("ta").value = "";
  }
}

function shortenTextArea(){
  var txt = document.getElementById("ta");
  txt.value = txt.value.substr(0, 100);
}

function addEvents(iframe){
  var events = new Array( "mousedown", "mouseup", "mouseout", "mouseover",
                          "click", "keydown", "keyup", "keypress" );
  for(var i = 0; i < events.length; i++){
    if(iframe.contentDocument && iframe.contentDocument.addEventListener){
      // DOM L2
      iframe.contentDocument.addEventListener(events[i], function(oEvent){
        var txt = document.getElementById("ta");
        txt.value = oEvent.type + "()\n" + txt.value;
        shortenTextArea();
      }, false);
    }else if(iframe.contentWindow && iframe.contentWindow.document &&
             iframe.contentWindow.document.attachEvent){
      // IE
      iframe.contentWindow.document.attachEvent("on" + events[i], function(){
        var txt = document.getElementById("ta");
        var event = iframe.contentWindow.document.parentWindow.event;
        txt.value = event.type + "()\n" + txt.value;
        shortenTextArea();
      });
    }
  }

  if(iframe.contentDocument){ // focus, blur
    iframe.contentDocument.addEventListener("blur", function(oEvent){
        var txt = document.getElementById("ta");
        txt.value = oEvent.type + "()\n" + txt.value;
        shortenTextArea();
    }, false);
    iframe.contentDocument.addEventListener("focus", function(oEvent){
        var txt = document.getElementById("ta");
        txt.value = oEvent.type + "()\n" + txt.value;
        shortenTextArea();
    }, false);
  }else if(iframe.contentWindow.document.attachEvent){
    iframe.attachEvent("onblur", function(){
        var txt = document.getElementById("ta");
        var event = window.event;
        txt.value = event.type + "()\n" + txt.value;
        shortenTextArea();
    });
    iframe.attachEvent("onfocus", function(){
        var txt = document.getElementById("ta");
        var event = window.event;
        txt.value = event.type + "()\n" + txt.value;
        shortenTextArea();
    });
  }

  if(iframe.contentDocument){ // select
    // DOM L2
    iframe.contentDocument.addEventListener("mouseup", function(oEvent){
      var txt = iframe.contentDocument.getSelection();
      if(txt.length > 0){
        var ctrl = document.getElementById("ta");
        ctrl.value = oEvent.type + "( " + txt + " )\n" + ctrl.value;
        shortenTextArea();
      }
    }, false);
  }else if(iframe.contentWindow.document.attachEvent){
    // IE
    iframe.contentWindow.document.attachEvent("onmouseup", function(){
      var range = iframe.contentWindow.document.selection.createRange();
      var txt = range.text;
      if(txt.length > 0){
        var ctrl = document.getElementById("ta");
        var event = iframe.contentWindow.document.parentWindow.event;
        ctrl.value = event.type + "( " + txt + " )\n" + ctrl.value;
        shortenTextArea();
      }
    });
  }
}

