//
// OptionalBreakMarker
//
// A utility class for indicating optional breaks in long strings such as URLs,
// so that a browser can wrap these within narrow text containers. 
// 
// Version: 1.0
// Author: Chris DiGiano
// Date: 28 July 2004
//
// Installation:
// Because of dependencies (described below), here's how you should load the
// class before using it in an HTML file:
// <script language="JavaScript" src="browser_detect.js"></script>
// <script language="JavaScript1.2" src="OptionalBreakMarker.js"></script>
//
// Usage:
// Create an instance of the OptionalBreakMarker class, then call the method
// "mark" on a string. This will return a new string with the appropriate html
// tag inserted into reasonable places for wrapping the string.
//
// Example:
// marker = new OptionalBreakMarker()
// marker.mark("http://verylongurl.com/whenisthis/goingtoend")
// Result>> http:<wbr>/<wbr>/<wbr>verylongurl.<wbr>com/<wbr>whenisthis/<wbr>goingtoend
//
// Properties:
// longestAlphanumericRun - how long before a break is marked between a string
//                          of alphanumeric characters (defaults to 10)
//
// Dependencies:
// This class depends on regular expression searching introduced
// in JavaScript 1.2 and the excellent browser_detect script found here
// http://www.dithered.com/javascript/browser_detect/index.html
//
// For details on indicating optional breaks, see the wonderfully thorough
// article here:
// http://www.cs.tut.fi/~jkorpela/html/nobr.html#suggest

// OptionalBreakMarker.mark
function mark(str) {
  if (browser.isIE5x) {
		return "<nobr>" + this._mark(str) + "</nobr>";
	}
	else {
		return this._mark(str);
	}
}

// private OptionalBreakMarker._mark
// This method does all the real work by recursing over
// the string until there are no more good places to 
// break and the string is 10 or fewer characters.
function _mark(str) {
  // If there's a non-alphabetic character, mark a break just
	// after it and recurse with the rest.
  var nonAlphaPos = str.search(/\W/);
	if (nonAlphaPos != -1) {
		return str.substring(0, nonAlphaPos+1).concat(
				this.optionalBreak.concat(this.mark(str.substring(nonAlphaPos+1))));
	}
	// If the string is still long, mark a break in the middle
	// and recurse on either end.
  if (str.length > this.longestAlphanumericRun) {
	  var midPos = Math.round(str.length / 2);
		return this.mark(str.substring(0, midPos)).concat(
				this.optionalBreak.concat(this.mark(str.substring(midPos))));
	}
	return str; 
}

// OptionalBreakMarker constructor
function OptionalBreakMarker() {
	// Initialize properties
	this.longestAlphanumericRun = 10;
	if (browser.isSafari) {
		this.optionalBreak = "&#8203;"
	}
	else {
		this.optionalBreak = "<wbr>";
	}
	// Link methods
	this.mark = mark;
	this._mark = _mark;
}
