(function($) {
   $.fn.nicer = function() {
      function oznackovat(textarea_name, tagOpen, tagClose, sampleText) {
         var txtarea = document.getElementById(textarea_name);

         if(tagOpen == '\"') {
            var url = prompt('Vlozte prosim adresu odkazu','http://');
            if(url != null)
               tagClose = "\":["+url+"]";
         }

         var was_space = 0;
            // pre IE a ine prehliadace, ktore podporuju "document.selection"
         if (document.selection && !is_gecko) {
            var theSelection = document.selection.createRange().text;
               if (!theSelection)
                  theSelection=sampleText;
            txtarea.focus();
            // osetrenie medzier na zaciatku a konci vybraneho textu
            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;

         // pre prehliadace postavene na jadre Gecko (Mozilla, Firefox)
         }
         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;
            // osetrenie medzier na zaciatku a konci vybraneho textu
            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();
      }

      function trim(str) {
         return str.replace(/^\s*|\s*$/g,"").replace(/\s+/," ");
      }

      return this.each(function() {
         obj = $(this);
         obj.siblings('.textareapanel')
            .append('<a href="#" class="ui-button" style="font-weight: bold" data-button="bold">tučný</a>'
                  + '<a href="#" class="ui-button" style="font-style: italic" data-button="italic">šikmý</a>'
                  + '<a href="#" class="ui-button" style="text-decoration: line-through" data-button="strike">prečiarknutý</a>'
                  + '<span class="mhm"></span>'
                  + '<a href="#" class="ui-button" data-button="cite">"citát"</a>'
                  + '<a href="#" class="ui-button" text-decoration: underline" data-button="anchor">odkaz</a>'
                  + '<span class="mhm"></span>'
                  + '<a href="#" class="ui-button" data-button="pre">pre</a>'
                  + '<a href="#" class="ui-button" data-button="code">kód</a>');

         $('.textareapanel a').live('click', function() {
            if($(this).data('button') == "bold")
               oznackovat('msg','<strong>','</strong>','text');
            else if($(this).data('button') == "italic")
               oznackovat('msg','<i>','</i>','text');
            else if($(this).data('button') == "strike")
               oznackovat('msg','<s>','</s>','text');
            else if($(this).data('button') == "cite")
               oznackovat('msg','<cite>','</cite>','citat');
            else if($(this).data('button') == "anchor")
               oznackovat('msg','<a href="','"></a>','odkaz');
            else if($(this).data('button') == "pre")
               oznackovat('msg','<pre>','</pre>','kod');
            else if($(this).data('button') == "code")
               oznackovat('msg','<code>','</code>','kod');
            return false;
         });
      });
   }
})(jQuery);
