/**
 * Controls the accessibilty controls on teh page. This includes the text size controls and print button.
 *
 * Assumes that the main content div #content exists and that it has a p element with font-size property, won't stricly break assume that the default
 * font size is 12px (or 1.2em at body font size of 0.625em.
 *
 * Note: below properties are all optional as they have defaults set.
 *
 * This script can be used as a standalone but it is best used as part of teh cmslibrary in conjunction with /HTMLElements/HTMLTextControl.php
 *
 * The css file textControl.css is required for this class to work.
 *
 * @author Luke Hoggett <luke@fatpublisher.com.au>
 * @property float emModifier - amount the font size increases/decreases by when the text controls are clicked on
 * @property int stepsUp - number of increments the font size can be increased by
 * @property int stepsDown - number of increments the font size can be decreased by
 * @property int cookieExpires - number days the cookie is live for
 */

TextControl = new Class({
	// implements
	Implements: [Options],

	// options
	options: {
		emModifier: 0.10,
		stepsUp: 5,
		stepsDown: 2,
		cookieExpires: 30,
		defaultSizeElement: 'p'
	},
	// class variables
	currentStep: 0,
	defaultSize: 12,
	currentSize: 0,
	maxSize: 0,
	minSize: 0,

	// initialisation
	initialize: function(options){
		// set.options
		this.setOptions(options);
		this._configure();
	},

	_configure: function() {
		this._attachEvents();
		this._setDefaultSize();
		this._setMaxSize();
		this._setMinSize();
		this.run();
	},

	run: function() {
		var cookieVal = Cookie.read('emSize');
		if(cookieVal != null) {
			if(cookieVal >= this.minSize && cookieVal <= this.maxSize) {
				this.currentSize = cookieVal;
				this._changeTextSizeEM();
			} else {
				Cookie.dispose('emSize');
				this.currentSize = this.defaultSize;
			}
		} else {
			this.currentSize = this.defaultSize;
		}
	},

	printPage: function() {
		window.print();
	},

	increaseText: function() {
		if(this.currentSize < this.maxSize) {
			this.currentSize++;
			this._changeTextSizeEM();
		}
	},

	decreaseText: function() {
		if(this.currentSize > this.minSize) {
			this.currentSize--;
			this._changeTextSizeEM();
		}
	},

	resetText: function() {
		this.currentSize = this.minimumSize;
		this._changeTextSizeEM(this.minimumSize);
	},

	/**
	 * get the default font size for the content areas
	 */
	_setDefaultSize: function () {
		if (this.options.defaultSizeElement == null) {
			this.defaultSize = $('content').getElement('p').getStyle('font-size');
		} else {
			this.defaultSize = $('content').getElement(this.options.defaultSizeElement).getStyle('font-size');
		}
		if (this.defaultSize.indexOf('em') == -1) {
			this.defaultSize = this.defaultSize.toInt();
		} else {
			// get parent wrapper size
			var size = $('content').getElement('p').getStyle('font-size').toFloat();
			// get p size
			var pSize = this.defaultSize.toFloat();
			// calculate the percentage from the parent to the p
			this.defaultSize = size * pSize * 10;
		}
	},

	/**
	 *
	 */
	_changeTextSizeEM: function() {
		var self = this;
		var size = (this.currentSize).toInt();
		$$('body').removeProperty('class').addClass('textModifier' + size);
		Cookie.write('emSize', size, {path: '/', duration: self.cookieExpires});
		return;
	},
	/**
	 * add the events to the text control buttons
	 */
	_attachEvents: function() {

		var self = this;
		$$('#contentControl button.print').addEvent('click', function() {
			self.printPage();
		});
		$$('#contentControl button.decrease').addEvent('click', function() {
			self.decreaseText();
		});
		$$('#contentControl button.increase').addEvent('click', function() {
			self.increaseText();
		});
		$$('#contentControl button.reset').addEvent('click', function() {
			self.resetText();
		});
	},

	_setMaxSize: function() {
		this.maxSize = (this.defaultSize + this.options.stepsUp).toInt();
	},
	_setMinSize: function() {
		this.minSize = (this.defaultSize - this.options.stepsDown).toInt();
	}

});
