// Function:	RIcutwords()
// Use:		<body onload="RIcutwords();">
// Created By:	Richard M. <http://ri.ssxh.net/>
// Copyright:	No... You can't copyright JS. Get real, people.
// Version:	1..? Who versions JS? :-/
//
// Defaults:	len = 15;
//			tname = "span";
//			tclass = "trunc";
//			tooltip = true;
//			chr = "…";
//
// The function will run through every tag you specify with the tname
// variable, then, if it's class attribute matches the one you specify with
// tclass, then it will search though it's innerHTML for words longer than the
// value you specify in len. If it finds them, it will truncate them to len,
// and add chr onto the end. It will bypass HTML tags, but will affect the
// inside of them. Specifically, if you did an <a> tag with a URL:
// <a href="http://google.com">http://www.google.com/</a>
// and len was 10, then it would truncate the text inbetween the <a> tag to
// "http://www" while still retaining the <a> tag in full.
//
// If you specify the tooltip variable to true, then it will add a <span> tag
// around the replaced text with a title attribute saying what the truncated
// text was.

function RIcutwords() {
  len = 15;		// Change to the length desired for cutting.
  tname = "span";	// Change this to the tag name you will use.
  tclass = "trunc";	// Change this to the class="" attribute of the tags to be modified.
  tooltip = true;	// true for tooltip of what the word is; false for no tooltip.
  chr = "…";		// The character to add to the end. Alt + 0133 = … (three dots)

  if (!document.getElementsByTagName) return;
  z = document.getElementsByTagName(tname);

  for (i=0;i<z.length;i++) {
    if (z[i].className == tclass) {
      c = z[i].innerHTML;
      c = c.replace(/\</g," <").replace(/\>/g,"> ");
      d = c.split(" ");
      onhtml = false;
      for (g in d) {
        if (d[g].charAt(0) == "<" && !onhtml) onhtml = true;
        if (!onhtml) {
          if (d[g].length > len) {
            tt1 = " <span title=\""+d[g]+"\">";
            tt2 = "</span> ";
            if (!tooltip) {
              tt1 = "";
              tt2 = "";
            }
            c = c.replace(d[g],tt1+d[g].substr(0,len)+chr+tt2);
          }
        }
        if (d[g].charAt((d[g].length-1)) == ">" && onhtml) onhtml = false;
      }
      z[i].innerHTML = c.replace(/ </g,"<").replace(/> /g,">");
    }
  }
}