/** 
 *  @fileoverview TextResizeDetector
 * 
 *  Detects changes to font sizes when user changes browser settings
 *  <br>Fires a custom event with the following data:<br><br>
 * 	iBase  : base font size  	
 *	iDelta : difference in pixels from previous setting<br>
 *  	iSize  : size in pixel of text<br>
 *  
 *  * @author Lawrence Carvalho carvalho@uk.yahoo-inc.com
 * @version 1.0
 */

/**
 * @constructor
 */
 var isIE = (window.navigator.userAgent.indexOf("MSIE") > 0);

if (! isIE) {
	HTMLElement.prototype.__defineGetter__("innerText", function () { return(this.textContent); });
	HTMLElement.prototype.__defineSetter__("innerText", function (txt) { this.textContent = txt; });
}
TextResizeDetector = function() {
    var el = null;
	var iIntervalDelay  = 200;
	var iInterval = null;
	var iCurrSize = -1;
	var iBase = -1;
 	var aListeners = [];;
 	var createControlElement = function() {
	 	el = document.createElement('span');
		el.id='textResizeControl';
		el.innerText = "&#160;";
//		el.text='&#160;';
		el.style.position="absolute";
		el.style.left="-9999px";
		var elC = document.getElementById(TextResizeDetector.TARGET_ELEMENT_ID);
		// insert before firstChild
		if (elC)
			elC.insertBefore(el,elC.firstChild);
		iBase = iCurrSize = TextResizeDetector.getSize();
 	};

 	function _stopDetector() {
		window.clearInterval(iInterval);
		iInterval=null;
		};
	function _startDetector() {
		if (!iInterval) {
			iInterval = window.setInterval('TextResizeDetector.detect()',iIntervalDelay);
			}
		};
 	
 	 function _detect() {
 		var iNewSize = TextResizeDetector.getSize();
		
 		if(iNewSize!== iCurrSize) {
			for (var 	i=0;i<aListeners.length;i++) {
				aListnr = aListeners[i];
				var oArgs = {  iBase: iBase,iDelta:((iCurrSize!=-1) ? iNewSize - iCurrSize + 'px' : "0px"),iSize:iCurrSize = iNewSize};
				if (!aListnr.obj) {
					aListnr.fn('textSizeChanged',[oArgs]);
				}
				else  {
					aListnr.fn.apply(aListnr.obj,['textSizeChanged',[oArgs]]);
				}
			}

 		}
 		return iCurrSize;
 	};
	var onAvailable = function() {
		
		if (!TextResizeDetector.onAvailableCount_i ) {
			TextResizeDetector.onAvailableCount_i =0;
		}

		if (document.getElementById(TextResizeDetector.TARGET_ELEMENT_ID)) {
			TextResizeDetector.init();
			if (TextResizeDetector.USER_INIT_FUNC) {
				TextResizeDetector.USER_INIT_FUNC();
				}
			TextResizeDetector.onAvailableCount_i = null;
			}
		else {
			if (TextResizeDetector.onAvailableCount_i<600) {
	  	 	    TextResizeDetector.onAvailableCount_i++;
				setTimeout(onAvailable,200);
			}
		}
	};
	setTimeout(onAvailable,500);

 	return {
		 	/*
		 	 * Initializes the detector
		 	 * 
		 	 * @param {String} sId The id of the element in which to create the control element
		 	 */
		 	init: function() {
		 		createControlElement();		
				_startDetector();
 			},
			/**
			 * Adds listeners to the ontextsizechange event. 
			 * Returns the base font size
			 * 
			 */
 			addEventListener:function(fn,obj,bScope) {
				aListeners[aListeners.length] = {
					fn: fn,
					obj: obj
				}
				return iBase;
			},
			/**
			 * performs the detection and fires textSizeChanged event
			 * @return the current font size
			 * @type {integer}
			 */
 			detect:function() {
 				return _detect();
 			},
 			/**
 			 * Returns the height of the control element
 			 * 
			 * @return the current height of control element
			 * @type {integer}
 			 */
 			getSize:function() {
	 				var iSize;
			 		return el.offsetHeight;
		 		
		 		
 			},
 			/**
 			 * Stops the detector
 			 */
 			stopDetector:function() {
				return _stopDetector();
			},
			/*
			 * Starts the detector
			 */
 			startDetector:function() {
				return _startDetector();
			}
 	}
 }();

TextResizeDetector.TARGET_ELEMENT_ID = 'doc';
TextResizeDetector.USER_INIT_FUNC = null;


		function editText(pTags,numChr,origTxt,newTxt) {
			for (x=0;x<pTags.length;x++) {										// for each <p> tag...
				origTxtChars = origTxt[x].split("");							// split the string into an array of individual characters
				for (i=0;i<numChr[x];i++) {									// character by character, add from the old string to the new string until we reach the limit
					if (i<origTxtChars.length) {								// stop adding characters if the number of orig characters is less than max length
						newTxt[x] += origTxtChars[i];
						}
					}
				var lastBit = '';												// variable to store the remainder of any word before the last space
				for (i=numChr[x];i<origTxt[x].length;i++) {						// starting with where we left off, append chars to lastBit until a space is found
					if (origTxtChars[i] != " ") {								// if the char is not a space, add it to lastBit
						lastBit += origTxtChars[i];
						}
					else break;													// break out of the for loop once a space is found
					}
				newTxt[x] = newTxt[x] + lastBit + "...";						// concatenate together
				var pTagsID = pTags[x].id;										// get the ID for this <p> tag
				document.getElementById(pTagsID).innerHTML = newTxt[x];			// write the new text to the inner HTML
				}
			}

    	function switchFS(fontSize) {
			switch(fontSize) {
				case 10:
					numChars = 300;
					break;
				case 11:
					numChars = 240;
					break;
				case 12:
					numChars = 140;
					break;
				case 13:
					numChars = 132;
					break;
				case 14:
					numChars = 108;
					break;
				case 15:
					numChars = 88;
					break;
				case 17:
					numChars = 72;
					break;
				case 18:
					numChars = 40;
					break;
				case 20:
					numChars = 32;
					break;
				case 21:
					numChars = 28;
					break;
				case 22:
					numChars = 24;
					break;
				case 25:
					numChars = 18;
					break;
				case 29:
					numChars = 12;
					break;
				default:
					numChars = 64;
					break;
				}
			return numChars;
			}

    	function switchFS5(fontSize) {
			switch(fontSize) {
				case 10:
					numChars = 900;
					break;
				case 11:
					numChars = 750;
					break;
				case 12:
					numChars = 600;
					break;
				case 13:
					numChars = 492;
					break;
				case 14:
					numChars = 384;
					break;
				case 15:
					numChars = 324;
					break;
				case 17:
					numChars = 216;
					break;
				case 18:
					numChars = 120;
					break;
				case 20:
					numChars = 96;
					break;
				case 21:
					numChars = 84;
					break;
				case 22:
					numChars = 72;
					break;
				case 25:
					numChars = 54;
					break;
				case 29:
					numChars = 36;
					break;
				default:
					numChars = 192;
					break;
				}
			return numChars;
			}