/*
 * Copyright (c) Kazuhiro Iizuka
 * kazuhiro at mail.aimy.jp ( http://aimy.co.jp/ )
 * License: BSD Licence
 */
var Pager = Class.create();
Pager.prototype =
{
  config: null,
  total_: 0,
  start_: 0,
  show_data_count_: 10,
  show_page_range_: 15,
  page_key_: 'p',
  mode_: 'straight',
  half_range_: true,
  max_page_: 0,
  current_page: 0,
  previous_text_: 'Previous',
  next_text_: 'Next',
  separator: '|',

  calc_page_index: function(i){
    var page_index = 0;
    switch(this.mode_){
    case 'straight':
      page_index = i-1;
      break;
    case 'count_up':
      page_index = (i-1)*this.show_data_count_;
      break;
    case 'same':
      page_index = i;
      break;
    }
    return page_index;
  },

  initialize: function(config){
    if( config===undefined ){
      alert("[Pager]you need to set config = {link_url:'Js.Function',start:0,total,99,display_count:10}");
      return;
    }
    if( config.link_url===undefined ){
      alert("[Pager]You need to set link_url at config");
      return;
    }
//debug("<hr>");
    var start = (config.start===undefined)?0:parseInt(config.start);
//debug("start="+start);
    this.link_url_ = config.link_url;
    this.total = parseInt(config.total);
    if( config.display_count ){
      this.show_data_count_ = config.display_count;
    }
    if( config.page_range ){
      this.show_page_range_ = config.page_range;
    }
    if( config.mode ){
      this.mode_ = config.mode;
    }
    this.max_page = Math.ceil(this.total/this.show_data_count_);
    var half_range = Math.floor(this.show_page_range_/2);
    switch(this.mode_){
    case 'straight':
      this.current_page = start+1;
      if( this.half_range_ && this.current_page>half_range ){
	this.start_page = this.current_page-half_range;
	this.stop_page = Math.min(this.max_page,this.current_page+half_range);
      }else{
	this.start_page = Math.floor(this.calc_page_index(this.current_page)/this.show_page_range_)*this.show_page_range_+1;
	this.stop_page = Math.min(this.max_page,this.start_page+this.show_page_range_-1);
      }
      break;
    case 'count_up':
      this.current_page = parseInt(start/this.show_data_count_)+1
      if( this.half_range_ && this.current_page>half_range ){
	this.start_page = this.current_page-half_range;
	this.stop_page = Math.min(this.max_page,this.current_page+half_range);
      }else{
	this.start_page = Math.floor((this.current_page-1)/this.show_page_range_)*this.show_page_range_+1;
	this.stop_page = Math.min(this.max_page,this.start_page+this.show_page_range_-1);
      }
      break;
    case 'same':
      this.current_page = (start==0?1:start);
      if( this.half_range_ && this.current_page>half_range ){
	this.start_page = this.current_page-half_range;
	this.stop_page = Math.min(this.max_page,this.current_page+half_range);
      }else{
	this.start_page = Math.floor((this.current_page-1)/this.show_page_range_)*this.show_page_range_+1;
	this.stop_page = Math.min(this.max_page,this.start_page+this.show_page_range_-1);
      }
      break;
    default:
      alert("[Pager.js]no such mode");
      return;
    }
/*
debug("show_page_range_="+this.show_page_range_);
debug("current_page="+this.current_page);
debug("start_page="+this.start_page);
debug("stop_page="+this.stop_page);
debug("max_page="+this.max_page);
debug("half_range="+half_range);
*/
  },
  display: function(params){
    if( params===undefined ){
      params={use_id:true};
    }

    var links = "";
    if( this.max_page>1 ){
      links += '<span class="page_link">';
      // Previous
      if( this.current_page>1 ){
	var previous_page = this.calc_page_index(this.current_page-1);
	links += '<a '+(params.use_id?'id="page_link_previous" ':'');
	links += 'href="javascript:'+this.link_url_+'('+previous_page+');">';
	links += this.previous_text_+'</a> '+this.separator+'&nbsp;';
      }

      // Page Listing
      for(var i=this.start_page;i<=this.stop_page;i++){
	if( i==this.current_page ){
	  links += '<span class="page_link_current">'+i+'</span>&nbsp;';
	}else{
	  page_index = this.calc_page_index(i);
	  links += '<a class="page_link_a" href="javascript:'+this.link_url_+'('+page_index+');">'+i+'</a>&nbsp;';
	}
      }

      // Next
      if( this.current_page < this.max_page ){
	var next_page = this.calc_page_index(this.current_page+1);
	links += this.separator+'&nbsp;';
	links += '<a '+(params.use_id?'id="page_link_next" ':'');
	links += 'href="javascript:'+this.link_url_+'('+next_page+');">';
	links += this.next_text_+'</a>';
      }

      links += '</span>';
    }
    return links;
  }
};

//function debug(str){ $('debug').innerHTML+=str+"<br>"; }
