// @version $Id: make_form.js 4579 2011-09-26 14:58:49Z sjelner $

function makeForm(sel) {
	$.extend(this,{
		sel: sel,
		els: false,
		tmp: false,

		transformRadio: function() {
			var that = this;
			this.els = $(sel);
			this.tmp = {};
			this.els.each(function(i,el) {
				el = $(el);
				el.removeAttr('data-transform'); //prevent recursive transformation
				//radio sequences have a unique name
				var name = el.attr('name');
				if(typeof(name) === 'undefined' || name === '') name = el.attr('id');
				if(typeof(name) === 'string' && name !== '') {
					//if the entry for the radio sequence name does not exist, create one
					if(typeof(that.tmp[name]) === 'undefined') {
						that.tmp[name] = {'real':[],'fake':[],'checked':false,'initial':false};
					};
					var tmp = that.tmp[name]; //shorthand
					tmp.real.push(el); //fill the array of real elements
					var j = (tmp.real.length-1); //measure new length
					if(el[0].checked) {
						tmp.checked = j; //set the fake checked status
						tmp.initial = j;
					};
					el.css('display','none'); //hide original radio
					tmp.fake[j] = $('<a />').attr({ //create fake element
						'href': '#',
						'class': 'makeform-radio'
					}).bind({
						'click': function(e) {
							e.preventDefault();
							e.target.blur();
							that.rebuildRadio(j,name);
						}
					});
					el.before(tmp.fake[j]); //inject fake element
					//fake the <label for=-functionality
					var lId = el.attr('id');
					if(typeof(lId) === 'string' && lId !== '') {
						$('label[for='+lId+']').removeAttr('for').bind({
							'click': function(e) {
								e.preventDefault();
								that.rebuildRadio(j,name);
							}
						});
					};
				};
			});
			$.each(this.tmp,function(key,value) {
				$(value.real[0]).bind('reset',function() {
					that.rebuildRadio(value.initial,key,true);
				});
				that.rebuildRadio(value.checked,key,true);
			});
			return true;
		},

		rebuildRadio: function(i,name,first) {
			if(typeof(this.tmp[name]) !== 'undefined') {
				var tmp = this.tmp[name];
				if(i === false || i < tmp.real.length) {
					tmp.checked = i;
					for(var j=0;j<tmp.real.length;j++) {
						tmp.real[j][0].checked = false;
						tmp.real[j].removeAttr('checked');
						tmp.fake[j].removeClass('checked');
					};
					if(tmp.checked !== false) {
						tmp.real[tmp.checked][0].checked = true;
						tmp.real[tmp.checked].attr('checked','checked');
						tmp.fake[tmp.checked].addClass('checked');
					};
					if(typeof(first) === 'undefined' || !first) tmp.real[tmp.checked].trigger('change');
				};
				return true;
			} else return false;
		},

		transformSelect: function() {
			var that = this;
			this.els = $(sel);
			this.tmp = {};
			this.els.each(function(i,el) {
				el = $(el);
				el.removeAttr('data-transform'); //prevent recursive transformation
				//radio sequences have a unique name
				var name = el.attr('name');
				if(typeof(name) === 'undefined' || name === '') name = el.attr('id');
				if(typeof(name) === 'string' && name !== '') {
					//if the entry for the radio sequence name does not exist, create one
					if(typeof(that.tmp[name]) === 'undefined') {
						that.tmp[name] = {
							'real': false,
							'realOptions': false,
							'fake': false,
							'fakeCur': false,
							'fakeList': false,
							'fakeListEls': false,
							'selected': false,
							'initial': false
						};
					};
					var tmp = that.tmp[name]; //shorthand
					tmp.real = el; //the real element
					tmp.fake = $('<div />').attr('class','makeform-select'); //fake element
					tmp.fakeCur = $('<a />').attr({
						'href':	'#',
						'class': 'makeform-select-link'
					}).bind({
						'click': function(e) {
							e.preventDefault();
							e.stopPropagation();
							e.target.blur();
							tmp.fake.addClass('flyout');
						}
					});
					tmp.fake.append(tmp.fakeCur);
					$(document).bind('click',function() {
						tmp.fake.removeClass('flyout');
					});
					tmp.fakeList = $('<ul />').attr('class','makeform-select-list');
					tmp.fake.append(tmp.fakeList);
					tmp.realOptions = tmp.real.find('option');
					tmp.realOptions.each(function(j,el2) {
						el2 = $(el2);
						tmp.fakeList.haml(['%li',['%a',{
							'href': '#',
							'events': {
								'click': function(e) {
									e.preventDefault();
									e.stopPropagation();
									e.target.blur();
									that.rebuildSelect(j,name);
								}
							}
						},el2.html()]]);
						if(el2[0].selected) {
							tmp.selected = j;
							tmp.initial = j;
						};
					});
					tmp.fakeListEls = tmp.fakeList.find('li');
					el.css('display','none'); //hide original radio
					el.before(tmp.fake); //inject fake element
				};
			});
			$.each(this.tmp,function(key,value) {
				$(value.real).bind('reset',function() {
					that.rebuildSelect(value.initial,key,true);
				});
				that.rebuildSelect(value.selected,key,true);
			});
			return true;
		},

		//the select fake has to be rebuild all the time
		rebuildSelect: function(i,name,first) {
			if(typeof(this.tmp[name]) !== 'undefined') {
				tmp = this.tmp[name];
				tmp.selected = i;
				tmp.fake.removeClass('flyout');
				tmp.fakeListEls.each(function(j,el) {
					el = $(el);
					el.removeClass('selected');
					if(j === i) el.addClass('selected');
				});
				tmp.realOptions.each(function(j,el) {
					el = $(el);
					el.removeAttr('selected');
					if(j === i) {
						el[0].selected = true;
						el.attr('selected','selected');
						tmp.fakeCur.html(el.html());
					};
				});
				if(typeof(first) === 'undefined' || !first) tmp.real.trigger('change');
				return true;
			} else return false;
		},

		transformCheckbox: function() {
			var that = this;
			this.els = $(sel);
			this.tmp = {};
			this.els.each(function(i,el) {
				el = $(el);
				el.removeAttr('data-transform'); //prevent recursive transformation
				var name = el.attr('name');
				if(typeof(name) === 'undefined' || name === '') name = el.attr('id');
				if(typeof(name) === 'string' && name !== '') {
					if(typeof(that.tmp[name]) === 'undefined') {
						that.tmp[name] = {'real':false,'fake':false,'checked':false,'initial':false};
					};
					var tmp = that.tmp[name]; //shorthand
					tmp.real = el; //real element
					if(el[0].checked) {
						tmp.checked = true;
						tmp.initial = true;
					};
					el.css('display','none'); //hide original radio
					tmp.fake = $('<a />').attr({ //create fake element
						'href': '#',
						'class': 'makeform-checkbox'
					}).bind({
						'click': function(e) {
							e.preventDefault();
							e.target.blur();
							that.rebuildCheckbox(name);
						}
					});
					el.before(tmp.fake); //inject fake element
					//fake the <label for=-functionality
					$('label[for='+name+']').removeAttr('for').bind({
						'click': function(e) {
							e.preventDefault();
							that.rebuildCheckbox(name);
						}
					});
				};
			});
			$.each(this.tmp,function(key,value) {
				$(value.real).bind('reset',function() {
					that.rebuildCheckbox(value.initial,key,true);
				});
				that.rebuildCheckbox(value.checked,key,true);
			});
			return true;
		},

		rebuildCheckbox: function(state,name,first) {
			if(typeof(this.tmp[name]) !== 'undefined') {
				var tmp = this.tmp[name];
				if(state === true) {
					tmp.checked = false;
					tmp.real[0].checked = false;
					tmp.real.removeAttr('checked');
					tmp.fake.removeClass('checked');
				} else {
					tmp.checked = true;
					tmp.real[0].checked = true;
					tmp.real.attr('checked','checked');
					tmp.fake.addClass('checked');
				};
				if(typeof(first) === 'undefined' || !first) tmp.real.trigger('change');
				return true;
			} else return false;
		}
	});
};

var makeFormHelpers = {
	radioVal: function(els) {
		var tmp = false;
		els.each(function(i,el) { if(el.checked) tmp = $(el).attr('value'); });
		return tmp;
	},

	reset: function(els) {
		els.find('input, select').trigger('reset');
	}
};
