// IOTBS2.1 :: Invasion of the Body Switchers - Look Who's Switching Too
// >>> "Original"
//      Generates the switching controls as forms and selectors
// ***********************************************
// 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/
//************************************************



//switcher form constructor
function bodySwitcher(divid, label, isnative)
{
	//if load-mode is in use, and this is not ie, and this is the master of the native switching group
	if(switcher.path != null && !switcher.isie && typeof isnative != 'undefined' && isnative == 'yes')
	{
		//compile preferred/alternate stylesheets collection
		//and integrate with native switching
		switcher.integrate(
			this, //reference to this bodySwitcher object
			divid //switching control id
			);
	}

	//create an associate array of classnames for this option
	//so we can later iterate through and remove them from the custom classname string
	this.classes = [];

	//don't continue if the container doesn't exist
	if(document.getElementById(divid) == null) { return false; }

	//create a reference to 'this'
	var self = this;

	//start counting options, because we'll need the index of each option as it's created
	//so that an option can be selected by default if necessary
	this.options = 0;

	//outer form
	var attrs = { 'action' : '' };
	var frm = document.getElementById(divid).appendChild(switcher.create('form', attrs));

	//fieldset inside form
	var fieldset = frm.appendChild(switcher.create('fieldset'));

	//label inside fieldset
	attrs = { 'for' : 'select-' + divid };
	var labele = fieldset.appendChild(switcher.create('label', attrs));

	//span inside label containing label text
	attrs = { 'text' : label };
	labele.appendChild(switcher.create('span', attrs));

	//select inside label
	attrs = { 'id' : 'select-' + divid };
	this.select = labele.appendChild(switcher.create('select', attrs));

	//bind onchange handler
	this.select.onchange = function()
	{
		//save the current switcher state
		switcher.save(
			this.id.replace('select-', ''), //convert id of selector into switcher ident (label)
			this.options[this.options.selectedIndex].value, //new value from option
			this.options.selectedIndex, //the index of this option in the switcher group
			self //a reference to this bodySwitcher object
			);
	};

	return true;
};

//add a new class option method
bodySwitcher.prototype.defineClass = function(key, val)
{
	//store the classname
	this.classes[this.classes.length] = key;

	//don't continue if the select doesn't exist
	if(typeof this.select == 'undefined') { return false; }

	//option inside select
	var attrs = { 'value' : key, 'text' : val };
	this.select.appendChild(switcher.create('option', attrs));

	//check for cookie value
	if(switcher.cookie != null)
	{
		//if value contains this key
		if(switcher.cookie.indexOf(' ' + key + ' ')!=-1)
		{
			//select this option
			this.select.selectedIndex = this.options;
		}
	}

	//increase option count
	this.options ++;

	return true;
};

//update the switcher control from programatic option changes
bodySwitcher.prototype.update = function(ind)
{
	//update the selector if it exists
	if(typeof this.select != 'undefined')
	{
		this.select.selectedIndex = ind;
	}
};

