/**
 * Vloží do textarea pořado 
 * 
 * @param   string    Jméno tagu 
 * @param   bool      Je tag párový? Výchozi true
 * @param   string    Obsah tagu, výchozí hodnotou je vybraný text
 * @param   string    Text vkládaný před tag. Výchozí null
 * @param   string    Text vkládaný za tag. Výchozí null
 */
function insertTag(tag, parovy, text, prependText, appendText, zavorky)
{
	//inicializace proměných
	// var textarea = document.forms["postMsg"].elements["poslText"];
	var textarea = document.getElementsByTagName("textarea")[0];


	if(typeof zavorky == 'undefined')
	{
		var startTag = '[' + tag + ']';
		var endTag = '[/' + tag + ']';
	}
	else
	{
		var startTag = tag;
		var endTag = tag;
	}

	

	if (typeof text == 'undefined') {
		var tagText = getTextareaSelection();
	} else {
		var tagText = text;
	}
	
	//alert("asd");
	
	if (typeof parovy == 'undefined') {
		var parovy = true;
	}

	if (typeof prependText == 'undefined') {
		var prependText = '';
	}
	
	if (typeof appendText == 'undefined') {
		var appendText = '';
	}
	
	
	
	
	//přepsání výchozích hodnot pro specifické tagy
	if (tag == 'url') {
		if (tagText == '') {
			tagText = 'Odkaz';
		}
		var regexp = new RegExp("^((http|https|ftp)\\://)([a-zA-Z0-9\\.\\-]+(\\:[a-zA-Z0-9\\.&%\\$\\-]+)*@)?((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|([a-zA-Z0-9\\-]+\\.)*[a-zA-Z0-9\\-]+\\.[a-zA-Z]{2,4})(\\:[0-9]+)?(/[^/][a-zA-Z0-9\\.\\,\\?\\'\\\\/\\+&%\\$#\\=~_\\-@]*)*$");
		var regexp2 = new RegExp("^([a-zA-Z0-9\\.\\-]+(\\:[a-zA-Z0-9\\.&%\\$\\-]+)*@)?((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|www\\.([a-zA-Z0-9\\-]+\\.)*[a-zA-Z0-9\\-]+\\.[a-zA-Z]{2,4})(\\:[0-9]+)?(/[^/][a-zA-Z0-9\\.\\,\\?\\'\\\\/\\+&%\\$#\\=~_\\-@]*)*$");
		
		if (regexp.test(tagText)) {
			startTag = '[url=' + tagText + ']';
		} else if (regexp2.test(tagText)) {
			startTag = '[url=http://' + tagText + ']';
		} else {
			startTag = '[url=http://example.com]';
			selectionStart = prependText.length + 5;
			selectionEnd = prependText.length + 23;
		}
	}
	
	if( tag == 'color')
	{
		startTag = '[color=barva]';
		selectionStart = prependText.length + 7;
		selectionEnd = prependText.length + 12;
	}
		
	//vytvoření textu pro vložení
	if (!parovy) {
		endTag = '';
	}
		
	var text2insert = prependText + startTag + tagText + endTag + appendText;
	
	//vložení:
	textarea.focus();
	
	if (typeof textarea.selectionStart != 'undefined') {
		var start = textarea.selectionStart;
		var end = textarea.selectionEnd;
		
		textarea.value = textarea.value.substr(0, start) + text2insert + textarea.value.substr(end);
		
		//nastaveni kurzoru na spravné misto / vybrani pozadovaneho textu
		if (typeof selectionStart != 'undefined' && typeof selectionEnd != 'undefined') {
			textarea.selectionStart = start + selectionStart;
			textarea.selectionEnd = start + selectionEnd;
		} else {
			if (tagText == '') {
				//skoceni mezi tagy
				textarea.selectionStart = start + prependText.length + startTag.length;
				textarea.selectionEnd = start + prependText.length + startTag.length;
			} else {
				//skoceni za posledni tag
				textarea.selectionStart = start + text2insert.length;
				textarea.selectionEnd = start + text2insert.length;
			}
		}
	}
	else if (typeof document.selection != 'undefined') {
		var range = document.selection.createRange();
		range.text = text2insert;
		
		//nastaveni kurzoru na spravné misto / vybrani pozadovaneho textu
		if (typeof selectionStart != 'undefined' && typeof selectionEnd != 'undefined') {
			range.moveStart('character', -text2insert.length + selectionStart);
			range.moveEnd('character', -text2insert.length + selectionEnd);
		} else {
			if (tagText == '') {
				//skoceni mezi tagy
				range.move('character', -(endTag.length + appendText.length));
			} else {
				//skoceni za posledni tag
				//protoze prohlizec v tomto pripade skoci za posledni tag automaticky, neni treba nic posouvat	
			}
		}
		range.select();
	}

	//unset promenych
	delete selectionStart;
	delete selectionEnd;
}


function insertQuote(el)
{
	var text = getDocumentSelection();
	if (text == '') {
		text = el.parentNode.nextSibling.nextSibling.innerText || el.parentNode.nextSibling.nextSibling.textContent;
		text = text.split("\n");
		
		for (var i = 0; i < text.length; i++) {
			text[i] = text[i].trim();	// kvůli IE
			if (text[i] == '') {
				continue;
			}
			insertTag('i', true, text[i], '„', "“\r\n");			
		}
	} else {
		insertTag('i', true, text, '„', "“\r\n");
	}
}

function getTextareaSelection()
{
	// var textarea = document.forms["postMsg"].elements["poslText"];
	var textarea = document.getElementsByTagName("textarea")[0];
	
	if (typeof textarea.selectionStart != 'undefined') {
		var selection = textarea.value.substring(textarea.selectionStart, textarea.selectionEnd);
	} else if (typeof document.selection != 'undefined') {
		var selection = document.selection.createRange().text;
	} else {
		//alert('Bug!');
	}
	return selection;
}

function getDocumentSelection()
{
	// var textarea = document.forms["postMsg"].elements["poslText"];
	var textarea = document.getElementsByTagName("textarea")[0];
	
	if (document.getSelection) selection = document.getSelection();
	else if(document.selection) selection = document.selection.createRange().text;
	else if(window.getSelection) selection = window.getSelection();
	else selection = '';
	return selection;
}

function getQuotation()
{
	document.writeln('<a href="#" onclick="insertQuote(this); return false;">Citovat</a>');
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}