/*
Script:
	fontSizer.js, <http://files.justinmaier.com/
Author:
	Justin Maier, <http://justinmaier.com>

License:
	MIT-style license.
*/
var fontSizer = new Class({
	getOptions: function(){
		return {
			onSelect: function(el){
				//function to occur once a font has been selected. el is the element that has been editted.
			},
			className: 'fontSizer',
			fade: true
		};
	},
	
	initialize: function(elements, options){
		this.elements = elements;
		this.setOptions(this.getOptions(), options);
		this.container = new Element('div').addClass(this.options.className).setStyles('width:290px;height:30px;position:absolute;background:#fff;z-index:1000;border:1px solid #999;').setOpacity(0).injectInside(document.body);
		this.slideBox = new Element('div').setStyles('width:230px;margin:5px 0 0 5px;height:20px;border:1px solid #999;z-index:1002;').addClass(this.options.className+'-sBox').injectInside(this.container);
		this.knob = new Element('div').setStyles('cursor:pointer;width:3px;height:20px;background:#0099ff;z-index:1003;').addClass(this.options.className+'-sKnob').injectInside(this.slideBox);
		this.curVal = new Element('input').setStyles('display:block;position:absolute;left:235px;top:5px;height:20px;width:26px;border:1px solid #999;color:#666;font-size:16px;padding:0 3px 0 2px;z-index:1001;').addClass(this.options.className+'-curVal').injectInside(this.container);
		this.closeSelector = new Element('div').setStyles('padding:0;margin:0;color:#999;cursor:pointer;width:20px;height:22px;position:absolute;z-index:1001;top:2px;left:273px;font-size:20px;').appendText('X').injectInside(this.container);
		$each(elements, function(el){
			el.addEvent('click', function(event){
				this.show(el,event);
			}.bindWithEvent(this));
		}, this);
		this.slider = new Slider(this.slideBox,this.knob,{steps:100,wheel:true,onComplete:function(pos){this.changeSize(pos)}.bind(this)});
		if (this.options.initialize) this.options.initialize.call(this);
	},
	
	show: function(el,event){
		this.target = el;
		this.slider.set(this.target.getStyle('font-size').toInt());
		this.curVal.addEvent('change', function(){
			this.slider.set(this.curVal.value);
		}.bindWithEvent(this));
		this.closeSelector.addEvent('click',this.hide.bind(this));
		this.container.setStyles({left: event.page.x+'px', top: event.page.y+'px'});
		if(this.options.fade == true){this.container.effect('opacity').start(1);}else{this.container.setOpacity(1);}
	},
	
	changeSize: function(pos){
		this.curVal.value = pos;
		if(this.target.getStyle('font-size').substring(3).toLowerCase() == 'x'){
			this.target.setStyle('font-size',pos+'px');
		} else{
			this.target.setStyle('font-size',pos+'pt');
		}
		this.fireEvent('onSelect', [this.target]);
	},
	
	hide: function(){
		this.target = null;
		this.curVal.removeEvents('change');
		this.closeSelector.removeEvents('click');
		if(this.options.fade == true){this.container.effect('opacity').start(0);}else{this.container.setOpacity(0);}
	},
	
	remove: function(){
		this.elements.each(function(el){
			el.removeEvents('click');
		});
		this.container.remove();
	}
});

fontSizer.implement(new Events);
fontSizer.implement(new Options);