(function($)
{
   $.fn.tagToolbar = function()
   {
      return this.each(function()
      {
         obj = $(this);
         tarea = $(this).text();
         obj.load('/tag_toolbar/toolbar.html');
         var tagy = $('ul li a', obj);

         function insertTags(tagOpen, tagClose, sampleText)
         {
            if( typeof $j != 'undefined' && typeof $j.fn.textSelection != 'undefined' && ( currentFocused.nodeName.toLowerCase() == 'iframe' || currentFocused.id == 'wpTextbox1' ) )
            {
               $j('#wpTextbox1').textSelection('encapsulateSelection', { 'pre': tagOpen, 'peri': sampleText, 'post': tagClose });
               return;
            }

            var txtarea = document.getElementById(tarea);
            var selText, isSample = false;

            // IE/Opera
            if(document.selection  && document.selection.createRange)
            {
               //save window scroll position
               if (document.documentElement && document.documentElement.scrollTop)
                  var winScroll = document.documentElement.scrollTop
               else if (document.body)
                  var winScroll = document.body.scrollTop;
               //get current selection
               txtarea.focus();
               var range = document.selection.createRange();
               selText = range.text;
               //insert tags
               checkSelectedText();
               range.text = tagOpen + selText + tagClose;
               //mark sample text as selected
               if (isSample && range.moveStart)
               {
                  if (window.opera)
                     tagClose = tagClose.replace(/\n/g,'');
                  range.moveStart('character', - tagClose.length - selText.length);
                  range.moveEnd('character', - tagClose.length);
               }
               range.select();
               //restore window scroll position
               if (document.documentElement && document.documentElement.scrollTop)
                  document.documentElement.scrollTop = winScroll
               else if (document.body)
                  document.body.scrollTop = winScroll;

            }
            // Mozilla
            else if(txtarea.selectionStart || txtarea.selectionStart == '0')
            {
               //save textarea scroll position
               var textScroll = txtarea.scrollTop;
               //get current selection
               txtarea.focus();
               var startPos = txtarea.selectionStart;
               var endPos = txtarea.selectionEnd;
               selText = txtarea.value.substring(startPos, endPos);
               //insert tags
               checkSelectedText();
               txtarea.value = txtarea.value.substring(0, startPos)
                  + tagOpen + selText + tagClose
                  + txtarea.value.substring(endPos, txtarea.value.length);
               //set new selection
               if (isSample)
               {
                  txtarea.selectionStart = startPos + tagOpen.length;
                  txtarea.selectionEnd = startPos + tagOpen.length + selText.length;
               }
               else
               {
                  txtarea.selectionStart = startPos + tagOpen.length + selText.length + tagClose.length;
                  txtarea.selectionEnd = txtarea.selectionStart;
               }
               //restore textarea scroll position
               txtarea.scrollTop = textScroll;
            }

            function checkSelectedText()
            {
               if (!selText)
               {
                  selText = sampleText;
                  isSample = true;
               }
               //exclude ending space char
               else if (selText.charAt(selText.length - 1) == ' ')
               {
                  selText = selText.substring(0, selText.length - 1);
                  tagClose += ' ';
               }
            }
         }

         tagy.live("click", function()
         {
            tag = $(this).attr("class");
            if(tag == "text_bold") insertTags('<strong>', '</strong>', 'tučný text');
            else if(tag == "text_italic") insertTags('<i>', '</i>', 'šikmý text');
            else if(tag == "text_underline") insertTags('<u>', '</u>', 'podčiarknutý text');
            else if(tag == "text_paragraph") insertTags('<p>', '</p>', 'odstavec');
            else if(tag == "text_heading_2") insertTags('<h2>', '</h2>', 'nadpis druhej úrovne');
            else if(tag == "text_heading_3") insertTags('<h3>', '</h3>', 'nadpis tretej úrovne');
            else if(tag == "text_heading_4") insertTags('<h4>', '</h4>', 'nadpis štvrtej úrovne');
            else if(tag == "text_heading_5") insertTags('<h5>', '</h5>', 'nadpis piatej úrovne');
            else if(tag == "text_heading_6") insertTags('<h6>', '</h6>', 'nadpis šiestej úrovne');
            else if(tag == "text_list_bullets") insertTags('<ul><li>', '</li></ul>', 'zoznam');
            else if(tag == "text_strikethrough") insertTags('<s>', '</s>', 'preškrtnutý text');
            else if(tag == "text_blockquote") insertTags('<blockquote>', '</blockquote>', 'citát');
            else if(tag == "text_subscript") insertTags('<sub>', '</sub>', 'sub');
            else if(tag == "text_superscript") insertTags('<sup>', '</sup>', 'sup');
            else if(tag == "text_code") insertTags('<code>', '</code>', 'code');
            else if(tag == "text_newline") insertTags('<br />', '', '');
            else if(tag == "script_code") insertTags('<pre class="brush: js">', '</pre>', 'skript');
            else if(tag == "image") insertTags('<image src="" alt="" />', '', '');
            else if(tag == "link") insertTags('<a href="">', '</a>', 'odkaz');
            return false;
         });
      });
   }
})(jQuery);
