// Nasty kludgy Soft-hyphen hack
// v0.1 by yoz@yoz.com, 17/01/05

// 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


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);
//if(confirm(splitword))throw new Error("stop");
	
	// 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() {
	if (navigator.userAgent.indexOf("Gecko/") == -1)
		return;
	
	// 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
	var nbody = document.body.innerHTML.replace(wordre,replacer);
	if (!shyids.length)
		return;
	document.body.innerHTML = nbody;
	
	// 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.id = n;	
	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() {
	if (navigator.userAgent.indexOf("Gecko/") == -1)
		return;

	// to start: make all Shy objects visible
	for (i=0;i<shys.length;i++)
	{
		shys[i].style.display = "inline";
	}
	
	// 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";
		}else{
			document.getElementById("shy"+shys[i].id).innerHTML = '- ';
			if (shys[i].next.offsetTop == shys[i].prev.offsetTop)
			{
			shys[i].style.display = "none";
			}
		}
	}
}


