// IOTBS2.0 :: Invasion of the Body Switchers - Look Who's Switching Too
// >>> "The Radio Edit"
//      Generates the switching controls as forms, legends and radios
// ***********************************************
// This copyright statement must remain in place for both personal and commercial use
// GNU General Public License -- http://www.gnu.org/copyleft/gpl.html
// ***********************************************
// Original concept by Andy Clarke -- http://www.stuffandnonsense.co.uk/
// DOM scripting by brothercake -- http://www.brothercake.com/
// Create element and attributes based on a method by beetle -- http://www.peterbailey.net/
//************************************************
function bodySwitcher(divid, label, isnative) {
	if (switcher.path != null && !switcher.isie && typeof isnative != 'undefined' && isnative == 'yes') {
		switcher.integrate(this, divid);
	}
	this.classes = [];
	if (document.getElementById(divid) == null) {
		return false;
	}
	this.divid = divid;
	var attrs = { 'action' : '' };
	this.form = document.getElementById(divid).appendChild(switcher.create('form', attrs));
	this.fieldset = this.form.appendChild(switcher.create('fieldset'));
	attrs = { 'text' : label };
	this.fieldset.appendChild(switcher.create('legend', attrs));
	return true;
};
bodySwitcher.prototype.defineClass = function(key, val) {
	this.classes[this.classes.length] = key;
	if (typeof this.fieldset == 'undefined') {
		return false;
	}
	var self = this;
	var attrs = { 'for' : 'for-' + this.divid + '-' + key };
	var labele = this.fieldset.appendChild(switcher.create('label', attrs));
	if (switcher.iskde && typeof labele.innerHTML != 'undefined') {
		labele.innerHTML += '<input type="radio" name="select-' + this.divid + '" id="for-' + this.divid + '-' + key + '" />';
		var radio = labele.lastChild;
	}
	else {
		attrs = { 'type' : 'radio', 'name' : 'select-' + this.divid, 'id' : 'for-' + this.divid + '-' + key };
		radio = labele.appendChild(switcher.create('input', attrs));
	}
	attrs = { 'text' : val };
	labele.appendChild(switcher.create('span', attrs));
	radio.checked = false;
	if (key == 'default') {
		radio.checked = true;
	}
	if ((switcher.cookie != null && switcher.cookie.indexOf(' ' + key + ' ')!=-1)) {
		radio.checked = true;
		if (key != 'default') {
			this.fieldset.childNodes[1].firstChild.checked = false;
		}
	}
	if (switcher.isie) {
		radio.checker = function(radio, iskbd) {
			var len = self.classes.length;
			for (var i=0; i<len; i++) {
				this.form.getElementsByTagName('input')[i].checked = false;
			}
			radio.checked = true;
			radio.focus();
			if (iskbd) {
				radio.click();
			}
		};
		radio.onkeydown = function() {
			var code = event.keyCode;
			var label = this.parentNode;
			if (code == 40 && label.nextSibling != null && /label/i.test(label.nextSibling.nodeName)) {
				this.checker(label.nextSibling.firstChild, true);
			}
			if (code == 38 && label.previousSibling != null && /label/i.test(label.previousSibling.nodeName)) {
				this.checker(label.previousSibling.firstChild, true);
			}
		};
	}
	radio.onclick = function() {
		var items = self.fieldset.getElementsByTagName('input');
		var len = items.length;
		for (var i=0; i<len; i++) {
			if (items[i] == this) {
				var ind = i;break;
			}
		}
		switcher.save(this.name.replace('select-', ''), this.id.split('for-' + self.divid + '-')[1], ind, self);
		if(switcher.isie) {
			this.checker(this, false);
		}
		return true;
	};
	return true;
};
bodySwitcher.prototype.update = function(ind) {
	if (typeof this.fieldset != 'undefined') {
		this.fieldset.getElementsByTagName('input')[ind].checked = true;
	}
};
