﻿//
// If making customizations to this file:
//
// -Make sure there is no other option than customizing this (there is nothing built in to handle the customization 
//     you are trying to do and there is no way to handle the customization somewhere else)
// -Be aware that many pages and applications may be referencing this and all changes to this will affect all of those pages
// -If there is absolutly no other possibility then make sure you test everything that could be using this to ensure your change doesnt break
//     those and document the customization for that site.
//
namespace("CareWorks.Utilities");

(function() {
	var $ns = CareWorks.Utilities;

	if(typeof($ns.TextResizer) == "undefined" || $ns.TextResizer == null) {
		$ns.TextResizer = function(parentContainerId, elementList, minFontSize, maxFontSize) {
			this.parentContainerId = parentContainerId;

			if(typeof(elementList) == "undefined" || elementList == null)
				this.elementList = ['table', 'tr', 'td', 'div', 'span', 'font', 'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
			else
				this.elementList = elementList;

			this.currentSizeModifier = 0;
			
			if(typeof(minFontSize) != "undefined" && minFontSize != null && parseInt(minFontSize, 10) > 0)
				this.minFontSize = parseInt(minFontSize, 10);
			else
				this.minFontSize = 1;
			
			if(typeof(maxFontSize) != "undefined" && maxFontSize != null)
				this.maxFontSize = parseInt(maxFontSize, 10);
			else
				this.maxFontSize = 30;
		}

		$ns.TextResizer.prototype = {
			parentContainerId: null,
			elementList: null,
			sizeList: null,
			initialIndex: null,
			currentIndex: null,
			currentSizeModifier: null,
			minReached: false,
			minFontSize: 1,
			maxFontSize: 30,

			doResize: function(multiplier) {
				if(this.elementList == null || this.elementList.length <= 0 || this.parentContainerId == null)
					return;

				var parentContainer = document.getElementById(this.parentContainerId);
				
				if(parentContainer == null)
					return;

				var originalModifier = this.currentSizeModifier;
				
				if(typeof(multiplier) == "undefined" || parseInt(multiplier, 10) == 0)
					this.currentSizeModifier = 0;
				else
					this.currentSizeModifier += parseInt(multiplier, 10);
				
				//
				// Perform an initial pass through the elements to make sure none go below or above the minimum/maximum font size
				//
				for(var i=0; i<this.elementList.length; i++) {
					var tags = parentContainer.getElementsByTagName(this.elementList[i]);

					for(var j=0; j<tags.length; j++) {
						var elem = tags[j];
						var currentFont = parseInt($(elem).css("font-size"), 10);

						//
						// Restore the original font
						//
						currentFont += (originalModifier * -1);

						//
						// Update with new modifier
						//
						currentFont += this.currentSizeModifier;
						
						if((multiplier < 0 && currentFont <= this.minFontSize) || (multiplier > 0 && currentFont >= this.maxFontSize)) {
							this.currentSizeModifier = originalModifier;
							return;
						}
					}
				}

				//
				// If the previous block didnt return, then it is safe to run the loop again and actually update the font sizes
				//
				for(var i=0; i<this.elementList.length; i++) {
					var tags = parentContainer.getElementsByTagName(this.elementList[i]);

					for(var j=0; j<tags.length; j++) {
						var elem = tags[j];
						var currentFont = parseInt($(elem).css("font-size"), 10);

						//
						// Restore the original font
						//
						currentFont += (originalModifier * -1);
						
						//
						// Update with new modifier
						//
						currentFont += this.currentSizeModifier;
						
						elem.style.fontSize = currentFont + "px";
					}
				}
			}
		};
	}
})();
