var postsAnimPage;
function initPages() {
    var elements = document.getElementsByClassName('page');
    postsAnimPage = new AnimatePages(elements, {skipSpeed: 15, skipCallback: function() { postsPageSkipped(); }, playCallback: function() { postsPlay(); }, pauseCallback: function() { postsPause(); }});
    $('pageinfo').innerHTML = 'Viser side '+postsAnimPage.getCurrentPage()+' af '+postsAnimPage.getPageCount();
    EventX.observe('playpause','click',function() { postsAnimPage.playpauseClicked(); });
    EventX.observe('prevbutton','click',function(){ postsAnimPage.prevClicked(); });
    EventX.observe('nextbutton','click',function(){ postsAnimPage.nextClicked(); });
}
function postsPageSkipped() {
    $('pageinfo').innerHTML = 'Viser side '+postsAnimPage.getCurrentPage()+' af '+postsAnimPage.getPageCount();
}
function postsPlay() {
    $('playpause').className = 'play';
}
function postsPause() {
    $('playpause').className = 'pause';
}

function validatesearch(form) {
    if (form.query.value.length > 0) {
        try {
            document.location.href = '/search.php/query-' + escape(form.query.value) + '.html'
            return false;
        } catch(e) {
            return true;
        }
    } else {
        alert('Indtast venligst et søgeord.');
        return false;
    }
}

var slideAnimPage;
function initSlides() {
    var elements = document.getElementsByClassName('slide');
    slideAnimPage = new AnimatePages(elements, {skipSpeed: 10});
    EventX.observe('slideleft','click',function(){ slideAnimPage.prevClicked(); });
    EventX.observe('slideright','click',function(){ slideAnimPage.nextClicked(); });
}

var statusTickerAnim;
function initStatus() {
    statusTickerAnim = new StatusTicker($('statusticker').getElementsByTagName('div'));
}

var StatusTicker = Class.create();
StatusTicker.prototype = {
  initialize: function(elements, options) {
    this.elements        = elements;
    
    if (this.setOptions) {
        this.setOptions(options);
    } else {
        this.options = options || {};
    }

    this.options.popSpeed = this.options.popSpeed || 5;
    
    this.popInterval = setInterval(this.next.bind(this), (this.options.popSpeed*1000));
    this.currentIdx = 0;

    Element.show(this.elements[0]);
  },
  
  next: function() {
	  if (this.elements.length > 0) {
          new Effect.SlideUp(this.elements[this.currentIdx], {afterFinish:this.slideUpDone.bind(this)});
	  }
  },

  slideUpDone: function() {
      this.currentIdx++;
      if (this.currentIdx >= this.elements.length) {
	      this.currentIdx = 0;
      }
      new Effect.SlideDown(this.elements[this.currentIdx]);
  }

}