var Lister = Class.create({
    /*
     Function: initialize
     Constructor for Lister Class.
     
     Parameters:
     el - {Mixed} the HTML object that Lister is initialize from. This can be either an HTML Object or ID to an HTML object.
     opt  - {Object} config Object
     */
     
    initialize: function(el, opt){
        el = $(el);
        opt = opt || {};
        this.options = {
        		movement: 'slide',
            amount: 10,
            standing_header: '',
            omit: {},
            ul_List: "ul_" + el.id.capitalize(),
            starting: 0,
            previous_button_enabled: true,
            url: "/inc/" + el.id + '.php',
            orientation: 'vertical',
            el: el
        };
  		Object.extend(this.options, opt);
  		Object.extend(this, this.options);
 		
  	//	this.wrap();
      this.handlePreviousButton();


      el.select('.previous').invoke('observe','click',function() {
        if (this.movement == 'page')	{ 
	        this.changeStarting(-this.amount);
        }	else	{
	        this.changeStarting(-1);
        }
        this.load();
      }.bind(this));

      el.select('.next').invoke('observe','click',function() {
        if (this.movement == 'page')	{ 
        	this.changeStarting(this.amount);
        }	else	{
	        this.changeStarting(1);
        }
        this.load();       
      }.bind(this));
      
      
  		
    },
    
    changeStarting: function(delta)  {

      this.starting = parseInt(this.starting) + parseInt(delta);

      if (this.starting <= 0) {
        // disable!  this is giving us trouble.  nice, but think it through better!
        this.starting = 0;
        //this.previous_button_enabled = false;
        //this.handlePreviousButton();
      } else if(!this.previous_button_enabled)  {
        this.previous_button_enabled = true;
        this.handlePreviousButton();
      }
    },
    
    handlePreviousButton: function()  {
        this.el.select('.previous').each(function(e) {
          e.disabled = !this.previous_button_enabled;
        }.bind(this));
    },
    
    wrap: function()  {
            
      var prev_value = this.orientation == 'horizontal' ? '&larr;' : '&uarr;';
      var next_value = this.orientation == 'horizontal' ? '&rarr;' : '&darr;';
      
      prev_value = prev_value.unescapeHTML();
      next_value = next_value.unescapeHTML();
      
      //make a new div for the top
      var w = new Element('div',{'class':'listwrap'});
      
      var h = this.el.select('h1');
      
      var p = new Element('input',{
        'type':'button',
        'class':'previous',
        'value': prev_value
        });
        
      var n = new Element('input',{
        'type':'button',
        'class':'next',
        'value': next_value
        });

      this.el.insert({top: w});
      w.insert({bottom: p});
      w.insert({bottom: n}); 
      w.insert({bottom: h});
      
      //console.log('hey' + w.innerHTML);
      
    },
    
    load: function()  {
        new Ajax.Updater(this.ul_List,this.url,{
          parameters: {
            starting: this.starting,
            amount: this.amount,
            search_string: this.search_string,
            omit: Object.toJSON(this.omit),
            ar_Tags: Object.toJSON(this.ar_Tags),
            delta: this.delta,
            standing_header: this.standing_header,
            dt: this.dt,
            dt_Now: this.dt_Now
          },
          onSuccess: function(){
            //alert('!!');
          },
          onFailure: function(){
          },
          onComplete: function(){
            //alert(this.delta);
          }
        });
    }
    
  });