(function($) {
  $.fn.extend({
    slider : function() { if(!$.slider.initilized) $.slider.init(this); }
  });
  var Slider = {};
  (function(){
    this.initilized = false;
    this.state = 'start';
    this.page = 0;
    this.contents = null;
    this.range = {section:0, page:0};
    this.position = {section:0, page:0};
    this.init = function(target) {
      this.contents = $(target).find('.contents');
      this.contents.prepend('<h1 class="agenda">Agenda</h1>');
      this.initilized = true;
      this.range.section = this.contents.find('.section').length;
      this.range.page = this.contents.find('.page').length;
      $(target).find(".footer").css({position: 'absolute', right:'10px', bottom:'10px'});
      this.exec();
    }
    this.exec = function() {
      this.contents.find('*').hide();
      this[this.state]();
    }
    this.start = function() {
      this.contents.css({height:$(window).height(),width:$(document).width()});
      this.contents.find('h1.title').show().center(false).hide().fadeIn('slow');
      this.state = 'agenda';
      var self = this;
      $('body').bind("click",function(){ return self.exec.apply(self);});
    }
    this.agenda = function() {
      this.contents.find('h1.agenda').show().css({textAlign: 'center'});
      var elems = this.contents.find('.section:eq(' + this.position.section + ')');
      elems.find('h2').andSelf().show().css({padding: '10px', marginLeft: '1em'});
      elems.find('.page').find('h3').andSelf().show().css({padding: '10px', marginLeft: '1em'});
      this.position.section++;
      if(this.position.section >= this.range.section) {
        this.state = 'nextpage';
        this.position.section = 0;
      }
    }
    this.nextpage = function () {
      var elems = this.contents.find('.page:eq(' + this.position.page + ')');
      elems.parent().find('h2').andSelf().show();
      elems.find('*').andSelf().show();
      this.position.page++;
      if(this.position.page >= this.range.page) {
        this.state = 'start';
        this.position.page = 0;
      }
    }
  }).apply(Slider);

  $.extend({ slider : Slider });
})(jQuery);