// this little bit of weirdness creates an isolated execution context
// in short, it allows this piece of code to run and NOT interfere with
// any other namespaces that might be available.
(function () {
	var TextSize = new Class({	
		bodyNode : null,   // this is a reference to the body element
		textTools : null,  // this is an array of references to the text sizing anchor elements
		isLarge : null,    // a switch to tell us whether we are showing large or regular sized text
		
		/**
		 * initialize - Constructor
		 *
		 * This method constructs the class, initializes all the variables and attaches the appropriate events.
		 * This method also attaches an unload event to the window object so that every time the page is refreshed
		 * or even closed, the application writes a cookie which stores the current text size for a year allowing
		 * a user to return to this page and find it in the same state as when they were last here.
		 **/
		initialize : function () {
			var thisRef = this;  // fix the 'loss of scope' problem
			
			this.textTools = $$('a.smaller,a.larger');  // collect the anchors
			
			this.bodyNode = $$('body');        // grab the body element
			this.bodyNode = this.bodyNode[0];  // and store an immediate reference to it
			
			var size = Cookie.get('font_size'); // get the current font size (if its available)
			
			// supposed to be large so set everything up appropriately
			if(size==='large' && !this.bodyNode.hasClass('larger')) {
				/*this.bodyNode.addClass('larger');
				this.textTools[0].removeClass('active');
				this.textTools[1].addClass('active');*/
				this.changeSize(this.textTools[1]);
			}
			
			this.isLarge = this.bodyNode.hasClass('larger');  // set the flag
			
			// attach the events
			$('text_tools').addEvent('click',this.onClick.bindAsEventListener(thisRef));
			window.addEvent('unload',this.storeCookie.bindAsEventListener(thisRef));
		},
		
		/**
		 * onClick - Event Delegator
		 *
		 * This method simply handles the click and delegates the response to the appropriate method, in
		 * this case it just passes the target element off to the changeSize method.
		 **/
		onClick : function (event) {
			event = new Event(event);  // create a MooTools Event object from the native event object
			event.stop();              // stop the default event action (don't follow the link when clicked)
			
			this.changeSize(event.target);  // change the font size using the appropriate target
		},
		
		/**
		 * changeSize - Event Handler
		 *
		 * This method simply toggles class names on the text size links AND the body element.
		 **/
		changeSize : function (target) {
			var larger = target==this.textTools[1];  // is this the larger link or the regular link
			
			if((larger && this.isLarge) ||   // see if we actually need to do anything
			  (!larger && !this.isLarge))
				return;
			
			// simply switch all the appropriate classes on/off on the appropriate elements
			// and set the flag to the appropriate value
			this.textTools[0].toggleClass('active');
			this.textTools[1].toggleClass('active');
			this.bodyNode.toggleClass('larger');
			this.isLarge = larger;
		},
		
		/**
		 * storeCookie - Utility Method
		 *
		 * This method simply stores the appropriate information in a cookie.
		 **/
		storeCookie : function () {
			var value = this.isLarge ? 'large' : 'regular';
			var options = {
				duration : 365,
				path : document.domain=='dev.blacksheep.net.nz' ? '/domains/roseklens.com/public_html/' : '/',
				domain : document.domain,
				secure : false
			};
			
			Cookie.set('font_size',value,options);
		}
	});

	window.addEvent('load',function () { var textsize=new TextSize(); }); // create a TextSize instance when the page loads and we're in business :o)
})();