/********************[EDITOR]********************/
function getEDoc() {
  if(IE) return window.frames[EDITOR_ID].document;
  else return document.getElementById(EDITOR_ID).contentDocument;
  return null;
}

function getEWin() {
  if(IE) return window.frames[EDITOR_ID];
  else return document.getElementById(EDITOR_ID).contentWindow;
  return null;
}

function initEditor(content, css) {
  if(!IE && !MIDAS){
    alert('Votre navigateur n\'est pas compatible avec ce système d\'éditeur WYSIWYG.');
    return false;
  }
  eDoc = getEDoc();
  if(eDoc.designMode != 'On') eDoc.designMode = 'On';
  attach_event(eDoc, "click", editorClick);
  eDoc.body.innerHTML = content;
  var link=document.createElement('link');
  link.setAttribute('rel', 'stylesheet');
  link.setAttribute('type', 'text/css');
  link.setAttribute('href', css);
  eDoc.getElementsByTagName("head")[0].appendChild(link);
  return true;
}

function widgetNavigatorInit(element) {
  var ul = appendElement(element, "ul");
  for(var i in widgets) {
    var li = appendElement(ul, "li");
    appendText(li, i);
    alert(i);
  }
}

function tag(value, start, end, tag, args) {
}

function editorSetContent(content) {


}

function editorInsert(html) {
  if(editorAreaId=='editorIframeArea') {
    eDoc=getEDoc();
    eWin=getEWin();
    eDoc.execCommand("inserthtml", false, html);
    eWin.focus();
  } else if(editorAreaId=='editorTextArea') {
    var editor = document.getElementById(editorAreaId);
    var start = editor.selectionStart;
    var end = editor.selectionEnd;
    var value = editor.value;
    editor.value = value.substr(0, start) + html + value.substr(end, value.length - end);
    editor.focus();
  }
}

function editorTag(tag, args) {
  if(editorAreaId=='editorIframeArea') {
    eDoc=getEDoc();
    eWin=getEWin();
    eDoc.execCommand("inserthtml", false, "<"+tag + (args?' '+args:'') +"/>");
    eWin.focus();
  } else if(editorAreaId=='editorTextArea') {
    var editor = document.getElementById(editorAreaId);
    var start = editor.selectionStart;
    var end = editor.selectionEnd;
    var value = editor.value;
    editor.value = value.substr(0, start) + '<' + tag + (args?' '+args:'') + '>' + value.substr(start, end - start) + '</' + tag + '>' + value.substr(end, value.length - end);
    editor.focus();
  }
}

function editorExec(cmd, arg) {
  if(editorAreaId=='editorIframeArea') {
    eDoc=getEDoc();
    eWin=getEWin();
    eDoc.execCommand(cmd, false, arg);
    eWin.focus();
    return;
  } else if(editorAreaId=='editorTextArea') {
    switch(cmd) {
    case 'formatblock':
      editorTag(arg);
      break;
    case 'bold':
      editorTag('b');
      break;
    case 'italic':
      editorTag('i');
      break;
    case 'strikethrough':
      editorTag('strike');
      break;
    case 'underline':
      editorTag('u');
      break;
    case 'insertorderedlist':
      editorTag('ol');
      break;
    case 'insertunorderedlist':
      editorTag('ul');
      break;
    case 'justifycenter':
      editorTag('p', 'align="center"');
      break;
    case 'justifyleft':
      editorTag('p', 'align="left"');
      break;
    case 'justifyright':
      editorTag('p', 'align="right"');
      break;
    case 'justifyfull':
      editorTag('p', 'align="full"');
      break;
    default:
      alert(cmd + ":" + arg + ":" + editor.selectionStart + ":" + editor.selectionEnd);
    }
  }
}

function editorClick(event) {
  var e = event || window.event;
  var target = e.target || e.srcElement;//IE events
  if(currentElement) currentElement.style.border = currentElementBorder;
  currentElementBorder = target.style.border;
  //target.style.border = "solid red 2px";
  currentElement = target;
  toolBoxElementSelect(document.getElementById("toolBoxElement"), target);
}

function toolBoxElementSelect(toolBoxElement, element) {
  var properties = Array("class", "id", "name", "onclick", "href", "src", "onfocus", "onblur", "ondblclick", "onsubmit", "value");
  if(toolBoxElement) {
    clearNode(toolBoxElement);
    var h3 = appendElement(toolBoxElement, "h3");
    appendText(h3, element.tagName);

    var ul = appendElement(toolBoxElement, "ul");
    for(var i=0; i<properties.length; i++) {
      if(element.hasAttribute(properties[i])) {
	var li = appendElement(ul, "li");
	appendText(li, properties[i] + ": " + element[properties[i]]);
      }
    }
  }
}

function formExec(form) {
  editorExec(form.cmd.value, form.arg.value);
  return false;
}

function getTemplateTag(el) {
  while(el && !el.tag) el=el.parentNode;
  if(el) return el.tag;
  else   return undefined;
}

function editorA() {
  alert(getTemplateTag(currentElement));
}

function editorFun() {
  function aux(el, sp) {
    var s="";
    var childNodes = elementChildNodes(el);
    for(var i=0; i<childNodes.length; i++) {
      if(childNodes[i].nodeType == Node.ELEMENT_NODE) {
        s += sp + childNodes[i].tagName + ":" + childNodes[i].getAttribute("tag") + "\n";
        s += aux(childNodes[i], sp + " ");
      }
    }
    return s;
  }
  alert(aux(getEDoc().body, ""));
  //  alert(.documentElement.innerHTML);
}

