// 2010.11.13

function addEvent(elem, evType, fn)
{
  if (elem.addEventListener)
  {
    elem.addEventListener(evType, fn, false);
    return fn;
  }

  var iefn = function() { fn.call(elem); }
  elem.attachEvent('on' + evType, iefn);
  return iefn;
}

function bindReady(handler)
{
  var called = false;

  function ready()
  {
    if (called) return;
    called = true;
    handler();
  }

  if (document.addEventListener)
  {
    document.addEventListener( "DOMContentLoaded", function()
    {
      ready();
    }, false );
  }
  else if (document.attachEvent)
  {
    if (document.documentElement.doScroll && window == window.top)
    {
      function tryScroll()
      {
        if (called) return;
        if (!document.body) return;
        try
        {
          document.documentElement.doScroll("left");
          ready();
        }
        catch(e)
        {
          setTimeout(tryScroll, 0);
        }
      }
      tryScroll();
    }

    document.attachEvent("onreadystatechange", function()
    {
      if ( document.readyState === "complete" )
        ready();
    });
  }

  if (window.addEventListener)
    window.addEventListener('load', ready, false);
  else if (window.attachEvent)
    window.attachEvent('onload', ready);
//  else
//    window.onload=ready;
}

function getSelectionText()
{
  var s = '';
  if (document.getSelection)
    s = document.getSelection();
  else if (document.selection)
    s = document.selection.createRange().text;
  else if (window.getSelection)
    s = window.getSelection().toString(); // Opera?
  return s;
}

function insertText(s)
{
  var e = document.getElementById('tamsg');
  if ('selectionStart' in e)
  {
    e.value = e.value.substring(0, e.selection.end) + s +
      e.value.substring(e.selection.end);
    e.selection.end += s.length;
    e.selection.start = e.selection.end;
    e.selectionStart = e.selection.start;
    e.selectionEnd = e.selection.end;
    e.focus();
  }
  else if (document.selection)
  {
    e.selection.range.text += s;
    e.selection.range.select();
  }
  else
    e.value += s;
  return false;
}

function insertTag(b, a)
{
  var e = document.getElementById('tamsg');
  if ('selectionStart' in e)
  {
    e.value = e.value.substring(0, e.selection.start) + b +
      e.value.substring(e.selection.start, e.selection.end) + a +
      e.value.substring(e.selection.end);
//    e.selection.start += b.length;
    e.selection.end += b.length + a.length;
    e.selectionStart = e.selection.start;
    e.selectionEnd = e.selection.end;
    e.focus();
  }
  else if (document.selection)
  {
    var l = e.selection.range.text.length;
    var t = e.selection.range.text;
    var r = 0;
    if (navigator.platform.indexOf('Win32') != -1) // \r на windows
      for (var i = 0; i < l; i++)
        if (t.charCodeAt(i) == 10)
          r++;
    e.selection.range.text = b + t + a;
//    e.selection.range.moveEnd("character", -a.length);
    e.selection.range.moveStart("character", -l + r - a.length - b.length);
    e.selection.range.select();
  }
  else
    e.value += b + a;
  return false;
}

bindReady(function()
{
  // Привязка здесь
  var ta = document.getElementById('tamsg');
  if (ta)
  {
    ta.selection = {};
    var fn = function() // saver
    {
      if ('selectionStart' in this)
  //    if (typeof(this.selectionStart) != "undefined")
      {
        this.selection.start = this.selectionStart;
        this.selection.end = this.selectionEnd;
      }
      else if (document.selection)
        this.selection.range = document.selection.createRange().duplicate();
    };

    fn.call(ta); // init
    if (document.selection) // fix initial selection in IE
      ta.selection.range = ta.createTextRange().duplicate();

    var list = ['change', 'click', 'keyup', 'focus', 'mouseup'];
    for (i in list)
      addEvent(ta, list[i], fn);
    addEvent(ta, 'keyup', sz);
  }
});

