// Nasty kludgy Soft-hyphen hack
// v0.1 by yoz@yoz.com, 17/01/05
// From http://cheerleader.yoz.com/a/20050118shy/shy.html

// This code can be added to any HTML page to provide better soft hyphen
// support for Mozilla-based browsers.
// doShyTags() should be called on page load, and shyreflow() on any
// occasion where reflow is needed (such as the onresize event)
//
// At present, soft hyphens are only dealt with when surrounded by
// characters matched by the "\w" regexp character class.
// If you'd like to fix this, please edit the regexp at the start
// of doShyTags() and send me your edits

addEvent(window, "load", writecomments);
addEvent(window, "load", loadfn);
addEvent(window, "resize", resizefn);

function writecomments() {
  if (document.getElementById("comments_form")) {
    document.getElementById("comments_form").action = "http://www.bentbacktulips.co.uk/cgi-bin/mt/mtfeedback.cgi";
  }
}

function loadfn() {
	doShyTags();
	shyreflow();
}

function resizefn() {
	shyreflow();
}


var doneshytags = false; // set to true once we've rebuilt the HTML
var counter = 0; // used for generating numeric IDs
var shys = new Array(); // used for storing refs to our Shy objects
var shyids = new Array(); // used for storing num IDs to Shy elements

// replacer() is called by the replace call in doShyTags()
// it takes words that use &shy; and breaks them into a number of SPAN
// elements, while storing the IDs of the shy elements
function replacer(s, n, full) {

	// split on &shy;
	var splitword = s.split(/\xAD/i);

	
	// now wrap everything in new SPAN elements, keeping track of 
	// used IDs
	var newword = "";
	for (i=0;i<splitword.length;i++)
	{
		if (newword != "") // if we're in the middle of a word
		{
			// add a shy element
			newword += "<span id='shy"+(++counter)+"'>- </span>";
			shyids.push(counter);
		}
		
		newword += "<span id='shy"+ (++counter) + "'>" + splitword[i] + "</span>";
	}

	return newword;	
	
}



// doShyTags() locates all &shy; entities and breaks the surrounding word into
// a set of separated referenceable elements 
// then creates and stores Shy objects for each &shy; element

function doShyTags() {
	
	// NOTE: ANY ADDED SUPPORT FOR FOREIGN CHARACTERS SHOULD GO
	// IN THE REGEXP BELOW, ADDED TO BOTH "[..]" CLASSES
       var wordre = /[\w]+\xAD[\w\xAD]+/gi; // RE to match words with &shy;


	
	// replace all words using &shy; with munged equivalents
	
       document.body.innerHTML = document.body.innerHTML.replace(wordre,replacer);


	
	// create Shy objects for each shy element we created
	for(i=0;i<shyids.length;i++)
	{
		shys.push(new Shy(shyids[i]));
	}
	
	doneshytags = true;
	
	// and we're done
	
}

// constructor for Shy object.
// Is given a numeric ID $n, looks for shy element named "shy$x" in the document
// then creates an object referencing that element's style object and the
// style objects of the text elements on either side

function Shy(n) {
	this.style = document.getElementById("shy"+n).style;
	this.next = document.getElementById("shy"+(n+1));
	this.prev = document.getElementById("shy"+(n-1));
	return this;
}

// shyreflow() - goes through the document checking and setting visibilities
// on shy elements. It does this by checking whether the text nodes before and
// after are on the same line; if so, the shy should not be visible

function shyreflow() {
	
	// to start: make all Shy objects visible
	for (i=0;i<shys.length;i++)
	{
		shys[i].style.display = "";
	}
	
	// now check heights of text elements on either side. If the same,
	// make the shy invisible
	for (i=0;i<shys.length;i++)
	{
		
		//alert("Left: "+ shys[i].prev.offsetTop +
		//			", right:" + shys[i].next.offsetTop);
			
		if (shys[i].next.offsetTop == shys[i].prev.offsetTop)
		{
			
			shys[i].style.display = "none";
		}
	}
}


function addEvent(elm, evType, fn, useCapture)
// addEvent and removeEvent
// cross-browser event handling for IE5+,  NS6 and Mozilla
// By Scott Andrew
{
  if (elm.addEventListener){
    elm.addEventListener(evType, fn, useCapture);
    return true;
  } else if (elm.attachEvent){
    var r = elm.attachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
} 
