/* HTML nástrojová lišta

Programoval:
   Radovan Moser - ARTIS
   internetové služby
   http://www.artis.cz
   moser@artis.cz
   
Všechna práva vyhrazena!
Jakékoliv kopírování částí tohoto a všech souvisejících souboru
není dovoleno bez vědomí a souhlasu jejich tvůrce!
*/

function storeCaret(editEl) {
   if (editEl.createTextRange)
      editEl.currRange = document.selection.createRange().duplicate();
}

function replace_selection(elemnt, action) {
   if (!elemnt) return false;
   if (!action) return false;
   var el = objGet(elemnt);
   var selection = getSel(el);
   if (!selection && ((action == "b") || (action == "i") || (action == "u") || (action == "center") || (action == "right") || (action == "justify"))) {
      var selection = prompt("Vložte text, který bude uzavřen do zvoleného tagu", "");
      if (selection == null)
         return;
   }
   switch(action) {
      case 'br': var txt = '<br>'; break;
      case 'b': var txt = '<b>'+ selection +'</b>'; break;
      case 'i': var txt = '<i>'+ selection +'</i>'; break;
      case 'u': var txt = '<u>'+ selection +'</u>'; break;
      case 'center': var txt = '<p align="center">'+ selection +'</p>'; break;
      case 'right': var txt = '<p align="right">'+ selection +'</p>'; break;
      case 'justify': var txt = '<p align="justify">'+ selection +'</p>'; break;
      case 'ul': txt = get_list('ul'); break;
      case 'ol': txt = get_list('ol'); break;
      case 'a_url': txt = get_href('url', selection); break;
      case 'a_mail': txt = get_href('mail', selection); break;
      default: var txt = action; break;
   }
/* pridavani mezery pred tag
   if (txt)
      if (el.value && (action != "br"))
         txt = ' ' + txt;
*/

/*
   var startPos = el.value.indexOf(selection);
   if (startPos!= 0) {
      var endPos = el.value.indexOf(selection) + selection.length;
      begin = el.value.substr(0,startPos);
      end = el.value.substr(endPos, el.value.length);
      el.value = begin + txt + end;
   } else {
      el.value = el.value + txt;
   }
*/

   if (selection.length > 0) {
      if (navigator.userAgent.toLowerCase().indexOf("firefox") > 0) {
         // firefox
         if (el.selectionStart!= undefined) {
            begin = el.value.substr(0, el.selectionStart);
            end = el.value.substr(el.selectionEnd);
         }
         el.value = begin + txt + end;
      } else {
         // ostatni
         document.selection.createRange().text = txt;
      }
   } else {
      el.value = el.value + txt;
   }

   el.focus();
   return true;
}

function get_list(typ) {
   if (!typ)
      var typ = "ul";
   var pocet = prompt("Kolik položek bude seznam obahovat? (max 10)", 2);
   if (pocet > 0) {
      if (pocet > 10)
         var pocet = 10;
      var polozky = new Array();
      for (i=1; i <= pocet; i++) {
         polozky[i] = prompt("Zadejte "+ i +". položku (z "+ pocet +") seznamu", "");
      }
   } else
      var polozky = new Array();
   var txt = "";
   for (i=1; i < polozky.length; i++)
      if (polozky[i] != null)
         var txt = txt + ' <li>'+ polozky[i] +'</li>\n';
   if (txt)
      var txt = '<'+ typ +'>\n'+ txt + '</'+ typ +'>';
   return txt;
}

function get_href(typ, content) {
   if (!typ)
      var typ = "url";
   switch(typ) {
      case "url":
         if (!content)
            var content = prompt('Zadejte text odkazu (např.: "Seznam - katalog odkazů").', '');
         if (content == null)
            var content = "{odkaz}";
         var href = prompt('Kam bude odkaz směřovat? (např.: http://www.seznam.cz).', 'http://');
         if (!href || (href == "http://"))
            var href = "#";
//         return '<a href="'+ href +'" target="_blank">'+ content +'</a>';
         return '<a href="'+ href +'" target="_self">'+ content +'</a>';
      break;
      case "mail":
         var href = prompt('Zadejte E-mailovou adresu (např.: muj@email.cz).', '@');
         if (!content)
            var content = prompt('Zadejte text odkazu (např.: "Aleš Novák").', '');
         if (content == null)
            var content = "{odkaz}";
         if (!href || (href == "@"))
            var href = "#";
         else
            var href = "mailto:" + href;
         return '<a href="'+ href +'">'+ content +'</a>';
      break;
   }
}
