// IOTBS1.2 :: Attack of the Body Switchers
// ***********************************************
// 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 iotbs() { //open initialisation function 
//************************************************

//initialise the preferences manager ('canvas-element')
switcher = new switchManager('body');

/*****************************************************************************
 Define switching controls
*****************************************************************************/

//create a new switcher control ('container-id', 'label')
var screenSwitcher = new bodySwitcher('screen-switcher', 'Readability');

//add a new class option ('classname', 'label')
screenSwitcher.defineClass('default', 'Normal');
screenSwitcher.defineClass('large', 'Large');
screenSwitcher.defineClass('contrast plain', 'Plain');
screenSwitcher.defineClass('contrast', 'High contrast');

/*****************************************************************************
*****************************************************************************/
};var switcher;if(typeof window.addEventListener != 'undefined'){window.addEventListener('load', iotbs, false);}else if(typeof document.addEventListener != 'undefined'){document.addEventListener('load', iotbs, false);}else if(typeof window.attachEvent != 'undefined'){window.attachEvent('onload', iotbs);window.attachEvent('onunload', function(){this.daLen = document.all.length;for(var i=0; i<this.daLen; i++){document.all[i]['onchange'] = null;}});}function switchManager(canvas){this.string  = '';this.canvas = document.getElementsByTagName(canvas)[0];this.initial = this.canvas.className;if(this.initial == ''){this.initial = 'itobs';}this.cookie = this.read();if(this.cookie != null){this.string = this.cookie;this.canvas.className = this.initial + this.string;}};switchManager.prototype.set = function(days){this.date = new Date();this.date.setTime(this.date.getTime() + ( days *24*60*60*1000));this.info = this.string.replace(/ /g,'#');if(this.info == '') { this.date.setTime(0); }document.cookie = 'bodySwitcher=' + this.info+ '; expires=' + this.date.toGMTString() + '; path=/';};switchManager.prototype.read = function(){this.cookie = null;if(document.cookie){if(document.cookie.indexOf('bodySwitcher')!=-1){this.cookie = document.cookie.split('bodySwitcher=');this.cookie = this.cookie[1].split(';');this.cookie = this.cookie[0].replace(/#/g,' ');}}return this.cookie;};function bodySwitcher(divid, label){if(document.getElementById(divid) == null) { return false; }this.classes = [];this.options = 0;this.attrs = { 'action' : '' };this.form = this.createElement('form', this.attrs);document.getElementById(divid).appendChild(this.form);this.fieldset = this.createElement('fieldset');this.form.appendChild(this.fieldset);this.attrs = { 'for' : 'select-' + divid };this.label = this.createElement('label', this.attrs);this.fieldset.appendChild(this.label);this.attrs = { 'text' : label };this.span = this.createElement('span', this.attrs);this.label.appendChild(this.span);this.attrs = { 'id' : 'select-' + divid };this.select = this.createElement('select', this.attrs);this.label.appendChild(this.select);var self = this;this.select.onchange = function(){this.classLen = self.classes.length;for(var i=0; i < this.classLen; i++){switcher.string = switcher.string.replace(' ' + self.classes[i] + ' ','');}this.chosen = this.options[this.options.selectedIndex].value;if(this.chosen != 'default'){switcher.string += ' ' + this.chosen + ' ';}switcher.canvas.className = switcher.initial + switcher.string;switcher.set(365);};return true;};bodySwitcher.prototype.defineClass = function(key, val){if(typeof this.select == 'undefined') { return false; }this.attrs = { 'value' : key, 'text' : val }; this.option = this.createElement('option', this.attrs);this.select.appendChild(this.option);if(switcher.cookie != null){if(switcher.cookie.indexOf(' ' + key + ' ')!=-1){this.select.selectedIndex = this.options;}}this.classes[this.options] = key;this.options ++;return true;};bodySwitcher.prototype.createElement = function(tag, attrs){this.ele = (typeof document.createElementNS != 'undefined') ? document.createElementNS('http://www.w3.org/1999/xhtml',tag) : document.createElement(tag);if(typeof attrs != 'undefined'){for(var i in attrs){switch(i){case 'text' :this.ele.appendChild(document.createTextNode(attrs[i]));break;case 'class' : this.ele.className = attrs[i];break;case 'for' : this.ele.setAttribute('htmlFor',attrs[i]);break;default : this.ele.setAttribute(i,'');this.ele[i] = attrs[i];break;}}}return this.ele;};