prohlizec = navigator.userAgent.toLowerCase(); // zjištění názvu prohlížeče
// prohlížeče na jádře "Gecko" které nejsou podporované
gecko = ((prohlizec.indexOf("gecko") != -1) && (prohlizec.indexOf("spoofer") == -1) && (prohlizec.indexOf("khtml") == -1) && (prohlizec.indexOf("netscape/7.0") == -1));

// doplní řetězce před a za označený text
function formatuj(textarea, tagOpen, tagClose, text) {
	textarea = document.getElementById(textarea);
	bylaMezera = 0;
	
	// pro IE a jiné prohlížeče, které podporují "document.selection"
	if (document.selection && !gecko) {
		vyber = document.selection.createRange().text;
		if (!vyber) vyber = text;
		textarea.focus();
		
		// ošetření mezer na začátku a na konci vybraného textu
		if (vyber.charAt(vyber.length - 1) == " ") {
			vyber = vyber.substring(0, vyber.length - 1);
			bylaMezera = 1;
		}
		
		if (vyber.charAt(0) == " ") {
			vyber = vyber.substring(1, vyber.length);
			
			if (bylaMezera == 1) bylaMezera = 3;
			else bylaMezera = 2;
		}
		
		if (bylaMezera == 1) document.selection.createRange().text = tagOpen + vyber + tagClose + " ";
		else if (bylaMezera == 2) document.selection.createRange().text = " " + tagOpen + vyber + tagClose;
		else if (bylaMezera == 3) document.selection.createRange().text = " " + tagOpen + vyber + tagClose + " ";
		else document.selection.createRange().text = tagOpen + vyber + tagClose;
	}
	
	// pro prohlížeče postavené na jádře Gecko
	else if (textarea.selectionStart || textarea.selectionStart == "0") {
		zmeneno = false;
		startPos = textarea.selectionStart;
		endPos = textarea.selectionEnd;
		if (endPos - startPos) zmeneno = true;
		scrollTop = textarea.scrollTop;
		formatovanyText = (textarea.value).substring(startPos, endPos);
		if (!formatovanyText) formatovanyText = text;
		
		// ošetření mezer na začátku a na konci vybraného textu
		if (formatovanyText.charAt(formatovanyText.length - 1) == " ") bylaMezera = 1;
		if (formatovanyText.charAt(0) == " ") {
			if (bylaMezera == 1) bylaMezera = 3;
			else bylaMezera = 2;
		}
	
		if (bylaMezera == 1) subst = tagOpen + formatovanyText.substring(0, (formatovanyText.length - 1)) + tagClose + " ";
		else if (bylaMezera == 2) subst = " " + tagOpen + formatovanyText.substring(1, formatovanyText.length) + tagClose;
		else if (bylaMezera == 3) subst = " " + tagOpen + formatovanyText.substring(1, formatovanyText.length - 1) + tagClose + " ";
		else subst = tagOpen + formatovanyText + tagClose;
		
		textarea.value = textarea.value.substring(0, startPos) + subst + textarea.value.substring(endPos, textarea.value.length);
		textarea.focus();
		
		if (zmeneno) {
			cPos = startPos + (tagOpen.length + formatovanyText.length + tagClose.length);
			textarea.selectionStart = cPos;
			textarea.selectionEnd = cPos;
		}
		
		else {
			textarea.selectionStart = startPos + tagOpen.length;
			textarea.selectionEnd = startPos + tagOpen.length + formatovanyText.length;
		}
		
		textarea.scrollTop = scrollTop;
	}
	
	if (textarea.createTextRange) textarea.caretPos = document.selection.createRange().duplicate();
}

function vlozSmajla(textarea, text) {
	textarea = document.getElementById(textarea);
	
	// pro IE a jiné prohlížeče, které podporují "document.selection"
	if (document.selection && !gecko) {
		textarea.focus();
		vyber = document.selection.createRange();
		vyber.text = text;
	}
	
	// pro prohlížeče postavené na jádře Gecko
	else if (textarea.selectionStart || textarea.selectionStart == 0) {
		startPos = textarea.selectionStart;
		endPos = textarea.selectionEnd;
		textarea.value = textarea.value.substring(0, startPos) + text + textarea.value.substring(endPos, textarea.value.length);
	}
	
	else textarea.value += text;
}

function escapeQuotes(textQuotes) {
	if (!textQuotes) textQuotes = '';
	regular = new RegExp("'", "g");
	textQuotes = textQuotes.replace(regular, "\\'");
	regular = new RegExp("\\n", "g");
	textQuotes = textQuotes.replace(regular, "\\n");
	return escapeQuotesHTML(textQuotes);
}

function escapeQuotesHTML(textHTML) {
	if (!textHTML) textHTML = '';
	regular = new RegExp('&', "g");
	textHTML = textHTML.replace(regular, "&amp;");
	regular = new RegExp('"', "g");
	textHTML = textHTML.replace(regular, "&quot;");
	regular = new RegExp('<', "g");
	textHTML = textHTML.replace(regular, "&lt;");
	regular = new RegExp('>', "g");
	textHTML = textHTML.replace(regular, "&gt;");
	return textHTML;
}

// vytvoří tlačítko
function tlacitko(textarea, cesta, obrazek, popis, tagOpen, tagClose, text) {
	if (!document.selection && !gecko) return false;
	
	// ošetření proměnných od nebezpečných znaků
	obrazek = escapeQuotesHTML(obrazek);
	tagOpen = escapeQuotes(tagOpen);
	tagClose = escapeQuotes(tagClose);
	text = escapeQuotes(text);
	popis = escapeQuotesHTML(popis);
	
	document.write("<a href=\"javascript:formatuj");
	document.write("('" + textarea + "', '" + tagOpen + "', '" + tagClose + "', '" + text + "');\">");
	document.write("<img width=\"24\" height=\"24\" src=\"" + cesta + obrazek + "\" alt=\"" + popis + "\" title=\"" + popis + "\" class=\"img-no-border\" />");
	document.write("</a>\n");
	return;
}

// vypíše tlačítka
function vypisTlacitka(textarea, cesta, typ) {
	if (typ == 1) {
		tlacitko(textarea, cesta, "strong.gif", "Tučné", "**", "**", "Tučné");
		tlacitko(textarea, cesta, "em.gif", "Kurzíva", "*","*","Kurzíva");
		tlacitko(textarea, cesta, "ins.gif","Podtrhnuté",  "++",  "++",  "Podtrhnuté");
		tlacitko(textarea, cesta, "del.gif","Preškrtnuté",  "--",  "--",  "Preškrtnuté");
		tlacitko(textarea, cesta, "sup.gif", "Horní index", "^^",  "^^",  "Horní index");
		tlacitko(textarea, cesta, "sub.gif",  "Dolní index", "__",  "__",  "Dolní index");
		tlacitko(textarea, cesta, "a.gif", "Hypertextový odkaz", "\"",  "\":[http://www.odkaz.cz/]", "text odkazu");
		tlacitko(textarea, cesta, "h1.gif", "Nadpis první úrovně", "\n### ", " ###\n","Nadpis první úrovně");
		tlacitko(textarea, cesta, "h2.gif", "Nadpis druhé úrovně", "\n== ",  "\n","Nadpis druhé úrovně");
		tlacitko(textarea, cesta, "img.gif", "Obrázek", "[* ", " *]", "http://www.odkaz.cz/obrazek.jpg");
		tlacitko(textarea, cesta, "hr.gif",  "Horizontální čára", "\n* * * * * *", "\n", "");
		tlacitko(textarea, cesta, "code.gif", "Zdrojový kód", "\n/---code\n", "\n\\\\---\n", "Zdrojový kód");
		tlacitko(textarea, cesta, "ul.gif", "Nečíslovaný seznam", "\n* ", "\n* \n* \n",  "");
		tlacitko(textarea, cesta, "ol.gif", "Číslovaný seznam","\n1) ","\n2) \n3) \n", "");
	}
	
	else if (typ == 2) {
		tlacitko(textarea, cesta, "strong.gif", "Tučné", "**", "**", "Tučné");
		tlacitko(textarea, cesta, "em.gif", "Kurzíva", "*","*","Kurzíva");
		tlacitko(textarea, cesta, "a.gif", "Hypertextový odkaz", "\"",  "\":[http://www.odkaz.cz/]", "text odkazu");
		tlacitko(textarea, cesta, "img.gif", "Obrázek", "[* ", " *]", "http://www.odkaz.cz/obrazek.jpg");
		tlacitko(textarea, cesta, "code.gif", "Zdrojový kód", "\n/---code\n", "\n\\\\---\n", "Zdrojový kód");
	}
}

// vytvoří smajlíka
function smajl(textarea, cesta, obrazek, text) {
	if (!document.selection && !gecko) return false;
	
	// ošetření proměnných od nebezpečných znaků
	obrazek = escapeQuotesHTML(obrazek);
	text = escapeQuotesHTML(text);
	
	document.write("<a href=\"javascript:vlozSmajla");
	document.write("('" + textarea + "', '" + text + "');\">");
	document.write("<img width=\"15\" height=\"15\" src=\"" + cesta + obrazek + "\" alt=\"" + text + "\" title=\"Vložit smajla &quot;" + text + "&quot;\" class=\"img-no-border\" />");
	document.write("</a>\n");
	return;
}

// vypíše smajlíky
function vypisSmajly(textarea, cesta) {
	smajl(textarea, cesta, "smile.gif", ":-)");
	smajl(textarea, cesta, "sad.gif", ":-(");
	smajl(textarea, cesta, "wink.gif", ";-)");
	smajl(textarea, cesta, "biggrin.gif", ":-D");
	smajl(textarea, cesta, "eek.gif", "8-O");
	smajl(textarea, cesta, "cool.gif", "8-)");
	smajl(textarea, cesta, "confused.gif", ":-?");
	smajl(textarea, cesta, "mad.gif", ":-x");
	smajl(textarea, cesta, "razz.gif", ":-P");
	smajl(textarea, cesta, "neutral.gif", ":-|");
}

// otevře odkaz do nového okna
function popUp(cesta) {
	byloOtevreno = false;
	okno = window.open(cesta);    
	return (typeof(okno) == 'object') ? true : false;
}