var currentPopup = null;
var counterTID = null;
var counterInput = null;
var counterLimit = null;
var counterDisp = null;
var animatPivot = null;
var animatForm = null;

function showPic(obj) {
  var span = obj.parentNode.childNodes.item(0);
  var clsbtn = span.childNodes.item(0);
  var img = span.childNodes.item(2);
  if (currentPopup) currentPopup.style.display = 'none';
  span.style.display = 'block';
  span.style.margin = '-100px 0 0 -' + (Math.floor(30 + img.width / 2)) + 'px';
  clsbtn.onclick = closePic;
  img.src = obj.href;
  currentPopup = span;
  return false;
}

function closePic() {
  if (currentPopup) currentPopup.style.display = 'none';
  return false;
}

function onloadInit() {
  if (document.getElementById('copyright')) {
    var obj = document.getElementById('emailbox');
    if (obj) {
      obj.onfocus = emailOnFocus;
      obj.onblur = emailOnBlur;
      obj.onblur();
    }
    steadyViewInit();
    setTimeout('hideVTips()', 30000);
  } else {
    setTimeout('onloadInit()', 200);
  }
}

function emailOnFocus() {
  if (this.value == 'email') this.value = '';
}

function emailOnBlur() {
  if (!this.value) this.value = 'email';
}

function hideVTips(obj) {
  if (obj) {
    if (obj.className == 'vtip') {
      obj.style.display = 'none';
    } else {
      var ch = obj.childNodes;
      for (var i=0; i<ch.length; i++) hideVTips(ch.item(i));
    }
  } else {
    var root = document.getElementById('authbox');
    if (root) hideVTips(root);
  }
}

function showComTab(obj, id) {
  while (obj.className != 'ctab') {
    if (!(obj = obj.parentNode)) return false;
  }
  obj.style.display = 'none';
  if (obj = obj.parentNode) {
    if (obj.childNodes.length > id) {
      obj.childNodes.item(id).style.display = 'block';
    }
  }
  return false;
}

function setTextCounter(obj, limit) {
  if (obj && limit) {
    clearTimeout(counterTID);
    counterInput = null;
    counterLimit = null;
    counterDisp = null;
    if (obj.form) {
      for (var i=0; i<obj.form.elements.length; i++) {
        var j = obj.form.elements.item(i);
        if (j.className == 'count') {
          counterInput = obj;
          counterLimit = limit;
          counterDisp = j;
          break;
        }
      }
    }
  }
  if (counterInput && counterDisp) {
    var d = counterLimit - counterInput.value.length;
    if (d < 0) {
      counterDisp.value = 'too many words';
      counterDisp.style.color = '#a00';
    } else {
      counterDisp.value = d + ' left';
      counterDisp.style.color = '#777';
    }
    counterTID = setTimeout('setTextCounter()', 500);
  }
}

function showComForm(obj) {
  if (obj) {
    while (obj.className != 'formpvt') {
      if (!(obj = obj.parentNode)) return false;
    }
    animatPivot = obj;
    animatForm = obj.lastChild;
    if (currentPopup) currentPopup.style.display = 'none';
    animatForm.style.display = 'block';
    currentPopup = animatForm;
    setTimeout('showComForm()', 200);
  } else if (animatPivot && animatForm) {
    var d = animatForm.clientHeight - animatPivot.clientHeight;
    if (d > 0) {
      if (d < 2) {
        animatPivot.style.height = animatForm.clientHeight + 'px';
      } else {
        animatPivot.style.height =
          (Math.ceil(d/4) + animatPivot.clientHeight) + 'px';
        setTimeout('showComForm()', 30);
      }
    }
  }
  return false;
}

function steadyViewInit() {
  if (document.links) for (var i=0; i<document.links.length; i++) {
    var a = document.links.item(i);
    if (!a.onclick && (typeof(a.href) == 'string') && (a.href.indexOf('/view.php?') >= 0)) {
      a.onclick = steadyViewLinkHook;
    }
  }
  if (document.forms) for (var i=0; i<document.forms.length; i++) {
    var f = document.forms.item(i);
    if (!f.onsubmit && (typeof(f.action) == 'string') && (f.action.indexOf('/view.php') >= 0)) {
      f.onsubmit = steadyViewFormHook;
    }
  }
}

function steadyViewLinkHook() {
  var sx = window.pageXOffset || document.body.parentNode.scrollLeft || 0;
  var sy = window.pageYOffset || document.body.parentNode.scrollTop || 0;
  if (sx || sy) {
    document.location.href = this.href + '&_sx=' + sx + '&_sy=' + sy;
    return false;
  }
  return true;
}

function steadyViewFormHook() {
  var sx = window.pageXOffset || document.body.parentNode.scrollLeft || 0;
  var sy = window.pageYOffset || document.body.parentNode.scrollTop || 0;
  if (sx || sy) {
    var e = document.createElement('input');
    e.setAttribute('type', 'hidden');
    e.setAttribute('name', '_sx');
    e.setAttribute('value', sx);
    this.appendChild(e);
    e = document.createElement('input');
    e.setAttribute('type', 'hidden');
    e.setAttribute('name', '_sy');
    e.setAttribute('value', sy);
    this.appendChild(e);
  }
  return true;
}


onloadInit();


// --- Google Analytics --------------------------------
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-8737092-1']);
_gaq.push(['_trackPageview']);

(function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
// --- Google Analytics --------------------------------


