  function $(x) { return document.getElementById(x) }
  function $A(x) { return Array.prototype.slice.call(x) }
  function collect() {
    var e = $A(document.body.childNodes)
            .filter(function(x) { return x.nodeType == 1 && x.getAttribute('selected') })[0],
        a = getInputs(e),
        enc = encodeURIComponent, r = [], i;
    window.actionURL = e.getAttribute('action');
    for (i = 0; i < a.length; i++)
      r.push(a[i].name + '=' + enc(a[i].value));
    return r.join('&');
  }
  function getInputs(e) {
    var a1 = $A(e.getElementsByTagName('input')),
        a2 = $A(e.getElementsByTagName('textarea')),
        a3 = $A(e.getElementsByTagName('select'));
    return a1.concat(a2).concat(a3);
  }
  function submit() {
    var req = new XMLHttpRequest(), data = collect();
    req.open('POST', window.actionURL, true);
    req.onreadystatechange = function() {
      if (req.readyState != 4) return;
      $('result').innerHTML = req.responseText;
      var m = /<script>(.+?)<\/script>/.exec(req.responseText);
      if (m) eval(m[1]);
        else iui.showPageById('result');
    };
    req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    req.send(data);
  }

  var showPageOriginal = iui.showPage, curPageID = null;
  iui.showPage = function(page) {
    if (!page) return;
    if (!auth && hasClass(page, 'members'))
      return alert('Please log in to access this area');
    if (page.id == 'login' || page.id == 'logout') window[page.id]();
    var skip = (curPageID == 'result' && $('back2'));
    curPageID = page.id;
    if (skip) return history.back();

    featReset(page.id == 'home');
    updateLogo(page.id == 'home' || page.id == 'loginForm');
    updateButton(page.getAttribute('action'));
    showPageOriginal.apply(this, arguments);
  }
  function updateLogo(on) {
    $('toolbar').className = 'toolbar' + (on ? ' logo' : '');
  }
  function updateButton(on) {
    $(!auth ? 'b_login' : 'b_logout').style.display = (!on ? 'block' : 'none');
    $('b_submit').style.display = (on ? 'block' : 'none');
  }

  function login() {
    auth = true;
    $('b_login').style.display = 'none';
    $('b_logout').style.display = 'block';
    var a = document.getElementsByClassName('members'), i;
    for (i = 0; i < a.length; i++)
      a[i].style.display = 'block';
    //setTimeout(function() { iui.showPageById('home'); }, 1000);
    window.location.reload();
  }
  function logout() {
    window.location.reload();
  }
  function userAlert() {
    alert('Please log in to access this area');
    return false;
  }

function hasClass(e, x) {
  return ((' ' + (e.className || '') + ' ').indexOf(' ' + x + ' ') >= 0);
}
if (!document.getElementsByClassName)
  document.getElementsByClassName = function(name) {
    var c = document.getElementsByTagName('*'), r = [], i;
    for (i = 0; i < c.length; i++)
      if (hasClass(c[i], name)) r.push(c[i]);
    return r;
  };

// featured
  var area = null, parts = [];
  function featInit() {
    area = $('home_featured');
    if (!area) return;
    var a = area.childNodes, i;
    for (i = 0; i < a.length; i++) {
      if (a[i].className == 'link') continue;
      if (a[i].nodeType == 1) parts.push(a[i]);
    }
    for (i = 0; i < parts.length; i++) {
      if (i) parts[i].style.left = '-100%';
      parts[i].style.display = 'block';
    }
  }

  var timer = null;
  function featReset(on) {
    if (!area) featInit();
    if (!area) return;
    if (on) timer = setTimeout(featNext, timeSwitch * 1000);
      else timer = clearTimeout(timer);
  }
  function featNext() {
    var step = 0, handler, e1 = parts[0], e2 = parts[1],
        size = e1.parentNode.offsetWidth, d = size / stepScroll;
    handler = setInterval(function() {
      var p = d * (++step);
      e1.style.left = Math.round(-p) + 'px';
      e2.style.left = Math.round(size - p) + 'px';
      if (step < stepScroll) return;
      clearInterval(handler);
      e1.style.left = '-100%';
      e2.style.left = '0';
      parts.push(parts.splice(0, 1)[0]);
      featReset(true);
      ++featShown;
      if (!featAll && parts.length - featShown == 1)
        featLoad();
    }, 20);
  }

  var featAll = false, featShown = 0;
  function featLoad() {
    var list = [], i;
    for (i = 0; i < parts.length; i++) {
      var m = parts[i].href.match(/\d+/);
      if (m) list.push(m[0]);
    }
    var req = new XMLHttpRequest();
    req.open('POST', 'index/featuredpreload', true);
    req.onreadystatechange = function() {
      if (req.readyState != 4) return;
      var container = document.createElement('div'), res;
      container.innerHTML = req.responseText;
      res = container.childNodes;
      for (i = 0; i < res.length; i++)
        if (res[i].nodeType == 1) {
          parts.splice(1, 0, res[i]);
          res[i].style.left = '-100%';
          res[i].style.display = 'block';
          area.insertBefore(res[i], parts[2]);
        }
      if (!req.responseText.replace(/\s+/, ''))
        featAll = true;
    };
    req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    req.send('ignore=' + list.join(','));
  }