// Nasty kludgy Soft-hyphen hack
// v0.1 by yoz@yoz.com, 17/01/05 
// some  unnecessary comments removed by H. Jussila webmaster@musignanodianreoli.it 
// added also a small "user / useage / installation manual 
// and the script has been compacted by taking away empty lines
// user/installation manual - sort of: 
// link this file hyphen.js on the <head></<head> section of you html code as follows:
// <script type="text/javascript" src="<path here>/hyphen.js></script>
// add the following onload="doShyTags(); shyreflow();" or replace your <body> tag with the following
// <body onload="doShyTags(); shyreflow()" additional class or id styles after the javascript>

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; breaking them into 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 creating and storing Shy objects for &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, and it looks for shy 
// element named "shy$x" in the document creating an object that references to 
// the 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() checks the document and sets the visibility 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";
		}
	}
}
