/**
 * Zine Website Scripts
 * ~~~~~~~~~~~~~~~~~~~~
 *
 * Some scripts for the Zine webpage.
 *
 * :copyright: 2008 by Armin Ronacher.
 * :license: BSD.
 */

var zine = {
  
  /* make a list an animated feature list */
  showAsSlideshow : function(expr, options) {
    options = options || {};
    var
      items = $('li', expr).hide(),
      index = -1,
      timeout = options.timeout || 4000,
      fadeTime = options.fadeTime || 'slow';

    if (!items.length)
      return;

    function nextImage() {
      if (index >= 0)
        $(items[index]).fadeOut(fadeTime);
      $(items[index = (index + 1) % items.length]).fadeIn(fadeTime);
      window.setTimeout(nextImage, timeout);
    }
    nextImage();
  },

  /* displays one mail at the time */
  mailingListDisplay : function(mails) {
    function display(id) {
      $('div.jumpbox li div.line').removeClass('selected');
      $(document.getElementById('link-' + id)).addClass('selected');
      container.html($(document.getElementById(id)).html());
    }

    mails.hide();
    var container = $('<div class="mail"></div>')
      .appendTo('div.body div.contents div.mails');

    $('div.jumpbox a,div.jumpbox div.line').each(function() {
      var id = $('a', $(this).parent()).attr('href').substr(1);
      $(this).click(function() { display(id); });
    }).css({cursor: 'pointer'});

    var href = document.location.href.split(/#/, 2)[1];
    if (href != null) {
      display(href);
      window.scroll(0, 0);
    }
    else
      display(mails[0].id);
  },

  /* called on initializations */
  init : function() {

    /* add anchors to headlines */
    for (var i = 1; i <= 6; i++)
      for (var j = 0; j <= 1; j++)
        $('h' + i + (j == 1 ? ' a' : '') + '[@id]').each(function() {
          var anchor = $('<a class="anchor">¶</a>')
            .attr('href', '#' + $(this).attr('id'))
            .attr('title', 'Permalink to this headline')
            .appendTo(this);
        });

    /* one-thread-display for mailinglist */
    var mails = $('div.mails div.mail');
    if (mails.length > 0)
      zine.mailingListDisplay(mails);
  }
};

$(function() {
  zine.init();
});
