NewMind.ETWP.TextSizeSwitcher = function() {

	var $mainText;
	var currentSize;

	var SetTextSizeFromCookie = function() {
		//alert('font_size from cookie: ' + getCookie('font_size'));
		if (getCookie('font_size') !== '') {
			$mainText.css('font-size', getCookie('font_size'));
		}
	};

	var InitTextSizeSwitcher = function() {

		/**
		* Works out the new text size for a numerically based text size
		* @param {string} action
		*/
		var SwitchNumericTextSize = function(sAction) {
			// calculate text size
			if (sAction == 'larger') {
				num = num * 1.4;
			}
			else if (sAction == 'smaller') {
				num = num / 1.4;
			}

			SetTextSize(num + unit); //set new size

			return false;
		};

		/**
		* Works out the new text size for text based sizes
		*/
		var SwitchTextSize = function(sAction) {
			var newSize;
			currentSize = getCurrentFontSize();
			var sizes = ["xx-small", "x-small", "small", "medium", "large", "x-large", "xx-large"];
			var currentSizePosition = jQuery.inArray(currentSize, sizes); //find the position of our current size, not found = -1

			if (sAction == 'larger') {
				if (currentSizePosition < sizes.length) { //cant make it bigger if it's at its max
					newSize = sizes[currentSizePosition + 1];
				}
			}
			else if (sAction == 'smaller') {
				if (currentSizePosition > 0) { //cant make it smaller if it's at its min
					newSize = sizes[currentSizePosition - 1];
				}
			}
			SetTextSize(newSize);
			return false;
		};

		/**
		* Sets the new Text Size
		* @param {object} the new text size - could be in em's px etc
		*/
		var SetTextSize = function(newSize) {
			// set the font Size value of the mainText
			$mainText.css('font-size', newSize);
			// set text size cookie
			setCookie('font_size', newSize, 30);
		};

		/**
		* Gets the body font size, $maintext is declared in Init.
		* font size has a bug in IE so this value is calculated by creating an
		* span in the textswitcher with a width of 1EM, which will match the font size
		* and return it in pixels.
		* @return body font size in pixels
		**/
		var getCurrentFontSize = function() {
			// $maintext in IE gets an incorrect value for the font size of the body when % e.g. 100% is used
			// returning a value of IE6 : 1259px | IE7, IE8 : 1260px instead
			var txtSize;
			if (jQuery.browser.msie) {
				// Create an element with width 1em and grab its width in pixels - this will be the base size
				// for the body font. Has to be inserted direct into the body to ensure there's no styling
				// that will mess with the font size. After use, remove element immediately as it is no
				// longer required. Not using jQuery to add / remove as the version we're using (1.2.6
				// as of 2007-07-15) doesn't return a reference to the new element(s) on creation so
				// we'd have to mess around with giving it a temporary, unique id or classname.
				// - Happy to use jQuery to get pixel width cos it saves any messing about
				var objEleTemp = document.createElement("div");
				objEleTemp.style.width = "1em";
				document.body.appendChild(objEleTemp);
				txtSize = jQuery(objEleTemp).width() + "px";
				document.body.removeChild(objEleTemp);
			}
 			else {
 				txtSize = String($mainText.css('font-size'));
 			}
			return txtSize;
		};

		currentSize = getCurrentFontSize();

		// get unit type (px, em ,etc.)
		var unit;
		if (currentSize.indexOf("%") > 0) {
			unit = "%";
		}
		else {
			unit = currentSize.slice(-2);
		}

		switch (String(unit).toLowerCase()) { //as we may be dealing with em, px, pt, or even "small, large" etc we need to work out which mehtod to use
			case "em": case "px": case "pt": case "%":
				// parse the number value out of the font size value, set as a var called 'num'
				var num = parseFloat(currentSize, 10);
				// change text size on click
				jQuery('a.changer').click(function() {
					SwitchNumericTextSize(this.id);
				});
				break;

			case "ll": case "um": case "ge": //these are the last 2 characters in x-small -=> xx-large
				// change text size on click
				jQuery('a.changer').click(function() {
					SwitchTextSize(this.id);
				});
				break;

			default:
				//if its not recognised let's not error but do nothing
				if (typeof (console) !== "undefined") {
					console.warn("unrecognised unit type");
				}
				else {
					// alert("unknown unit type: " + unit); -re add for debugging
				}
				break;
		}

	};

	var setCookie = function(c_name, value, expiredays) {
		var exdate = new Date();
		exdate.setDate(exdate.getDate() + expiredays);
		document.cookie = c_name + '=' + escape(value) + ((expiredays === null) ? '' : ';expires=' + exdate.toGMTString());
	};

	var getCookie = function(c_name) {
		if (document.cookie.length > 0) {
			c_start = document.cookie.indexOf(c_name + '=');
			if (c_start != -1) {
				c_start = c_start + c_name.length + 1;
				c_end = document.cookie.indexOf(';', c_start);
				if (c_end == -1) {
					c_end = document.cookie.length;
				}
				return unescape(document.cookie.substring(c_start, c_end));
			}
		}
		return '';
	};

	return {

		Init: function() {

			//set the 'body' as $mainText
			$mainText = jQuery('body');
			//set default text size from cookie
			SetTextSizeFromCookie();
			//init text size switcher on click
			InitTextSizeSwitcher();
		}

	};

} ();