var dumb = function() { };

/* (c) livejournal.com */

function xParent(e,bNode){
  if (!(e=xGetElementById(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 xParent2(e){
  var p=null;
  if (xDef(e.parentNode)) p=e.parentNode;
  else if (xDef(e.parentElement)) p=e.parentElement;
  return p;
}

function xGetElementById(e) {
  if(typeof(e)!='string') return e;
  if(document.getElementById) e=document.getElementById(e);
  else if(document.all) e=document.all[e];
  else e=null;
  return e;
}

function xDef() {
  for(var i=0; i<arguments.length; ++i){if(typeof(arguments[i])=='undefined') return false;}
  return true;
}

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;

  to_swap_pn.replaceChild(orig, to_swap);
  orig_pn.insertBefore(to_swap, next_sibling);

  return true;
 }

/* (c) design.ru */

function InputPlaceholder (input, value, cssFilled, cssEmpty)
{
  var thisCopy = this
  
  this.Input = input
  this.Value = value
  this.SaveOriginal = (input.value == value)
  this.CssFilled = cssFilled
  this.CssEmpty = cssEmpty

  this.setupEvent (this.Input, 'focus', function() {return thisCopy.onFocus()})
  this.setupEvent (this.Input, 'blur',  function() {return thisCopy.onBlur()})
  this.setupEvent (this.Input, 'keydown', function() {return thisCopy.onKeyDown()})

  if (input.value == '') this.onBlur();

  return this
}

InputPlaceholder.prototype.setupEvent = function (elem, eventType, handler)
{
  if (elem.attachEvent)
  {
    elem.attachEvent ('on' + eventType, handler)
  }

  if (elem.addEventListener)
  {
    elem.addEventListener (eventType, handler, false)
  }
}

InputPlaceholder.prototype.onFocus = function()
{
  if (!this.SaveOriginal &&  this.Input.value == this.Value)
  {
    this.Input.value = ''
  }
  else
  {
//      this.Input.className = ''
  }
}

InputPlaceholder.prototype.onKeyDown = function()
{
//  this.Input.className = ''
}

InputPlaceholder.prototype.onBlur = function()
{
  if (this.Input.value == '' || this.Input.value == this.Value)
  {
    this.Input.value = this.Value
//    this.Input.className = this.CssEmpty
  }
  else
  {
//    this.Input.className = this.CssFilled
  }
}

/* (c) faq20.com :) */

var qaskbody_prev = false;
var qaskbody_hold = false;
var qaskbody_now = false;
var qaskbody_timein = false;
var qaskbody_timeout = false;
var qaskbody_nowspinning = false;
var qeditorbody_prev = false;
var qeditorbody_prev_container = false;
var qeditorbody_prev_container_color = false;
var qeditor_now = false;

//function debug(s)
// {
//  $('debugger').innerHTML = s + '<br>' + $('debugger').innerHTML;
// }

function qindo(q)
 {
//  debug('qindo ' + q);

  q = $(q);

  if (q.id == qaskbody_now) return false;

  if (qaskbody_prev != false)
   SwapNodes(qaskbody_prev, $('qaskbody'));

  SwapNodes($('qaskbody'), qaskbody_prev = $(q.id + 'cd'));

  qaskbody_now = q.id;

  qaskbody_timein = false;
  qaskbody_nowspinning = false;
 }

function qin(q)
 {
  qaskbody_nowspinning = q.id;

//  debug('qin ' + q.id);

  qaskbody_timein = setTimeout('qindo(\'' + q.id + '\');', 50);
 }

function qoutdo(q)
 {
  if (q == qaskbody_nowspinning)
   {
//    debug('qoutdo ' + q);

    q = $(q);

    if (qaskbody_timein)
     {
      clearTimeout(qaskbody_timein);
      qaskbody_timein = false;
     }

    if (qaskbody_prev != false)
     {
      SwapNodes(qaskbody_prev, $('qaskbody'));

      qaskbody_prev = false;
      qaskbody_now = false;
     }
   }

  qaskbody_timeout = false;
 }

function qout(q)
 {
  qaskbody_nowspinning = q.id;

//  debug('qout ' + q.id);

  qaskbody_timeout = setTimeout('qoutdo(\'' + q.id + '\');', 50);
 }

function qinask(q)
 {
//  debug('qinask ' + q.id);

  if (qaskbody_timeout)
   {
    clearTimeout(qaskbody_timeout);
    qaskbody_timeout = false;
   }
 }

function qoutask(q)
 {
//  debug('qoutask ' + q.id);

  if (qaskbody_timeout)
   {
    clearTimeout(qaskbody_timeout);
    qaskbody_timeout = false;
   }

  setTimeout('qoutask2(\'' + q.id + '\')', 10);
 }

function qoutask2(q)
 {
//  debug('qoutask2');

  qoutdo(q);

  if (qaskbody_timeout)
   {
    clearTimeout(qaskbody_timeout);
    qaskbody_timeout = false;
   }
 }

function qstartedit(l)
 {
  if (qaskbody_prev != false)
   {
    SwapNodes(qaskbody_prev, $('qaskbody'));
    qaskbody_prev = false;
   }

  if (qeditorbody_prev != false)
   {
    SwapNodes(qeditorbody_prev, $('qeditorbody'));
    qeditorbody_prev = false;

    $(qeditorbody_prev_container).style.background = qeditorbody_prev_container_color;
   }

  SwapNodes($('qeditorbody'), qeditorbody_prev = $(qaskbody_now + 'cd'));

  qeditorbody_prev_container = qaskbody_now;
  qeditorbody_prev_container_color = $(qaskbody_now).style.background;
  qeditor_now = qaskbody_now;

  setTimeout('qfocus();', 250);

//  $(qaskbody_now).style.background = "#E7E6D2";
  
  return false;
 }

function qfocus()
 {
  $('qemail').focus();
 }

function qsend(q)
 {
//  alert(qaskbody_now);

  if (qaskbody_prev != false)
   {
    SwapNodes(qaskbody_prev, $('qaskbody'));
    qaskbody_prev = false;
   }

  if (qeditorbody_prev != false)
   {
    SwapNodes(qeditorbody_prev, $('qeditorbody'));
    qeditorbody_prev = false;

    $(qeditorbody_prev_container).style.background = qeditorbody_prev_container_color;
   }

  $(qeditor_now).style.background = "#E7E6D2";
  $(qeditor_now).onmouseover = dumb;
  $(qeditor_now).onmouseout = dumb;

  $(qeditor_now + 'c').innerHTML = $('qsending').innerHTML;

  params = 'email=' + encodeURIComponent($('qemail').value) + '&' +
           'body=' + encodeURIComponent($('qcontent').value) + '&' +
           'no=' + qeditor_now;

  $('qcontent').value = '';

  new Ajax.Updater(qeditor_now + 'c',
                   '/' + current_lang + '/' + current_kind + '/add.html',
                   {asynchronous: true, 
                    onSuccess: dumb,
                    method: 'post',
                    parameters: params });

  return false;
 }

function SendReply(questionID)
 {
  params = 'reply=' + encodeURIComponent($('reply').value);

  $('reply').value = '';
  $('replybutton').disabled = true;

  new Ajax.Updater('thebody',
                   '/' + current_lang + '/' + current_kind + '/' + questionID + '.html',
                   {asynchronous: true, 
                    onSuccess: dumb,
                    method: 'post',
                    parameters: params });

  return false;
 }

function SubmitVote(questionID, hash, vote)
 {
  params = 'questionID=' + questionID + '&' + 
           'hash=' + hash + '&' +
           'vote=' + vote;

  new Ajax.Updater('qvote' + questionID,
                   '/vote_question.html',
                   {asynchronous: true, 
                    onSuccess: dumb,
                    method: 'post',
                    parameters: params });

  return false;
 }

function SubmitVoteReply(replyID, hash, vote)
 {
  params = 'replyID=' + replyID + '&' + 
           'hash=' + hash + '&' +
           'vote=' + vote;

  new Ajax.Updater('rvote' + replyID,
                   '/vote_reply.html',
                   {asynchronous: true, 
                    onSuccess: dumb,
                    method: 'post',
                    parameters: params });

  return false;
 }

function ShowReply(id)
 {
  $('hr' + id).style.display = 'inline';
  $('hra' + id).style.display = 'none';

  return false;
 }

function GiveMeTheCooooode()
 {
  $('codeholder').style.display = 'block';
  $('codeopener').style.display = 'none';

  return false;
 }
