var truncate = Class.create({
    
	initialize: function(container, options) {
    this.options = Object.extend({
      defaultLength: 100
    }, options || {});
    
    this.container = $(container);
    
    // see if there is a class name that is a number
    this.container.classNames().each(function(name){
      if(parseInt(name) > 0) this.options.defaultLength = parseInt(name);
    }.bind(this));   

    this.originalContent       = this.container.innerHTML;
    this.wrapper               = this.container.up();
    this.originalWrapperHeight = this.wrapper.style.height; // getHeight wouldn't work with IE6
    console.log(this.originalContent.length);
    console.log(this.options.defaultLength);
    if (this.originalContent.length <= this.options.defaultLength) {
      return true;
    }
    
    this.truncatedContent = this.originalContent.substring(0, this.options.defaultLength);
    this.truncatedContent = this.truncatedContent.replace(/\w+$/, '');
    
    this.moreLink = '... <a href="javascript:void(0);" class="more">show full text</a>';
    this.lessLink = '<a href="javascript:void(0);" class="less">hide text</a>';
    
    this.truncate();
  },
  
  truncate: function() {
    this.container.innerHTML = this.truncatedContent + this.moreLink;
    this.wrapper.setStyle({height: this.originalWrapperHeight});
    var link = this.container.select('.more').first();    
    Event.observe(link, 'click', this.expand.bindAsEventListener(this));
  },
  
  expand: function() {
    this.wrapper.setStyle({height: ""});
    this.container.innerHTML = this.originalContent + this.lessLink;
    var link = this.container.select('.less').first();    
    Event.observe(link, 'click', this.truncate.bindAsEventListener(this));
  }
});

document.observe('dom:loaded', function() { 
	$$('.truncate').each(function(e){ new truncate(e); });
});
