/***************************************************************
 * Copyright notice
 *
 * (c) 2009 Oliver Hader <oliver@typo3.org>
 * All rights reserved
 *
 * This script is part of the TYPO3 project. The TYPO3 project is
 * free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * The GNU General Public License can be found at
 * http://www.gnu.org/copyleft/gpl.html.
 *
 * This script is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * This copyright notice MUST APPEAR in all copies of the script!
 ***************************************************************/

var tx_fontsize = Class.create({
	isInitialized: false,
	currentDeviation: 0,
	options: {},
	initialStyles: {},
	cookies: {},

	initialize: function(options) {
		this.options = options;

		if (options.initialDeviation) {
			this.changeFontsize(options.initialDeviation, true);
		}

		try {
			Event.observe($$('.tx-fontsize a.smaller').first(), 'click', this.smallerFontsizeAction.bind(this));
			Event.observe($$('.tx-fontsize a.normal').first(), 'click', this.normalFontsizeAction.bind(this));
			Event.observe($$('.tx-fontsize a.bigger').first(), 'click', this.biggerFontsizeAction.bind(this));
		} catch (exception) { }

		this.isInitialized = true;
	},

	isFontsizeChangePossible: function(deviation) {
		var possibleDeviation = this.currentDeviation + deviation;
		return (this.currentDeviation && !deviation || this.currentDeviation != possibleDeviation && possibleDeviation > -3 && possibleDeviation < 3);
	},

	changeFontsize: function(deviation, force) {
		if (this.isFontsizeChangePossible(deviation) || force) {
			this.currentDeviation = (deviation ? this.currentDeviation + deviation : deviation);
			$$(this.options.applyChangeSelector).each(this.applyFontsizeChange.bind(this));

			if (this.isInitialized) {
				this.updateSessionData();
			}
		}
		return false;
	},

	applyFontsizeChange: function(element) {
		var styles;
		if (!this.initialStyles[element]) {
			styles = {
				fontSize: element.getStyle('fontSize'),
				lineHeight: element.getStyle('lineHeight')
			};
			this.initialStyles[element] = styles;
		} else {
			styles = this.initialStyles[element];
		}
		if (styles.fontSize) {
			if (this.currentDeviation) {
				this.applyStyleChangeOnElement(element, styles.fontSize, 'fontSize');
				this.applyStyleChangeOnElement(element, styles.lineHeight, 'lineHeight');
			} else {
				if (this.initialStyles[element]) {
					this.applyStyleChangeOnElement(element, this.initialStyles[element].fontSize, 'fontSize', true);
					this.applyStyleChangeOnElement(element, this.initialStyles[element].lineHeight, 'lineHeight', true);
				}
			}
		}
	},

	applyStyleChangeOnElement: function(element, value, styleName) {
		if (value && !isNaN(parseFloat(value))) {
			styles = {};
			value = parseFloat(value) * (1 + this.currentDeviation * 0.2);
			styles[styleName] = value + 'px';
			element.setStyle(styles);
		}
	},

	smallerFontsizeAction: function(event) {
		event.stop();
		return this.changeFontsize(-1);
	},

	normalFontsizeAction: function(event) {
		event.stop();
		return this.changeFontsize(0);
	},

	biggerFontsizeAction: function(event) {
		event.stop();
		return this.changeFontsize(1);
	},

	updateSessionData: function() {
		new Ajax.Request(
			this.options.eidUrl,
			{
				method: 'post',
				parameters: '&deviation=' + this.currentDeviation,
				onFailure: this.handleAjaxResponse.bind(this)
			}
		);
	},

	handleAjaxResponse: function(response) {
		// alert(response.responseText);
	}
});