////////////////////////////////////////////////////
// Namespace creation

// Create the global symbol "com" if it doesn't exist
// Throw an error if it does exist but is not an object

/**
var com;
if (!com) com = {};
else if (typeof com != "object")
    throw new Error("com already exists and is not an object");

// Repeat the creation and type-checking code for the next level
if (!com.tp) com.tp = {}
else if (typeof com.tp != "object")
    throw new Error("com.tp already exists and is not an object");

// Throw an error if com.tp.User already exists
if (com.tp.User)
    throw new Error("com.tp.User already exists");
*/

// Otherwise, create and populate the namespace with one big object literal
com.tp.User = {}; 


////////////////////////////////////////////////////////
// Public Members

com.tp.User.getBrowserKnowsToBreakAtTsheg = function() {
	// read-only
	var User = com.tp.User;
	if (User._browserKnowsToBreakAtTsheg === undefined) {
		User._browserKnowsToBreakAtTsheg = User._checkBrowserKnowsToBreakAtTsheg();
	}
	return User._browserKnowsToBreakAtTsheg;
};

com.tp.User.getBrowserBreaksUCStibetanJustification = function() {
	// read-only
	var User = com.tp.User;
	if (User._browserBreaksUCStibetanJustification === undefined) {
		User._browserBreaksUCStibetanJustification = User._checkBrowserBreaksUCStibetanJustification();
	}
	return User._browserBreaksUCStibetanJustification;
};

com.tp.User.getBrowserCanEmbedFonts = function() {
	// read-only
	var User = com.tp.User;
	if (User._browserCanEmbedFonts === undefined) {
		User._browserCanEmbedFonts = User._checkBrowserCanEmbedFonts();
	}
	return User._browserCanEmbedFonts;
};
	
	
////////////////////////////////////////////////////////
// Private Members

com.tp.User._checkBrowserCanEmbedFonts = function() {
	// just check if IE
	var sUserAgent = navigator.userAgent;
	if ((sUserAgent.indexOf("compatible") > -1) && (sUserAgent.indexOf("MSIE") > -1)) {
		return true;
	}
	/* Safari can embed, but not Tibetan Unicode stuff, can't figure out why
	if ((sUserAgent.indexOf("AppleWebKit") > -1) && (sUserAgent.indexOf("3.1") > -1)) {
		return true;
	} */
	return false;
};

com.tp.User._checkBrowserKnowsToBreakAtTsheg = function() {
	// first, check if testing DIV element exists,
	// if not, create one in document
	if (!document.getElementById('tp_tshegCheck')) {
		var oDiv = document.createElement('div');
		oDiv.setAttribute('id','tp_tshegCheck');
		document.body.appendChild(oDiv);
	}
	// variables:
	var eWriteDiv = document.getElementById('tp_tshegCheck');        
	var oSpan = []; // will hold created SPAN elements
	var aTshegIds = ['tp_tshegCheckControl','tp_tshegCheckTest'];
	var tshegChar = "\u0f40\u0f0b"; // tibetan ka plus tsheg
	// create two new SPAN elements within that testing DIV
	for (var i=0; i<aTshegIds.length; i++) {
		oSpan[i] = document.createElement('span');
		oSpan[i].id = aTshegIds[i];
		oSpan[i].lang = 'bo'; // indicate to browser that this is Tibetan
		oSpan[i].style.fontSize = '24pt';
		oSpan[i].style.display = 'block';
		oSpan[i].style.position = 'absolute';
		oSpan[i].style.width = '190px';
		oSpan[i].style.left = '-200px'; // hide from screen
		oSpan[i].style.top = '-' + ((i+1)*(200)) + 'px'; // hide
		oSpan[i].style.backgroundColor = ((i===0)?'red':'green');		
		// create a long string of 50+ tshegs if creating 2nd box
		if (aTshegIds[i]=='tp_tshegCheckTest') {
			for (var i2=0; i2<50; i2++) {
				tshegChar += "\u0f40\u0f0b";
			}
		}
		oSpan[i].appendChild(document.createTextNode(tshegChar));
		// add or replace SPAN node as needed
		if (document.getElementById(aTshegIds[i])) {
			eWriteDiv.replaceChild(oSpan[i], document.getElementById(aTshegIds[i]));
		}
		else { eWriteDiv.appendChild(oSpan[i]); }     
	}
	// retrieve the heights of the two boxes
	var nHeightControl = document.getElementById('tp_tshegCheckControl').offsetHeight;
	var nHeightTest = document.getElementById('tp_tshegCheckTest').offsetHeight;    
	// if test box appears about twice the height of the control box or taller,
	// then browser is line breaking at tsheg, so return TRUE
	// console.log("_checkBrowserKnowsToBreakAtTsheg = " + (nHeightTest >= (2*nHeightControl)));
	var twiceSize = (2*nHeightControl);
	var result = (nHeightTest >= twiceSize);
	return result;
};

com.tp.User._checkBrowserBreaksUCStibetanJustification = function() {
	// SNIFFING MOZ FOR WINDOWS (the only one who messes up)
	var sUserAgent = navigator.userAgent;
	//var isWin = (navigator.platform == "Win32") || (navigator.platform == "Windows");
	var isKHTML = sUserAgent.indexOf("KHTML") > -1 || sUserAgent.indexOf("Konqueror") > -1 || sUserAgent.indexOf("AppleWebKit") > -1; 
	var isMoz = sUserAgent.indexOf("Gecko") > -1 && !isKHTML;
	// both Win and OSX have weird justify problems with Tibetan.  Firefox 3 is leaving gaps inbetween.
	// LATER MAKE THIS SNIFF FOR VERSION SO IF BUG FIXED I CAN SEND FALSE; // CHECK LINUX
	if (isMoz) {
		return true;
	}
	return false;
};

com.tp.User._checkBrowersDoesDOMStylesheets = function() {
	// any Firefox
	var s = navigator.userAgent;
	var isKHTML = s.indexOf("KHTML") > -1 || s.indexOf("Konqueror") > -1 || s.indexOf("AppleWebKit") > -1; 
	var isMoz = s.indexOf("Gecko") > -1 && !isKHTML;
	if (isMoz) {
		return true;
	}
	return false;
};

