function initTeaserGallery()
{
  if ($('.gallery-item:visible').size() > 1)
    $('.gallery-item:first-child').addClass('selected');

  $('#gallery-pager a').click(
    function()
    {
      clearTimeout(_slideshowTimer, _sloganTimer);
      _slideshowTimer = setTimeout(slideshowNext, 4000);

      var m = this.search.match(/i=(\d+)/), i;

      if (!m)
        return;

      i = parseInt(m[1]);
      $('.gallery-item.last-selected')
        .removeClass('last-selected');
      $('.gallery-item.selected')
        .removeClass('selected')
        .addClass('last-selected');
      $('#gallery-item-' + i)
        .css({opacity: 0.0})
        .animate({opacity: 1.0}, 1000)
        .addClass('selected');
      $('#gallery-pager a.selected').removeClass('selected');
      $(this).addClass('selected');

      _sloganTimer = setTimeout(
        function()
        {
          var s = '';
          if ($('.gallery-item.selected img').attr('alt') != undefined)
            s += '<h1>' + $('.gallery-item.selected img').attr('alt') + '<\/h1>';
          if ($('.gallery-item.selected img').attr('title') != undefined)
            s += '<p>' + $('.gallery-item.selected img').attr('title') + '<\/p>';

          $('#slogan-wrapper')
            .empty()
            .html(s);
        },
        0
      );
      if (i == 0) {
        $('.scrollable').data('scrollable').begin(1000);
      } else {
        var api = $('.scrollable').data('scrollable'),
            s = api.getItems().size();
        if (i + 1 < s)
          api.seekTo(i - 1, 1000);
      }
      $('.scrollable').find('div.selected').removeClass('selected');
      $('.scrollable').find('a.selected').parent().addClass('selected');
      this.blur();
      return false;
    }
  );

  _slideshowTimer = setTimeout(slideshowNext, 4000);
} // end func initTeaserGallery

var _slideshowTimer, _sloganTimer;

function slideshowPrev()
{
  var vis = $('#gallery-pager a.selected');
  if (vis.parent().prev('div').size()) {
    vis.parent().prev('div').find('a').click();
  } else {
    vis.parent().siblings('div').find('a:last').click();
  }
  return false;
}

function slideshowNext()
{
  var vis = $('#gallery-pager a.selected');
  if (vis.parent().next('div').size()) {
    vis.parent().next('div').find('a').click();
  } else {
    vis.parent().siblings('div:first').find('a').click();
  }
  return false;
}
