var Textsize = new Class({
	Implements: Options,
	
	options: {
		cookieName: "textsize",		
		cookieOptions: {
			domain: false,
			path: '/',
			duration: 30,
			secure: false
		},
		elements: $$(), // example: $$('.textSmall', '.textMedium', '.textLarge',)
		scale: ['100%', '110%', '120%', '130%'],
		targeted: document.id(document.body),
		defaultScale: 0,
		activeClass: "active"
	},
	curr: null,
	
	initialize: function(opts) {
		this.setOptions(opts);
		this.elements = $$(this.options.elements);		
		this.targeted = $$(this.options.targeted);		
		
		this.curr = Cookie.read(this.options.cookieName);
		if (this.curr === null) {			
			var scale = (this.options.defaultScale <= this.options.scale.length - 1) ? this.options.defaultScale + "" : "0";
			Cookie.write(this.options.cookieName, scale);
			this.curr = scale;
		}
		this.curr = parseInt(this.curr);

		this.setTextsize = (function(self) {
			if (self.elements.length === 2) return function(index) {
				var size = null;
				switch (index) {
					case 0:
					size = Math.max(this.curr - 1, 0);
					break;
					case 1:
					size = Math.min(this.curr + 1, this.options.scale.length - 1);
					break;
				}
				this.update(size);				
			};
			else if (self.elements.length === 3) return function(index) {
				this.update(index);
			};
			else return function() {};
		})(this);

		this.update(this.curr);
		this.attach();				
	},
	
	attach: function() {
		this.elements.each(function(el, i) {
			el.addEvent("click", function(e) {
				new Event(e).stop();
				this.setTextsize(i);
			}.bind(this));
		}, this);
		
		return this;
	},
	
	update: function(index) {		
		var opts = this.options,
			curr = this.curr,
			elements = this.elements;
		
		this.targeted.setStyle("fontSize", this.options.scale[index]);
		
		if (curr !== null) elements[curr].removeClass(opts.activeClass);
		elements[index].addClass(opts.activeClass);	
		
		Cookie.write(this.options.cookieName, index + "", this.options.cookieOptions);
		
		this.curr = index;		
	
		return this;
	},
	
	setTextsize: function() {}
	
});
