var command = "";
var prefix = "http://www.miminet.cz/";

function InitToolbarButtons() {
  var kids = document.getElementsByTagName('DIV');

  for (var i=0; i < kids.length; i++) {
    if (kids[i].className == "imagebutton") {
      kids[i].onmouseover = tbmouseover;
      kids[i].onmouseout = tbmouseout;
      kids[i].onmousedown = tbmousedown;
      kids[i].onmouseup = tbmouseup;
      if (document.all) kids[i].onclick = IEclick;
      else kids[i].onclick = tbclick;
    }
    if (kids[i].className == "smilebutton") {
      kids[i].onmouseover = tbmouseover;
      kids[i].onmouseout = tbmouseout;
      kids[i].onmousedown = tbmousedown;
      kids[i].onmouseup = tbmouseup;
      if (document.all) kids[i].onclick = IEclick;
      else kids[i].onclick = tbclick;
    }
  }
}

function tbmousedown(e)
{
  if (!e) var e = window.event;
  this.firstChild.style.left = 2;
  this.firstChild.style.top = 2;
  this.style.border="inset 2px";
  if (e.preventDefault) e.preventDefault();
  else {
    e.cancelBubble = true;
    e.returnValue = false;
    document.getElementById("edit").focus();
  }
}

function tbmouseup()
{
  this.firstChild.style.left = 1;
  this.firstChild.style.top = 1;
  this.style.border="outset 2px";
}

function tbmouseout()
{
  this.style.border="solid 2px #C0C0C0";
}

function tbmouseover()
{
  this.style.border="outset 2px";
}

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 IEclick()
{
  if (this.id == "bold") {
    document.all('edit').focus();
    document.execCommand('Bold');
  }
  if (this.id == "italic") {
    document.all('edit').focus();
    document.execCommand('Italic');
  }
  if (this.id == "underline") {
    document.all('edit').focus();
    document.execCommand('Underline');
  }
   if (this.id == "subscript") {
    document.all('edit').focus();
    document.execCommand('Subscript');
  }
  if (this.id == "superscript") {
    document.all('edit').focus();
    document.execCommand('Superscript');
  }
  if (this.id == "insertorderedlist") {
    document.all('edit').focus();
    document.execCommand('InsertOrderedList');
  }
  if (this.id == "insertunorderedlist") {
    document.all('edit').focus();
    document.execCommand('InsertUnorderedList');
  }
  if (this.id == "createlink") {
    document.all('edit').focus();
    var szURL = prompt("Enter a URL:", "http://");
    if ((szURL != null) && (szURL != "")) document.execCommand('CreateLink', false, szURL);
  }
  if (this.id == "createmail") {
    document.all('edit').focus();
    var szURL = prompt("Enter a URL:", "mailto:");
    if ((szURL != null) && (szURL != "")) document.execCommand('CreateLink', false, szURL);
  }
  if (this.id == "undo") {
    document.all('edit').focus();
    document.execCommand('Undo');
  }
  if (this.id == "redo") {
    document.all('edit').focus();
    document.execCommand('Redo');
  }
   if (this.id == "e-smile") {
    document.all('edit').focus();
    document.getElementById('edit').document.selection.createRange().pasteHTML('<IMG SRC=\'RTE_files/emoticon-smile.gif\'>');
  }
   if (this.id == "e-laugh") {
    document.all('edit').focus();
    document.getElementById('edit').document.selection.createRange().pasteHTML('<IMG SRC=\'RTE_files/emoticon-laugh.gif\'>');
  }
   if (this.id == "e-sad") {
    document.all('edit').focus();
    document.getElementById('edit').document.selection.createRange().pasteHTML('<IMG SRC=\'RTE_files/emoticon-sad.gif\'>');
  }
   if (this.id == "e-surprised") {
    document.all('edit').focus();
    document.getElementById('edit').document.selection.createRange().pasteHTML('<IMG SRC=\'RTE_files/emoticon-surprised.gif\'>');
  }
   if (this.id == "e-unsure") {
    document.all('edit').focus();
    document.getElementById('edit').document.selection.createRange().pasteHTML('<IMG SRC=\'RTE_files/emoticon-unsure.gif\'>');
  }
   if (this.id == "e-wink") {
    document.all('edit').focus();
    document.getElementById('edit').document.selection.createRange().pasteHTML('<IMG SRC=\'RTE_files/emoticon-wink.gif\'>');
  }
   if (this.id == "e-tongue-in-cheek") {
    document.all('edit').focus();
    document.getElementById('edit').document.selection.createRange().pasteHTML('<IMG SRC=\'RTE_files/emoticon-tongue-in-cheek.gif\'>');
  }
   if (this.id == "e-h") {
    document.all('edit').focus();
    document.getElementById('edit').document.selection.createRange().pasteHTML('<IMG SRC=\'RTE_files/emoticon-h.gif\'>');
  }
   if (this.id == "e-y") {
    document.all('edit').focus();
    document.getElementById('edit').document.selection.createRange().pasteHTML('<IMG SRC=\'RTE_files/emoticon-y.gif\'>');
  }
   if (this.id == "e-n") {
    document.all('edit').focus();
    document.getElementById('edit').document.selection.createRange().pasteHTML('<IMG SRC=\'RTE_files/emoticon-n.gif\'>');
  }
}

function tbclick()
{
  if (this.id == "e-smile") {
    document.getElementById('edit').contentWindow.document.execCommand('InsertImage', false, prefix + 'RTE_files/emoticon-smile.gif');
  }
  else if (this.id == "e-laugh") {
    document.getElementById('edit').contentWindow.document.execCommand('InsertImage', false, prefix + 'RTE_files/emoticon-laugh.gif');
  }
  else if (this.id == "e-sad") {
    document.getElementById('edit').contentWindow.document.execCommand('InsertImage', false, prefix + 'RTE_files/emoticon-sad.gif');
  }
  else if (this.id == "e-surprised") {
    document.getElementById('edit').contentWindow.document.execCommand('InsertImage', false, prefix + 'RTE_files/emoticon-surprised.gif');
  }
  else if (this.id == "e-unsure") {
    document.getElementById('edit').contentWindow.document.execCommand('InsertImage', false, prefix + 'RTE_files/emoticon-unsure.gif');
  }
  else if (this.id == "e-wink") {
    document.getElementById('edit').contentWindow.document.execCommand('InsertImage', false, prefix + 'RTE_files/emoticon-wink.gif');
  }
  else if (this.id == "e-tongue-in-cheek") {
    document.getElementById('edit').contentWindow.document.execCommand('InsertImage', false, prefix + 'RTE_files/emoticon-tongue-in-cheek.gif');
  }
  else if (this.id == "e-h") {
    document.getElementById('edit').contentWindow.document.execCommand('InsertImage', false, prefix + 'RTE_files/emoticon-h.gif');
  }
  else if (this.id == "e-y") {
    document.getElementById('edit').contentWindow.document.execCommand('InsertImage', false, prefix + 'RTE_files/emoticon-y.gif');
  }
  else if (this.id == "e-n") {
    document.getElementById('edit').contentWindow.document.execCommand('InsertImage', false, prefix + 'RTE_files/emoticon-n.gif');
  }
  else if (this.id == "subscript") {
    document.getElementById('edit').contentWindow.document.execCommand('subscript', false, null);
  }
  else if (this.id == "superscript") {
    document.getElementById('edit').contentWindow.document.execCommand('superscript', false, null);
  }
  else if (this.id == "createlink") {
    var szURL = prompt("Enter a URL:", "http://");
    if ((szURL != null) && (szURL != "")) {
      document.getElementById('edit').contentWindow.document.execCommand("CreateLink", false, szURL);
    }
  }
  else if (this.id == "createmail") {
    var szURL = prompt("Enter a URL:", "mailto:");
    if ((szURL != null) && (szURL != "")) {
      document.getElementById('edit').contentWindow.document.execCommand("CreateLink", false, szURL);
    }
  }
  else {
    document.getElementById('edit').contentWindow.document.execCommand(this.id, false, null);
  }
}

function Select(selectname)
{
  var cursel = document.getElementById(selectname).selectedIndex;
  /* First one is always a label */
  if (cursel != 0) {
    var selected = document.getElementById(selectname).options[cursel].value;
    document.getElementById('edit').contentWindow.document.execCommand(selectname, false, selected);
    document.getElementById(selectname).selectedIndex = 0;
  }
  document.getElementById("edit").contentWindow.focus();
  document.getElementById("edit").focus();
}

function Start() {
  if (document.getElementById('edit')) {
    if (!document.all) {
      document.getElementById('edit').contentWindow.document.designMode = "on";
      try {
        document.getElementById('edit').contentWindow.document.execCommand("undo", false, null);
      }  catch (e) {
        alert("This program is not supported on your level of Mozilla.");
      }
    }
    InitToolbarButtons();
  }
}

function make_html(){
  var html = document.getElementById('clanek');
  if (document.all) {
    html.value = frames['edit'].document.body.innerHTML;
  }
  else {
    html.value = document.getElementById('edit').contentWindow.document.body.innerHTML;
  }
}

function make_iframe(){
  var html = document.getElementById('edit');
  if (document.getElementById('edit')) {
  if (document.all) {
    html.contentWindow.innerHTML = document.getElementById('clanek').value;
  }
  else {
    html.contentWindow.document.body.innerHTML = document.getElementById('clanek').value;
  }
  }
}
