function pause(numberMillis) {
    var now = new Date();
    var exitTime = now.getTime() + numberMillis;
    while (true) {
        now = new Date();
        if (now.getTime() > exitTime)
            return;
    }
}

var is_IE = ('\v' == 'v');

function get_selection_bounds(editor_window){
   var range, root, start, end

   if(editor_window.getSelection){ // Gecko, Opera
      var selection = editor_window.getSelection()
     
      range = selection.getRangeAt(0)

      start = range.startContainer
      end = range.endContainer
      root = range.commonAncestorContainer

      if(start.nodeName.toLowerCase() == "body") return null
      
      if(start.nodeName == "#text") start = start.parentNode
      if(end.nodeName == "#text") end = end.parentNode

      if(start == end) root = start

      return {
         root: root,
         start: start,
         end: end
      }

   } else if(editor_window.document.selection){ // MSIE
      range = editor_window.document.selection.createRange()
      if(!range.duplicate) return null

      

      var r1 = range.duplicate()
      var r2 = range.duplicate()
      r1.collapse(true)
      r2.moveToElementText(r1.parentElement())
    
      start = r1.parentElement()

      r1 = range.duplicate()
      r2 = range.duplicate()
      r2.collapse(false)
      r1.moveToElementText(r2.parentElement())
      
      end = r2.parentElement()

      root = range.parentElement()
      if(start == end) root = start

      return {
         root: root,
         start: start,
         end: end
      }
   }
   return null 
}




function bodyOnLoad(e)
{
    if (!e)    e = window.event;
    var body = document.getElementsByTagName('body');
    body[0].onmouseup = showMenu;

    if ('\v' == 'v')
    {
        document.getElementById('ctcblink').style.display = 'inline';
    }

}

var __selectedText = '';

function getSelectionText(e)
{

    var t = 

((window.getSelection&&window.getSelection())||(document.getSelection&&document.getSelection()

)||(document.selection&&document.selection.createRange&&document.selection.createRange().text)

);

    if (!t) return '';

    bounds = null;
    try {bounds = get_selection_bounds(window);} catch(e){}

    if (!bounds) // FF и выделаем в TEXTAREA
    {

    }
    else
    {
        if (bounds.root.tagName == 'TEXTAREA') // IE в TEXTAREA
        {         }
        else // Текст на странице
        {
            __selectedText = t;
            return t;
        }
    }

    return '';

}

window.onload = bodyOnLoad;
var contextMenuTable = null;



function getPageScroll(){

    var yScroll;

    if (self.pageYOffset)
    {
        yScroll = self.pageYOffset;
        xScroll = self.pageXOffset;

    }
    else if (document.documentElement && document.documentElement.scrollTop)
    {     // Explorer 6 Strict
      

  yScroll = document.documentElement.scrollTop;
        xScroll = document.documentElement.scrollLeft;
    }
    else if (document.body)
    {    // all other Explorers
        yScroll = document.body.scrollTop;
        xScroll = document.body.scrollLeft;
    }

    arrayPageScroll = new Array(xScroll,yScroll)
    return arrayPageScroll;
}

function showMenu(event)
{
    if (!contextMenuTable) contextMenuTable = document.getElementById('contextMenuTable');

    if (getSelectionText() == '')
    {
        hideMenu();
        return false;
    }

    if (!event) event = window.event;

    if (contextMenuTable.style.display == 'none')
    {
        if (is_IE) document.selection.empty();

        contextMenuTable.style.left        = event.clientX - 5 + 

getPageScroll()[0] + 'px';
        contextMenuTable.style.top        = event.clientY - 5 + 

getPageScroll()[1] + 'px';
        contextMenuTable.style.display  = 'block';

        return false;
    }
}

function hideMenu()
{
    contextMenuTable.style.display = 'none';
}


function putSelection(mode)
{

    text = getSelectionText();
    if (!text && !__selectedText)
    {
        alert('Вы не вошли в систему');
        return false;
    }

    if (!text && is_IE && __selectedText)
        text = __selectedText;



    ta = document.getElementById('message_text');
    if (ta)
    {
        if (mode == 'code')
        {
            putSelectionCode(ta, text);
        }
        else
        {
            putSelectionText(ta, text);
        }
    }
    else
    {
        alert('Вы не вошли в систему');
    }

    hideMenu();
}

function putSelectionCode(ta, text)
{
    ta.value = ta.value + '[quote]' + text + '[/quote]' + '\r\n';
}

function putSelectionText(ta, text)
{
    ta.value = ta.value + '\r\n' + text;
}

function copyToCB()
{
    text = getSelectionText();

    if (!text && !__selectedText)
    {
        alert('Не выбран текст для копирования');
        return false;
    }
    if (!text && is_IE && __selectedText) text = __selectedText;

    return copyToClipboard(text);
}

function copyToClipboard(content)
{
    if (window.clipboardData && window.clipboardData.setData)
    {
        window.clipboardData.setData("Text", content);
        return true;
    }
    else
    {
        alert('Ваш браузер не поддерживает работу с буфером обмена.');
        return false;
    }
}
