GetMore = Class.create({
  initialize: function(link_container, update_container, total_items, url, options) {
       //if(!$(link_container))
           //throw "Pagination could not find the element: " + link_container;
    this.link_container  = link_container;
    this.update_container = update_container;
    this.url = url;
	this.current_offset = 0;
	this.total_items = total_items;
    
    this.options = {
			itemsPerPage: 5,
            comment_for: 'poem',
            permission: 'public',
            for_id: 0
		};

        // this line extends the options to overide the default options.
        Object.extend(this.options, options || {});

        // check if we even need the load more link
        // remove the link cuzzz we dont need this stinkin' thang.....    :()
        if(this.total_items < this.options.itemsPerPage)
        {
             // remove the link to load more comments
             $(link_container).hide();
        }


        // create the initial links
        Event.observe($(link_container), 'click', this.load_more_comments.bind(this));
		
		//options.afterChange;

    },
    
    load_more_comments: function() {

        // calculate the offset for the querie
        this.current_offset = (this.current_offset + this.options.itemsPerPage);

        // now lets do a killer ajax request
        
        // setup the blessed params
        params = {
            offset_start: this.current_offset,
            num_per_page: this.options.itemsPerPage,
            commentFor: this.options.comment_for,
            for_id: this.options.for_id,
            area: this.options.permission
        };
        
        // AJAX Time!!
        new Ajax.Updater(this.update_container, this.url, {
            parameters: params,
            onCreate: this.loadWait.bind(this),
            insertion: 'bottom',
            evalScripts: true,
            onComplete: this.complete.bindAsEventListener(this),
            method: 'post'
        });
    },
    
    complete: function() {
    
        // stop the interval
        clearInterval(this.inverval);
        
        // change the text back
        // put the button text back to normal
        $(this.link_container).update(this.currentText); 

        // check to see if we should remove the "Load more comments" link
        if((this.current_offset + this.options.itemsPerPage) > this.total_items)
        {        
            //remove the link
            $(this.link_container).hide(); 
        }
    },
    
    loadWait: function() {
        
        // get the current test from the loading button
        this.currentText  = $(this.link_container).innerHTML;
        
        $(this.link_container).update('Loading');

        // change the text to loading
        this.inverval = window.setInterval(function(){
               text = $(this.link_container).innerHTML;
               if(text.length < 20)
               {
			       $(this.link_container).update(text + '.');
			   }
			   else
			   {
                   $(this.link_container).update('Loading');			   
			   }
			}.bind(this), 300);	
    }
    
});