var DocPager = new Class({

  /**
   * Constructor
   */
  initialize: function() {

    this.top = $('top').getCoordinates().top;
    this.last = 0;
    
    this.setButtons();
  },

  setButtons: function() {
  
    // only do this if msie 6+
    var ver = navigator.appVersion.match(/msie ?([5-8])/i);
    if(Browser.Engine.trident && ver[1].toInt() < 6) return;
    
    // print button
    $$('.print_button').each(function(el) {
      el.setProperty('href', '#');
      el.addEvent('click', function(e) {
        new Event(e).stop();
        this.printBox();
      }.bind(this));
    }, this);

    // topic button
    if($$('.topic_button, .cits_button').length > 0) {

      this.type = 'topic';
      this.pages = 2;
      this.topicButtons = $$('.topic_button, .cits_button');

      var sec = location.href.match(/sec=(1|2)/);
      this.page = sec ? sec[1].toInt() : 1;

      this.topicButtons.each(function(el) {
        el.setProperty('href', '#');
        el.addEvent('click', function(e) {
          new Event(e).stop();
          this.toggleTopic();
        }.bind(this));
      }, this);

    // article buttons
    } else if($$('#art_nav li').length > 0) {

      this.type = 'article';
      this.page = $$('#art_nav a.selected')[0].get('html').toInt();
      this.pages = $$('#art_nav ul li').length - 2;

      $$('#art_nav a').each(function(el) {
        var i;
        switch(el.get('html')) {
          case 'Next Page':
            i = 'next'; break;

          case 'Previous Page':
            i = 'prev'; break;

          default:
            i = el.get('html').toInt(); break;

        }

        el.setProperty('href', '#');
        el.addEvent('click', function(e) {
          new Event(e).stop();
          this.artPage(i);
        }.bind(this));

      }, this);

    }

  },
  
  /**
   * Toggle topic pages
   */
  toggleTopic: function() {
    var cap, cls;
    switch(this.page) {
      case 1:
        this.gotoPage(2);
        cap = 'View Topic Notes';
        cls = 'topic_button';
        break;
        
      case 2:
        this.gotoPage(1);
        cap = 'View Readings';
        cls = 'cits_button';
        break;
        
    }
    // set link html
    this.topicButtons.each(function(el) {
      el.set('html', cap);
      var alcs = cls == 'topic_button' ? 'cits_button' : 'topic_button';
      el.removeClass(alcs);
      el.addClass(cls);
    }, this);
  },
  
  /**
   * Go to article page
   * @param mixed num Number, 'prev' or 'next'
   */
  artPage: function(num) {

    if(num == 'prev' && this.page > 1) {
      this.gotoPage(this.page - 1);

    } else if(num == 'next' && this.page < this.pages) {
      this.gotoPage(this.page + 1);
      
    } else if(num != 'prev' && num != 'next') {
      this.gotoPage(num);
      
    }

    // set selected
    if(this.page != this.last) {
      var li = $$('#art_nav li');
      
      li[this.last].getElement('a').removeClass('selected');
      li[this.page].getElement('a').addClass('selected');
    }

  },
  
  /**
   * Go to "page"
   * @param integer num New page number
   */
  gotoPage: function(num) {
    if(num != this.page) {
      this.last = this.page;

      $('page' + this.last).removeClass('pager_show');
      $('page' + this.last).addClass('pager_hide');
      $('page' + num).removeClass('pager_hide');
      $('page' + num).addClass('pager_show');

      this.page = num;
      if(window.getScroll().y > this.top) window.scrollTo(0, this.top);
    }
  },
  
  /**
   * Show print dialog
   */
  printBox: function() {
    window.print();
  },
  
  /**
   * Qa jumper
   */
  jump: function() {
    var id = $('qamenu').get('value');
    location.href = 'qa.php?id=' + id;
  }

});

var pager;
window.addEvent('domready', function() { pager = new DocPager(); });
