function smajl(item){
		 var i=1;
		 var ele;
		 while (ele=document.getElementById('smajl'+i)) {
		 	 if (i==item)
		 	  ele.style.display='block';
   	   else
   	  	ele.style.display='none';
		   i++;
		}
}

function vlozEditor(iddiv, idtext, jmeno, cesta, sirka, vyska)
{

	var text = document.getElementById(idtext).value;	
	var div = document.getElementById(iddiv);
	var textik = editor(jmeno, cesta, sirka, vyska, text);
	div.innerHTML = textik;
}

function editor(jmeno, cesta, sirka, vyska, value){
  
  /*var re=/\<br[ ]?[\/]?\>/gi;
  var value = value.replace(re, "\r\n"); */
 
  var jmeno = jmeno;
  var cesta = cesta;
  var vyska = vyska; 
  var text = "";
  
  h_textarea = vyska - 12;
  h_div = vyska + 20;
   
  if(sirka < 540){
   sirka = 490;
  }
  w_textarea = sirka;
  w_div = sirka + 6;
  
  document.write("<style type=\"text/css\"><!--");
  document.write("div.wisiwyg div.buttons {text-align: left !important;    margin: 0 !important; }");
  document.write("div.wisiwyg div.buttons ul { list-style: none !important;  margin: 0 !important;  padding: 0 !important; }");
  document.write("div.wisiwyg div.buttons ul li { float: left; margin: 0 !important; }");
  document.write("div.wisiwyg div.buttons ul img {  background-color: #EAEAEA;  float: left !important; background-image: url('"+cesta+"vzhled/butt.gif');   border: 1px solid;   border-color: #D2D2D2 #999999 #AAAAAA #D2D2D2;   cursor: pointer;   cursor: hand;   margin-left: 3px;   margin-bottom: 3px;   padding: 0;   vertical-align: middle; float: left;}");
  document.write("div.wisiwyg div.buttons ul img:hover {  background-color: #EAEAEA; float: left !important;   background-image: url('"+cesta+"vzhled/over.gif');   border: 1px solid;   border-color: #D2D2D2 #999999 #AAAAAA #D2D2D2;   cursor: pointer;   cursor: hand;   margin-left: 3px;   margin-bottom: 3px;   padding: 0;   vertical-align: middle;   behavior: url("+cesta+"vzhled/hover.htc)}");
  document.write("dl {background: #f6f6f6;  border: 1px solid #c3c3c3;  margin: 2px 0 0 0;  display: none;  padding: 3px 3px 3px 3px;   position: absolute;   z-index: 100;   width: 150px;  text-align: left; }");
  document.write("dl img {background: none !important; border: none !important; }");
  document.write(" //--></style>");  
  text = text+"<div class=\"wisiwyg\" style=\" text-align:left;  width: "+w_div+"px; border: 1px solid #CFCFCF; background-color: #EFEFEF; height: "+h_div+"px;\">";
  text = text+"<textarea id=\""+jmeno+"\" name=\""+jmeno+"\" onmouseover='javascript:smajl(0);' style=\"width:"+w_textarea+"px; padding: 3px; border: 0px solid #CFCFCF; border-bottom: 1px solid #CFCFCF; height: "+h_textarea+"px; overflow: auto; background-color: white;\"  wrap=\"off\" >"+value+"</textarea>";
  text = text+"<div class=\"buttons\" style=\"width: 100%; height: 32px; _height: 22px; margin:0; text-align: left;\"><ul>";
  text = text+"<li><a href=\"javascript:oznackovat('"+jmeno+"', '**', '**', 'Tučný text')\" title=\"Tučně\"><img src=\"/"+cesta+"vzhled/bold.gif\" onmouseover='javascript:smajl(0);' alt=\"\" border=\"0\" height=\"20\" width=\"20\"></a></li><li><a href=\"javascript:oznackovat('"+jmeno+"', '*', '*', 'Kurzívý text')\" title=\"Kurzívou\"><img src=\"/"+cesta+"vzhled/italic.gif\" onmouseover='javascript:smajl(0);' alt=\"\" border=\"0\" height=\"20\" width=\"20\"></a></li><li><a href=\"javascript:oznackovat('"+jmeno+"', '++', '++', 'Podtrhnutý text')\" title=\"Potrhnutě\"><img src=\"/"+cesta+"vzhled/underline.gif\" onmouseover='javascript:smajl(0);' alt=\"\" border=\"0\" height=\"20\" width=\"20\"></a></li><li>&nbsp;</li>";
  text = text+"<li><a href=\"javascript:oznackovat('"+jmeno+"', '\\n- \\n- \\n- ', '', '')\" title=\"Nečíslovaný seznam\"><img src=\"/"+cesta+"vzhled/od.gif\" onmouseover='javascript:smajl(0);' alt=\"\"  border=\"0\" height=\"20\" width=\"20\"></a></li><li><a href=\"javascript:oznackovat('"+jmeno+"', '\\n1) \\n2) \\n3) ', '', '')\" title=\"Číslovaný seznam\"><img src=\"/"+cesta+"vzhled/cis.gif\" onmouseover='javascript:smajl(0);' alt=\"\" border=\"0\" height=\"20\" width=\"20\"></a></li><li>&nbsp;</li>";
  text = text+"<li><a href=\"javascript:oznackovat('"+jmeno+"', '\\n.<\\n', '\\n', 'Text zarovnaný vlevo')\" title=\"Vlevo\"><img src=\"/"+cesta+"vzhled/left.gif\" onmouseover='javascript:smajl(0);' alt=\"\" border=\"0\" height=\"20\" width=\"20\"></a></li><li><a href=\"javascript:oznackovat('"+jmeno+"', '\\n.<>\\n', '\\n', 'Text zarovnaný na střed')\" title=\"Na střed\"><img src=\"/"+cesta+"vzhled/center.gif\" onmouseover='javascript:smajl(0);' alt=\"\" border=\"0\" height=\"20\" width=\"20\"></a></li><li><a href=\"javascript:oznackovat('"+jmeno+"', '\\n.>\\n', '\\n', 'Text zarovnaný vpravo')\" title=\"Vpravo\"><img src=\"/"+cesta+"vzhled/right.gif\" onmouseover='javascript:smajl(0);' alt=\"\"  border=\"0\" height=\"20\" width=\"20\"></a></li><li><a href=\"javascript:oznackovat('"+jmeno+"', '\\n.=\\n', '\\n', 'Text zarovnaný do bloku')\" title=\"Do bloku\"><img src=\"/"+cesta+"vzhled/just.gif\" onmouseover='javascript:smajl(0);' alt=\"\" border=\"0\" height=\"20\" width=\"20\"></a></li><li>&nbsp;</li>";
  text = text+"<li><a href=\"javascript:oznackovat('"+jmeno+"', '&quot;', '&quot;:[http://www.aaa.xx]', '')\" title=\"Odkaz\"><img src=\"/"+cesta+"vzhled/link.gif\" onmouseover='javascript:smajl(0);' alt=\"\"  border=\"0\" height=\"20\" width=\"20\"></a></li>";
 text = text+"</dl>";
  text = text+"</li>";
  text = text+"</ul>";
  text = text+"</div>";
  text = text+"</div>";
  return text;
 }
 
/*
* Pomocne funkce, upravi text. Převzaty od Metju.
*/
 
function escapeQuotesHTML(text2) {
	if(!text2) { text2 = ''; }
	var re = new RegExp('&',"g");
	text2 = text2.replace(re,"&amp;");
	var re = new RegExp('"',"g");
	text2 = text2.replace(re,"&quot;");
	var re = new RegExp('<',"g");
	text2 = text2.replace(re,"&lt;");
	var re = new RegExp('>',"g");
	text2 = text2.replace(re,"&gt;");
	return text2;
}

function escapeQuotes(text) {
	if(!text) {	text = ''; }
	var re = new RegExp("'","g");
	text = text.replace(re,"\\'");
	re = new RegExp("\\n","g");
	text = text.replace(re,"\\n");
	return escapeQuotesHTML(text);
}


/*
* Samotna funkce na vkladani syntaxe. Převzata od Metju a upravena.
*/
var clientPC = navigator.userAgent.toLowerCase();
var is_gecko = ((clientPC.indexOf('gecko')!=-1) && (clientPC.indexOf('spoofer')==-1) && (clientPC.indexOf('khtml') == -1) && (clientPC.indexOf('netscape/7.0')==-1));


function oznackovat(textarea_name, tagOpen, tagClose, sampleText) {
  
  var txtarea = document.getElementById(textarea_name);
	var was_space = 0;
	
	/*Pokud chceme vlozit URL tak se zeptame na adresu a text.*/ 
	if(tagOpen == '\"')
		{
		var url = prompt('Vložte adresu odkazu','http://');
		var text = prompt('Vložte text odkazu');
		if(url != null)
		tagClose = text+"\":["+url+"]";
		}
	
 	if (document.selection && !is_gecko) {
 	
		var theSelection = document.selection.createRange().text;
		if (!theSelection)
		 theSelection=sampleText;
		 txtarea.focus();
		 
		if (theSelection.charAt(theSelection.length - 1) == " ") {
			theSelection = theSelection.substring(0, theSelection.length - 1);
			was_space = 1;
		}
    
    if (theSelection.charAt(0) == " ") {
			theSelection = theSelection.substring(1, theSelection.length);
      if (was_space == 1) was_space = 3;
      else was_space = 2;
		}
    
    if (was_space == 1) document.selection.createRange().text = tagOpen + theSelection + tagClose + " ";
    else if (was_space == 2) document.selection.createRange().text = " " + tagOpen + theSelection + tagClose;
    else if (was_space == 3) document.selection.createRange().text = " " + tagOpen + theSelection + tagClose + " ";
    else document.selection.createRange().text = tagOpen + theSelection + tagClose;
    
	} else if (txtarea.selectionStart || txtarea.selectionStart == '0') {
	
  	var replaced = false;
		var startPos = txtarea.selectionStart;
		var endPos = txtarea.selectionEnd;
	
  	if (endPos-startPos)
			replaced = true;

		var scrollTop = txtarea.scrollTop;
		var myText = (txtarea.value).substring(startPos, endPos);

	  if (!myText)
			myText=sampleText;
   
    if (myText.charAt(myText.length - 1) == " ") {
      was_space = 1;
			}
  
    if (myText.charAt(0) == " ") {
      if (was_space == 1) was_space = 3;
      else was_space = 2;
			}
   
    if (was_space == 1) subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + " ";
    else if (was_space == 2) subst = " " + tagOpen + myText.substring(1, myText.length) + tagClose;
    else if (was_space == 3) subst = " " + tagOpen + myText.substring(1, myText.length - 1) + tagClose + " ";
    else subst = tagOpen + myText + tagClose;
		txtarea.value = txtarea.value.substring(0, startPos) + subst +
			txtarea.value.substring(endPos, txtarea.value.length);
		txtarea.focus();

		if (replaced) {
			var cPos = startPos+(tagOpen.length+myText.length+tagClose.length);
			txtarea.selectionStart = cPos;
			txtarea.selectionEnd = cPos;
		} else {
			txtarea.selectionStart = startPos+tagOpen.length;
			txtarea.selectionEnd = startPos+tagOpen.length+myText.length;
		}
		
		txtarea.scrollTop = scrollTop;
 }

	if (txtarea.createTextRange)
		txtarea.caretPos = document.selection.createRange().duplicate();
}