$(function(){
    var init = function() {
      var ht = $(window).outerHeight() - 130;
      $('.toc').height(ht);
      $('.content').height(ht);
    };
    init();
  var pagination = {
    getMJQuery: function() {
      if(!this.mJQuery) {
        this.mJQuery = self.parent.frames['toc'].window.jQuery;
      }
      return this.mJQuery;
    },
    menus: ['itemFont_1','itemFont_2','itemFont_3','itemFont_4','itemFont_5','item01','item01_1','item01_2','item01_3','item02','item02_1','item02_2','item02_3','item02_3_1','item02_3_2','item02_4','item02_5','item03','item03_1','item03_1_1','item03_1_2','item03_2','item04','item04_1','item04_2','item04_2_1','item04_2_2','item04_2_3','item05','item05_1','item05_2','item05_3','item05_3_1','item05_3_2','item05_4','item06','item06_1','item06_2','item06_3','item07','item07_1','item07_2','item07_2_1','item07_2_2','item08','item08_1','item08_1_1','item08_1_2','item08_1_3','item08_1_4','item08_1_5','item08_2','item08_3','item08_3_1','item08_3_2','item09','item09_1','item09_2'],
    currentIndex: 0,
    setCurrent: function(id) {
      var mJQuery = this.getMJQuery(),
        item = mJQuery('#' + id);
      mJQuery('.current').removeClass('current');
      item.addClass('current');
      for(var i = 0; i < this.menus.length; i++) {
        if(id === this.menus[i]) {
          this.currentIndex = i;
          return;
        }
      }
    },
    expand: function(current) {
      var flag = current.parent().parent().parent('ul');
      while(flag && flag.is(':hidden')) {
        flag.parents('li').attr('class', 'collapsable')
        flag.parents('li').children('div').attr('class', 'hitarea collapsable-hitarea');
        flag.show();
        flag = flag.parent().parent('ul');
      }
    },
    rollNext: function() {
	  if (this.currentIndex == this.menus.length-1){
		return;
	  }
      var mJQuery = this.getMJQuery(),
      item = mJQuery('#' + this.menus[this.currentIndex>=this.menus.length-1 ? 0 : ++this.currentIndex]);
      this.expand(item);
      this.setCurrent(item.attr('id'));
      //$('.content').attr('src', item.attr('href'));
	  self.parent.frames['content'].window.location = item.attr('href');
    },
    rollPre: function() {
	  if (this.currentIndex <= 0){
		return;
	  }
      var mJQuery =  this.getMJQuery(),
      item = mJQuery('#' + this.menus[this.currentIndex <= 0 ? this.menus.length - 1 : --this.currentIndex]);
      this.expand(item);
      this.setCurrent(item.attr('id'));
	  //$(self.parent.frames['content']).attr('src', item.attr('href'));
	  self.parent.frames['content'].window.location = item.attr('href');
    },
    setEvent: function() {
      var self = this, mJQuery =  self.getMJQuery();
      mJQuery('.file a').click(function(e) {
        self.setCurrent($(this).attr('id'));
      });
      mJQuery('.folder a').click(function(e) {
        self.setCurrent($(this).attr('id'));
      });
    }
  };
  $('.up').click(function(e){
    e.preventDefault();
    pagination.rollPre();
  });
  $('.down').click(function(e){
    e.preventDefault();
    pagination.rollNext();
  });
  $('.logo, .home').click(function(e){
    pagination.currentIndex = -1;
  });
  setTimeout(function(){pagination.setEvent(); }, 1000);
  window.pagination = pagination;
});
