function iotbs() { switcher = new switchManager();
var screenSwitcher = new bodySwitcher('screen-switcher', 'Ваш Стиль:');
screenSwitcher.defineClass('var4',  'Круг и огни');
screenSwitcher.defineClass('var2',  'Радуга');
screenSwitcher.defineClass('var1',  'Голубая планета');
screenSwitcher.defineClass('var3',  'Сила личная');
screenSwitcher.defineClass('var5',  'Светлый фон');
};


var switcher;

//.. gecko, safari, konqueror and generic
if(typeof window.addEventListener != 'undefined')
{
	window.addEventListener('load', iotbs, false);
}
//.. opera 7
else if(typeof document.addEventListener != 'undefined')
{
	document.addEventListener('load', iotbs, false);
}
//.. win/ie
else if(typeof window.attachEvent != 'undefined')
{
	window.attachEvent('onload', iotbs);
}


function switchManager()
{
	this.string  = '';
	
	this.body = document.getElementsByTagName('body')[0];

	this.initial = this.body.className;
	
	if(this.initial == '')
	{
		this.initial = 'itobs';
	}
	
	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); }
	
	document.cookie = 'bodySwitcher=' + this.info
		+ '; expires=' + this.date.toGMTString() 
		+ '; path=/';
};


switchManager.prototype.read = function()
{
	this.cookie = null;
	
	//if a cookie exists
	if(document.cookie)
	{
		//if it's our cookie
		if(document.cookie.indexOf('bodySwitcher')!=-1)
		{
			//extract and store relevant information (turning '#' back into spaces)
			this.cookie = document.cookie.split('bodySwitcher=');
			this.cookie = this.cookie[1].split(';');
			this.cookie = this.cookie[0].replace(/#/g,' ');
		}
	}
	
	return this.cookie;
};


//switcher form constructor
function bodySwitcher(divid, label)
{
	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.div = this.createElement('div', this.attrs);
	this.label.appendChild(this.div);

	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.body.className = switcher.initial + switcher.string;

		switcher.set(365)
	};
};

bodySwitcher.prototype.defineClass = function(key, val)
{
	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 ++;
};

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)
			{
				//create a text node
				case 'text' :
					this.ele.appendChild(document.createTextNode(attrs[i]));
					break;
				
				//create a class name
				case 'class' : 
					this.ele.className = attrs[i];
					break;
				
				//create a for attribute 
				case 'for' : 
					this.ele.setAttribute('htmlFor',attrs[i]);
					break;
				
				//create a generic attribute using IE-safe attribute creation
				default : 
					this.ele.setAttribute(i,'');
					this.ele[i] = attrs[i];
					break;
			}
		}
	}
	return this.ele;
};

