// Tibetan class
// author: Christopher E. Walker
// date: September 2005

/* 
methods:
=======

	toBreakingTsheg(string)
		method makes breaking tshegs with HTML wbr extension.
		if no argument is passed, method will act up on the elements
		in the document with attribute lang="bo" (true will be returned).
		Otherwise , the passed object should be a text string requiring
		conversion, and will be returned as such.
		
		*method requires library miscClasses.js which has
		the function getElementsByAttributeValue();
		
	getSelectTibetan()
		returns select box tags (as string) for choosing Tibetan fonts..........
		need to specify myObject.__nameOfInstance__ = "myObject" beforehand;
		can be added to document with innerHTML method.
		
	setTibetanFont(sFont)
		sets the Tibetan fonts of elements with attribute lang="bo" with
		variable sent as sFont.  If arguments pass is "user defined..."
		then the user will be prompted to provide name font.
	
	getTibetanNodes()
		returns array of nodes with lang="bo" in the document.
		Sets up an global array that queried at this time, to be used
		again whenever a change is applied to all Tibetan nodes
		in the document (assuming one hasn't added more nodes,
		which would require a new call to this method.
		
*/

function Tibetan() {
}
Tibetan.prototype.getTibetanNodes = function() {
	this.__tibetanNodes__ = getElementsByAttributeValue("lang", "bo");
	return this.__tibetanNodes__;
}

Tibetan.prototype.toBreakingTsheg = function (sConvertThis) {
	// var extraHack = "";
	// if (isIE) {extraHack = "&#xFEFF;"}; // will allow for distributed justification
	// var breakingTsheg = "་<wbr><span class=\"x\">"+extraHack+"</span></wbr>";
	var breakingTsheg = "་<wbr></wbr>";
	var findTsheg = /་(?!།)/g; // find a tsheg not followed by a shad THESE ONLY NOT WITHOT \uXXXX (?)
	// I had to do the first part with regex... have to account for ELEMENT spaces in possible string
	if (sConvertThis==undefined) {
		var bo = (this.__tibetanNodes__)?(this.__tibetanNodes__) : (getElementsByAttributeValue("lang", "bo"));
			for (var i = 0; i<bo.length; i++) {
				var notInElement = true;
				var oneBo = bo[i];
				var boLen = oneBo.innerHTML.length;
				var newHTML = "";
				for (var z=0; z<boLen; z++) {
					var boChar = oneBo.innerHTML.substr(z,1);
					if (boChar == " " && notInElement == true) {
						newHTML += "&nbsp;<wbr></wbr>";
					}
					else {
						if (boChar == "<") {
							notInElement = false;
						}
						if (boChar == ">") {
							notInElement = true;
						}
						newHTML += boChar;
					}
				}
			bo[i].innerHTML = newHTML;
			bo[i].innerHTML = bo[i].innerHTML.replace(findTsheg, breakingTsheg);
			}
		}
		
		// REDO THIS BOTTOM PART
	else {
			var sConverted = sConvertThis.replace(findTsheg, breakingTsheg);
			return sConverted;
		}
}

Tibetan.prototype.getSelectTibetan = function() {
	var f = new Array(); // f means font array
		f.push("Microsoft Himalaya");
		f.push("Tibetan Machine Uni");
		f.push("TCRC Youtso Unicode");
		f.push("XTashi");
		f.push("Wangdi29");
		f.push("Uchen_05");
		f.push("XenoType Tibetan"); // this will start the mac group
		f.push("XenoType TB dBuMed");
		
	var oc = "onchange=\"var t=this.options[this.selectedIndex].text; " +
						"if (this.selectedIndex!=0) " +
						"{myTibetan.setTibetanFont((t=='user agent default')?'':t)}; \"";           	
	var t = "<select "+oc+">";
			t += "<option selected=\"selected\">Select a Unicode Tibetan font</option>";
			// t += "<option>------------------</option>";
			for (var i=0; i<f.length; i++) {
				if (f[i]=="Microsoft Himalaya") t += "<optgroup label=\"Windows:\">";
				if (f[i]=="XenoType Tibetan") t += "</optgroup><optgroup label=\"Mac OS X (& Windows):\">";
				t += "<option>"+f[i]+"</option>";
			};
			t +="<optgroup label=\"More:\">";
				t +="<option>user agent default</option>";
				t +="<option>user defined...</option>";
			t +="</optgroup>";
		t += "</optgroup>";
	t += "</select>";
	return t;
}
Tibetan.prototype.setTibetanFont = function(sTibetanFont) {
	if (sTibetanFont=="user defined...") {
		var t = "Enter the exact name of the Unicode\nTibetan Font as recognized by your system";
		var df = "enter exact name here";
		sTibetanFont = prompt(t, df);
	}
	var bo = getElementsByAttributeValue("lang","bo");
				for (var i=0; i<bo.length; i++) {
					bo[i].style.fontFamily = sTibetanFont;
					//bo[i].style.lineHeight = "1.25em";
					//bo[i].style.fontSize = "2em";
					
					// below CSS specifically works in IE
					// bo[i].style.textAlign = "justify";
					// bo[i].style.textJustify = "newspaper";
					// abnormal tags:
					// these get converted to UPPERCASE!
				}
}

Tibetan.prototype.convert = function(inputText) {
	// make overall search reluctant, not greedy
	var here = "tsh|th|ng|gy|g|y";
	var here2 = "ཚ|ཐ|ང|གྱ|ག|ཡ";
	
	
	var aHere = here.split("|");
	var aHere2 = here2.split("|");
	var sCase = "";
	for (var i=0; i<aHere.length; i++) {
		sCase += "case \""+aHere[i]+"\": return \""+aHere2[i]+"\";\n";
	}
	
	document.getElementById("output").innerHTML = sCase;
	
	
	
	// myRegex_old = /( |[a\.]?(tsh|th|ng|gy|g|y)){1}?/g;
	myRegex = new RegExp("( |[a\\.]?("+here+")){1}?",  "g");
	var result = inputText.replace(myRegex, function(sMatch) {
							 
		
		//@JSD_LOG "a match found!: "+sMatch
		
		if (sMatch.substr(0,1)=="a" || sMatch.substr(0,1)==".") {
			sMatch = sMatch.slice(1);
		}
		switch(sMatch) {
			case "tsh":
				return "ཚ";
			case "th":
				return "ཐ";
			case "ng":
				return "ང";
			case "gy":
				return "གྱ";													
			case "g":
				return "ག";
			case "y":
				return "ཡ";
			case " ":
				return "་";
			/* case "ang":
				return "Aང";
			case "ath":
				return "Aཐ"; */
				
			default:
				return "match but no replace?";
		}
	})
	return result;
}
				
				