/*
* FancyForm 0.94  By Vacuous Virtuoso, lipidity.com
* Checkbox and radio input replacement script.
* Toggles defined class when input is selected.
*/

var FormStyle = {
	start: function(elements, options){
		FormStyle.initing = 1;
		if($type(elements)!='array') elements = $$('input');
		if(!options) options = [];
		FormStyle.onclasses = ($type(options['onClasses']) == 'object') ? options['onClasses'] : {
			checkbox: 'checked',
			radio: 'selected'
		}
		FormStyle.offclasses = ($type(options['offClasses']) == 'object') ? options['offClasses'] : {
			checkbox: 'unchecked',
			radio: 'unselected'
		}
		if($type(options['extraClasses']) == 'object'){
			FormStyle.extra = options['extraClasses'];
		} else if(options['extraClasses']){
			FormStyle.extra = {
				checkbox: 'f_checkbox',
				radio: 'f_radio',
				on: 'f_on',
				off: 'f_off',
				all: 'fancy'
			}
		} else {
			FormStyle.extra = {};
		}
		FormStyle.onSelect = $pick(options['onSelect'], function(el){});
		FormStyle.onDeselect = $pick(options['onDeselect'], function(el){});
		var keeps = [];
		FormStyle.chks = elements.filter(function(chk){
			if( $type(chk) != 'element' ) return false;
			if( chk.get('tag') == 'input' && (FormStyle.onclasses[chk.getProperty('type')]) ){
				var el = chk.getParent();
				if(el.getElement('input')==chk){
					el.type = chk.getProperty('type');
					el.inputElement = chk;
					this.push(el);
				} else {
					chk.addEvent('click',function(f){
						if(f.event.stopPropagation) f.event.stopPropagation();
					});
				}
			} else if( (chk.inputElement = chk.getElement('input')) && (FormStyle.onclasses[(chk.type = chk.inputElement.getProperty('type'))]) ){
				return true;
			}
			return false;
		}.bind(keeps));
		FormStyle.chks = FormStyle.chks.combine(keeps);
		keeps = null;
		FormStyle.chks.each(function(chk){
			var c = chk.inputElement;
			c.setStyle('position', 'absolute');
			c.setStyle('left', '-9999px');
			chk.addEvent('selectStart', function(f){f.stop()});
			chk.name = c.getProperty('name');
			FormStyle.update(chk);
		});
		FormStyle.chks.each(function(chk){
			var c = chk.inputElement;
			chk.addEvent('click', function(f){
				f.stop(); f.type = 'prop';
				c.fireEvent('click', f, 1);
			});
			chk.addEvent('mousedown', function(f){
				if($type(c.onmousedown) == 'function')
					c.onmousedown();
				f.preventDefault();
			});
			chk.addEvent('mouseup', function(f){
				if($type(c.onmouseup) == 'function')
					c.onmouseup();
			});
			c.addEvent('focus', function(f){
				if(FormStyle.focus)
					chk.setStyle('outline', '1px dotted');
			});
			c.addEvent('blur', function(f){
				chk.setStyle('outline', 0);
			});
			c.addEvent('click', function(f){
				if(f.event.stopPropagation) f.event.stopPropagation();
				if(c.getProperty('disabled')) // c.getStyle('position') != 'absolute'
					return;
				if (!chk.hasClass(FormStyle.onclasses[chk.type]))
					c.setProperty('checked', 'checked');
				else if(chk.type != 'radio')
					c.setProperty('checked', false);
				if(f.type == 'prop')
					FormStyle.focus = 0;
				FormStyle.update(chk);
				FormStyle.focus = 1;
				if(f.type == 'prop' && !FormStyle.initing && $type(c.onclick) == 'function')
					 c.onclick();
			});
			c.addEvent('mouseup', function(f){
				if(f.event.stopPropagation) f.event.stopPropagation();
			});
			c.addEvent('mousedown', function(f){
				if(f.event.stopPropagation) f.event.stopPropagation();
			});
			if(extraclass = FormStyle.extra[chk.type])
				chk.addClass(extraclass);
			if(extraclass = FormStyle.extra['all'])
				chk.addClass(extraclass);
		});
		FormStyle.initing = 0;
		$each($$('form'), function(x) {
			x.addEvent('reset', function(a) {
				window.setTimeout(function(){FormStyle.chks.each(function(x){FormStyle.update(x);x.inputElement.blur()})}, 200);
			});
		});
	},
	update: function(chk){
		if(chk.inputElement.getProperty('checked')) {
			chk.removeClass(FormStyle.offclasses[chk.type]);
			chk.addClass(FormStyle.onclasses[chk.type]);
			if (chk.type == 'radio'){
				FormStyle.chks.each(function(other){
					if (other.name == chk.name && other != chk) {
						other.inputElement.setProperty('checked', false);
						FormStyle.update(other);
					}
				});
			}
			if(extraclass = FormStyle.extra['on'])
				chk.addClass(extraclass);
			if(extraclass = FormStyle.extra['off'])
				chk.removeClass(extraclass);
			if(!FormStyle.initing)
				FormStyle.onSelect(chk);
		} else {
			chk.removeClass(FormStyle.onclasses[chk.type]);
			chk.addClass(FormStyle.offclasses[chk.type]);
			if(extraclass = FormStyle.extra['off'])
				chk.addClass(extraclass);
			if(extraclass = FormStyle.extra['on'])
				chk.removeClass(extraclass);
			if(!FormStyle.initing)
				FormStyle.onDeselect(chk);
		}
		if(!FormStyle.initing)
			chk.inputElement.focus();
	},
	all: function(){
		FormStyle.chks.each(function(chk){
			chk.inputElement.setProperty('checked', 'checked');
			FormStyle.update(chk);
		});
	},
	none: function(){
		FormStyle.chks.each(function(chk){
			chk.inputElement.setProperty('checked', false);
			FormStyle.update(chk);
		});
	}
};

window.addEvent('domready', function(){
	FormStyle.start();
});