/* 
v1.1  : updated to cater for lines that are already short enough
Credit: Taimar, http://taimar.pri.ee/ 
*/
var truncater = {
  init: function() {
    // find all things of class "truncated" and truncate the text therein
    // so that it's only one line high.
    var allels = document.getElementsByTagName('*');
    for (var i=0;i<allels.length;i++) {
      if (allels[i].className && allels[i].className.match(/\btruncated\b/)) {
        truncater.truncate(allels[i]);
      }
    }
  },
  
  truncate: function(el) {
    // see if el already has been truncated; if so, untruncate it first
    var spans = el.getElementsByTagName('span');
    for (var j=0; j<spans.length; j++) {
      if (spans[j].className == 'truncremove') {
        el.firstChild.nodeValue += spans[j].firstChild.nodeValue;
        el.removeChild(spans[j]);
      }
    }
    // el now has all its text. Gradually remove words of text until it's only
    // one line high.
    var span = document.createElement('span');
    span.className = 'truncremove';
    span.style.display = 'none';
    var hidet = document.createTextNode('');
    span.appendChild(hidet);
    el.appendChild(span);
    
    var showt = el.firstChild; // assume el contains one text node only!
    
    // first, move all the text into the invisible span, bar one char
    hidet.nodeValue += showt.nodeValue;
    showt.nodeValue = 'X';
    
    // measure the line height
    var oneLineHeight = el.offsetHeight;
    
    // remove the char
    showt.nodeValue = '';
    
    // gradually add text, word by word, until the line height goes up
    var abort = false;
    var firstWord;
    while (!abort) {
      firstWord = hidet.nodeValue.match(/^\s*\S+\s+/);
      if (!firstWord) {
        if (hidet.nodeValue != '') {
          firstWord = hidet.nodeValue
        } else {
          abort = true;
          continue;
        }
      }
      showt.nodeValue += firstWord;
      hidet.nodeValue = 
hidet.nodeValue.substr(firstWord.toString().length);
      if (el.offsetHeight > oneLineHeight) abort = true;
    }
    
    // remove the last word we added, if we're over one line
    if (el.offsetHeight > oneLineHeight) {
      hidet.nodeValue = firstWord + hidet.nodeValue;
      showt.nodeValue = showt.nodeValue.substr(0,showt.nodeValue.length -
       firstWord.toString().length);
    }    
    
  },
  
  addEvent: function(obj, type, fn) {
    if ( obj.attachEvent ) {
      obj['e'+type+fn] = fn;
      obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
      obj.attachEvent( 'on'+type, obj[type+fn] );
    } else
      obj.addEventListener( type, fn, false );
  }
}
truncater.addEvent(window,"load",truncater.init);
truncater.addEvent(window,"resize",truncater.init);

