disKeyTrap = false;
document.onkeydown = CtrlLeftRightHandler;

function CtrlLeftRightHandler(evt)
 {
  if (!evt) evt = window.event;

  if (evt.ctrlKey && !disKeyTrap)
   {
    switch(evt.keyCode)
     {
      case 37:
       if (a = $('previous_page')) location.href = a.href;
       break;
      case 39:
       if (a = $('next_page')) location.href = a.href;
       break;
     }
   }
 }

function ReloadCaptcha(id, url)
 {
  $(id).src = url + '&' + Math.random();

  return false;
 }

function Helper(id)
 {
  width = 400;
  height = 350;

  var left = Math.round((document.body.clientWidth - width) / 2);
  var top = Math.round((document.body.clientHeight - height) / 2);

  var win = window.open(absbaseurl + 'helper.html?id=' + id,
                        'printhelper', 
                        'toolbar=no,' +
                        'scrollbars=yes,' +
                        'status=no,' +
                        'height=' + height + ',' +
                        'width=' + width + ',' + 
                        'top=' + top + ',' +
                        'left=' + left);

  win.focus();

  return false;
 }

function PostModerated(r)
 {
  r = r.responseText;

  if (r == '<!-- CLEAN -->')
   if (c = $('moderation_cleanup'))
    c.innerHTML = '';
 }

function AcceptPost(feedID, postID, full_body, tags_prefix)
 {
  params = 'feedID=' + feedID + '&' + 
           'postID=' + postID + '&' + 
           'mode=accept&' + 
           'digest=' + digest + '&' +
           'tags_prefix=' + encodeURIComponent(tags_prefix) + '&' +
           'full_body=' + full_body;

  new Ajax.Updater('post' + postID,
                   '/ajax/post_moderation.html?' + Math.random(),
                   {asynchronous: true,
                    method: 'post',
                    onSuccess: PostModerated,
                    parameters: params });

  return false;
 }

function DeclinePost(feedID, postID, full_body, tags_prefix)
 {
  params = 'feedID=' + feedID + '&' + 
           'postID=' + postID + '&' + 
           'mode=decline&' + 
           'digest=' + digest + '&' +
           'tags_prefix=' + encodeURIComponent(tags_prefix) + '&' +
           'full_body=' + full_body;

  new Ajax.Updater('post' + postID,
                   '/ajax/post_moderation.html?' + Math.random(),
                   {asynchronous: true,
                    method: 'post',
                    onSuccess: PostModerated,
                    parameters: params });

  return false;
 }

function SubmitPollVote(cb, postID, variantID, full_body, tags_prefix)
 {
  params = 'variantID=' + variantID + '&' + 
           'postID=' + postID + '&' + 
           'digest=' + digest + '&' +
           'tags_prefix=' + encodeURIComponent(tags_prefix) + '&' +
           'full_body=' + full_body;

  new Ajax.Updater('post' + postID,
                   '/ajax/poll_vote.html?' + Math.random(),
                   {asynchronous: true,
                    method: 'post',
                    parameters: params });

  cb.disabled = true;

  return false;
 }

/* textarea's shit support routines */

function taTwin(id, b, open, close)
 {
  var ta = $(id);
  ta.focus();

  if (document.selection)
   {
    theSelection = document.selection.createRange().text;

    if (theSelection) 
     {
      ta.focus();

      document.selection.createRange().text = open + theSelection + close;

      theSelection = '';

      return false;
     }
   }
  else
   {
    ta.value += open + close;

    return false;
   }

  if (b.tagged = !b.tagged)
   document.selection ? document.selection.createRange().text += open : ta.value += open;
  else
   document.selection ? document.selection.createRange().text += close : ta.value += close;

  return false;
 }

function taPut(ta, s)
 {
  ta.focus();

  document.selection ? document.selection.createRange().text += s : ta.value += s;

  return false;
 }

function taBold(ta, b) { return taTwin(ta, b, '<b>', '</b>'); }
function taItalic(ta, b) { return taTwin(ta, b, '<i>', '</i>'); }
function taUnderline(ta, b) { return taTwin(ta, b, '<u>', '</u>'); }
function taStrike(ta, b) { return taTwin(ta, b, '<s>', '</s>'); }
function taCut(ta, b) { return taPut($(ta), '<cut>'); }
function taHelp(kind) { Helper(kind); return false; }

function taURL(ta)
 {
  var ta = $(ta);

  var FoundErrors = '';

  var enterURL = prompt("Введите адрес ссылки", "http://");

  if (!enterURL) 
   {
    FoundErrors += "\nВы не ввели адрес ссылки.";
   }
  else
   {
    var enterTITLE = prompt("Введите текст ссылки", "Текст ссылки");

    if (!enterTITLE) 
     FoundErrors += "\nВы не ввели текст ссылки.";
   }
 
  if (FoundErrors)
   alert("Ошибка!" + FoundErrors);
  else
   taPut(ta, '<a href="' + enterURL + '">' + enterTITLE + '</a>');

  return false;
 }

/* LiveJournal, respect :) */

function SwapNodes(orig, to_swap)
 {
  var orig_pn = xParent(orig, true);
  var next_sibling = orig.nextSibling;
  var to_swap_pn = xParent(to_swap, true);

  if (!to_swap_pn) return false;
  if (!orig_pn) return false;
  if (!to_swap) return false;

  if ((to_swap_pn.replaceChild) && (orig_pn.insertBefore))
   {
    to_swap_pn.replaceChild(orig, to_swap);
    orig_pn.insertBefore(to_swap, next_sibling);
   }

  return true;
 }

// xParent / xDef (c) 2004 Michael Foster

function xParent(e, bNode)
 {
  if (!(e = $(e))) return null;
  var p = null;

  if (!bNode && xDef(e.offsetParent)) p = e.offsetParent;
  else if (xDef(e.parentNode)) p = e.parentNode;
  else if (xDef(e.parentElement)) p = e.parentElement;
  return p;
 }

function xDef() 
 {
  for (var i = 0; i < arguments.length; ++i)
   {
    if (typeof(arguments[i]) == 'undefined') 
     return false;
   }

  return true;
 }

/* comments */

function LockCommentControls()
 {
  if (s = $('s1')) s.disabled = true;
  if (s = $('s2')) s.disabled = true;
  if (s = $('s3')) s.disabled = true;
 }

function UnlockCommentControls()
 {
  if (s = $('s1')) s.disabled = false;
  if (s = $('s2')) s.disabled = false;
  if (s = $('s3')) s.disabled = false;
 }

function PreviewCommentLoaded(r)
 {
  UnlockCommentControls();
 }

function PreviewComment()
 {
  LockCommentControls();

  new Ajax.Updater('CommentPreview',
                   '/ajax/comment_preview.html?' + Math.random(),
                   {asynchronous: true, 
                    parameters: 'digest=' + digest + '&' + Form.serialize($('EditorForm')), 
                    onSuccess: PreviewCommentLoaded });
 }

/* our megacommenting engine :) produced by LJ & Kolbaskin :) */

var prevkapusta = 'abcc';
var prev1 = 0;
var prev2 = 0;
var submitreplycommentID = 0;

function FocusCommenter()
 {
  if (ta = $('ta'))
   ta.focus();
 }

function DelayedFocusCommenter()
 {
  setTimeout('FocusCommenter();', 100);
 }

function ReplyComment(ownerid, skipfocus)
 {
  var atarget = 'qr' + ownerid;
  var target = 'qra' + ownerid;

  if (!(t = $(target))) return;
  if (!(a = $(atarget))) return;
  if (!(c = $('CommentFormContainer'))) return;

  if ((prev1) && (prev2))
   SwapNodes(prev1, prev2);

  SwapNodes(c, t);

  prev1 = c;
  prev2 = t;

  if (prevkapusta)
   if (p = $(prevkapusta))
    p.style.display = 'inline';

  a.style.display = 'none';
  c.style.display = 'block';

  prevkapusta = atarget;

  if (o = $('replycommentID'))
   o.value = ownerid;

  if (!skipfocus)
   setTimeout('FocusCommenter();', 100);

  return false;
 }

function SubmitCommentDoneWorker()
 {
  if ($('new_comment'))
   {
    ReplyComment(0, 1);
    Element.scrollTo('new_comment');
//    $('CommentFormContainer').style.display = 'none';
   }
  else
   {
    ReplyComment(submitreplycommentID);
   }
 }

function SubmitCommentDone(r)
 {
  setTimeout('SubmitCommentDoneWorker()', 50);
 }

function SubmitComment()
 {
  submitreplycommentID = $('replycommentID').value;
  submitreplycommentID = submitreplycommentID ? submitreplycommentID : 0;

  LockCommentControls();

  new Ajax.Updater('comments_data',
                   '/ajax/comment_submit.html?' + Math.random(),
                   {asynchronous: true, 
                    method: 'post',
                    onSuccess: SubmitCommentDone,
                    parameters: 'digest=' + digest + '&' + Form.serialize($('EditorForm')) });

  return false;
 }

function DeleteComment(commentID)
 {
  if (confirm('Удалить комментарий?'))
   {
    LockCommentControls();

    new Ajax.Updater('comments_data',
                     '/ajax/comment_delete.html?' + Math.random(),
                     {asynchronous: true, 
                      method: 'post',
                      onSuccess: SubmitCommentDone,
                      parameters: 'digest=' + digest + '&commentID=' + commentID + '&' + Form.serialize($('EditorForm')) });
   }

  return false;
 }

var muccomment = false;

function MoveUpComment(commentID)
 {
  var s = $('c' + commentID);

  if (muccomment)
   muccomment.className = muccomment._className;

  s._className = s.className;
  s.className = 'inner parent_comment';

  Element.scrollTo(s);

  muccomment = s;

  return false;
 }

function PicturesLoader(id)
 {
  width = 600;
  height = 220;

  var left = Math.round((document.body.clientWidth - width) / 2);
  var top = Math.round((document.body.clientHeight - height) / 2);

  var win = window.open('/misc/pictures_addict.html?ta=' + id, 
                        '', 
                        'toolbar=no,' +
                        'scrollbars=yes,' + 
                        'status=no,' +
                        'height=' + height + ',' + 
                        'width=' + width + ',' + 
                        'top=' + top + ',' + 
                        'left=' + left);

  win.focus();

  return false;
 }
