/**
 * Text Resizzle	
 * For resizing text by attaching specific classes to a parent element. 
 * For more information, demos etc, see the page in confluence. 
 * @author Henry Singleton
 * @date Nov 3 2010
 */

var textResizzle = new Class({
	
	Binds: ['increaseSize','decreaseSize'], // happy, magic way of always binding certain functions to this, no matter the current context
	Implements: [Events,Options],
	options: {
		parent: 'body',				// select the parent to apply the size classes to
		increaseSizeSelector: '.text-increase', // select element to use for increase font size button or link. 
		decreaseSizeSelector: '.text-decrease', // select element to use for decrease font size button or link. 
		sizes: [					// specify an array of the sizes. These will be applied as class names to the parent element (selected above) to allow you to overide layout and font sizes. 
			'smallest',
			'small',
			'normal',
			'large',
			'largest'
		],
		defaultSize: 'normal', 		// specify which size above is the default. 
		saveCookie: true,			// choose weather to save the selected font size in a cookie
		cookieName: 'textResizzle'
	},

	
	initialize: function(options){
		this.currentSize = 'normal';
		
		//set up the options (combines default options above with options)
		this.setOptions(options);
		
		//set up our container
		this.parent = $$(this.options.parent);
		
		// set up the increase and decrease selectors
		this.increaseSizeLink = $$(this.options.increaseSizeSelector);
		this.increaseSizeLink.addEvent('click',this.increaseSize);
		
		this.decreaseSizeLink = $$(this.options.decreaseSizeSelector);
		this.decreaseSizeLink.addEvent('click',this.decreaseSize);
		
		//console.log('something');
		
		//set the size to whatever is stored in the current cookie
		if (Cookie.read(this.options.cookieName)) {
			this.setSize(Cookie.read(this.options.cookieName));
		}
		
	},
	
	increaseSize: function() {
		
		//get the next size up index
		var nextSize = this.options.sizes.indexOf(this.currentSize) + 1;

		//check if we have reached the top of the sizing.
		if (nextSize >= this.options.sizes.length) {
			return false;
		}
		
		//set the new size
		this.setSize(this.options.sizes[nextSize]);
		
		return false;
	},

	decreaseSize: function() {
		//get the next size up index
		var previousSize = this.options.sizes.indexOf(this.currentSize) - 1;

		//check if we have reached the top of the sizing.
		if (previousSize < 0) {
			return false;
		}
		
		//set the new size
		this.setSize(this.options.sizes[previousSize]);
		
		return false;

	},
	
	//explicitly set the size. Will normally only be called internally. 
	setSize: function(size) {
		
		if (typeof size == undefined) {
			size = this.options.defaultSize;
		}
		
		//clear any existing sizes that may be set
		this.clearSize();
		
		//set the new size
		this.parent.addClass(size);
		
		//write the new size to the current object
		this.currentSize = size;
		
		//write the change to the cookie
		if (this.options.saveCookie) {
			Cookie.write(this.options.cookieName, size, {path: '/'});
		}
	},
	
	//reset the size to the default
	clearSize: function() {
		//go through each size, and remove the class from the parent element.
		this.options.sizes.each(function(size) {
			this.parent.removeClass(size);
		}.bind(this));
	}
	
});
