var citations = {

  items : null,
  count : 0,
  index : 0,
  interval : null,

  next : function() {
      citations.index++;
      if (citations.index == citations.count) citations.index = 0;
      citations.show();
  },

  show : function() {
    var visible = $('.citations li.visible');
    $('.citation-links a').removeClass('select');
    if (visible.length > 0) {
      $('.citations li.visible').animate(
        {left:-560},
        1000,
        function() {
          $(this).hide().removeClass('visible').css('left','45px');
          citations.display();
        }
      );
    } else {
      citations.display();
    }
  },

  display : function() {
    citations.items.eq(citations.index).fadeIn(1000, function() {
      $(this).addClass('visible');
    });
    $('.citation-links a[href="#c'+(citations.index + 1)+'"]').addClass('select');
  },

  stop : function() {
    clearInterval(citations.interval);
  },

  init : function() {
    citations.items = $('.citations li');
    citations.count = citations.items.length;
    citations.show();
    citations.interval = setInterval(function() {
      citations.next();
    }, 8000);

    $('.citation-links a').click(function(e) {
      e.preventDefault();
      citations.stop();
      $('.citations li').hide().removeClass('visible');
      $('.citation-links a').removeClass('select');
      var target = $(this).attr('href');
      $(target).show();
      $(this).addClass('select');
    });
  }

}

var scroller = {

  curr : 0,
  top: 0,

  getOffset : function(index) {
    var offset = (Math.abs(index - scroller.curr)) * 970;
    offset = (index > scroller.curr) ? '-='+offset : '+='+offset;
    return offset;
  },

  goToHash : function(hash) {
    var link = $('#pagenav a[href="'+hash+'"]');
    var subpage = $(hash+'_page');
    var index = $('.subpage').index(subpage);
    var offset = scroller.getOffset(index);
    scroller.curr = index;
    //$('html, body').animate({scrollTop: scroller.top+'px'}, 300);
    $('html, body').animate({scrollTop: '0px'}, 300);
    $('#pagenav a').removeClass('select');
    $('#pagenav ul').css('opacity','.5');
    $('.scroller').animate({left:offset}, 1000, function() {
     $('#pagenav ul').css('opacity','1');
      link.addClass('select');
    });
    location.hash = hash;
  },

  init : function() {
    scroller.top = $('#pagenav').offset().top;
    var hash = location.hash;
    if (hash) scroller.goToHash(hash);

    $('#pagenav a, .next a').click(function(e) {
      e.preventDefault();
      var target = $(this).attr('href');
      if (target.indexOf('#') == 0) {
        scroller.goToHash(target);
      } else {
        $('html, body').animate({scrollTop: '0px'}, 300, function() {
          window.location = target;
        });
      }
    });

  }

}

$(function() {
  $('.honeypot').remove(); // remove temporary elem that prevent FF scroll to anchor

  // change scroller page id to avoid default hash behavior
  $('.subpage').each(function() {
    $(this).attr('id', $(this).attr('id') + '_page');
  });
  if ($('.citations').size()) citations.init();
  if ($('.scroller').size()) scroller.init();

  $('a[rel*=facebox]').facebox();

});
