﻿// Invasion of the Body Switchers
// This copyright statement must remain in place for both personal and commercial use
// ***********************************************************************************
// Creative Commons License -- http://creativecommons.org/licenses/by-nc-nd/2.0/
// Original concept and article by Malarkey (Andy Clarke) -- http://www.stuffandnonsense.co.uk/
// DOM scripting by Brothercake (James Edwards) -- http://www.brothercake.com/
// Create element and attributes based on a method by beetle -- http://www.peterbailey.net/
//
// Amended to suit single un-ordered list by the Team 2207
//
//************************************************

function dwflSwitcher()
{
    switcher = new switchManager();

    var screenSwitcher = new bodySwitcher('header-tools-switcher', '');

    screenSwitcher.defineClass('normal', 'Default text size');
    screenSwitcher.defineClass('larger', 'Larger text size');
    screenSwitcher.defineClass('largest', 'Largest text size'); 
    
    if (debug)
    {
        alert('dwflSwitcher.cstr()');
    }
};

var switcher;
var debug = false;

function switchManager()
{
	this.string  = '';
	this.body = document.getElementsByTagName('body')[0];
	this.initial = this.body.className;
	
	if (this.initial == '')
	{
		this.initial = ' normal ';
	}
	
	this.cookie = this.read();

	if (this.cookie != null)
	{
		this.string = this.cookie;
		this.body.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); }
	
	var cookieValue = 'bodySwitcher=' + this.info + '; expires=' + this.date.toGMTString() + '; path=/'
	
	document.cookie = cookieValue;
};

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)
{
	this.classes = [];
	this.options = 0;
	this.parent = document.getElementById(divid);
	this.list = this.createElement('ul');
	this.parent.appendChild(this.list);

	var self = this;
};

bodySwitcher.prototype.defineClass = function(key, val)
{
	this.attrs = { 'class' : 'a-' + key }; 
	this.option = this.createElement('li', this.attrs);
	
	this.attrs = { 'href' : '#'+key, 'title' : val, 'text' : 'A' }; 
	this.anchor = this.createElement('a', this.attrs);
	this.option.appendChild(this.anchor);
	
	this.anchor.onclick = function() 
	{
        switcher.string = '';
		this.chosen = key;

		switcher.string += ' ' + this.chosen + ' ';	
		switcher.body.className = switcher.initial + switcher.string;
		switcher.set(365);
	}
	
	this.list.appendChild(this.option);
	this.classes[this.options] = key;
	this.options ++;
};

//create element and attributes method -- http://www.codingforums.com/showthread.php?s=&postid=151108
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;
};
