// 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/
//************************************************
//open initialisation function

function iotbs() { 

	try{
	switcher = new switchManager("screen-switcher","");
	}catch(x){ }
	
	try{
	switcher = new switchManager("screen-switcher3rd","");
	}catch(x){ }

};

//global preferences manager reference
var switcher;

//setup initialisation function
//.. gecko, safari, konqueror and generic

if(navigator.appName.indexOf("Microsoft") >= 0) // I.E Only
{
	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);
	}
}
//preferences manager 
function switchManager(divid,styleName)
{

	this.string  = '';

	//store reference to body element
	this.body = document.getElementsByTagName('body')[0];

	if(styleName != "")
	{
		this.body.className = styleName;
		setCookie("DRSTS", styleName, "","/");

	}else{
		value = getCookie("DRSTS");

		if(value != null)
		{
			this.body.className = value;
		}else{
			this.body.className = "default";
		}
		
	}


	this.span = (typeof document.createElementNS != 'undefined') ? document.createElementNS('http://www.w3.org/1999/xhtml',"span") : document.createElement("span");
 	this.textObj = (typeof document.createElementNS != 'undefined') ? document.createElementNS('http://www.w3.org/1999/xhtml',"a") : document.createElement("a");
  	this.someImg = (typeof document.createElementNS != 'undefined') ? document.createElementNS('http://www.w3.org/1999/xhtml',"img") : document.createElement("img");		

		if(this.body.className == "default")
		{
			//sometext = document.createTextNode("Large Text");
			this.someImg.src = "/Images/shared/largetexticon2.gif";
			this.textObj.appendChild(this.someImg);
			this.textObj.setAttribute("href","javascript:switchManager('" + divid + "','large')");
			this.attrs = this.span.appendChild(this.textObj);
			document.body.style.height = document.documentElement.scrollHeight+'px';
		}else{
			//sometext = document.createTextNode("Normal Text");
			this.someImg.src = "/Images/shared/smalltexticon.gif";
			this.textObj.appendChild(this.someImg);
			this.textObj.setAttribute("href","javascript:switchManager('" + divid + "','default')");
			this.attrs = this.span.appendChild(this.textObj);
			document.body.style.height = '100%';
	}
	try{
	tempObj = document.getElementById(divid).childNodes[0];
	document.getElementById(divid).removeChild(tempObj);
	}catch(x){ }
	
	try{
	document.getElementById(divid).appendChild(this.attrs);
	}catch(x){ }

};

function setCookie(cookie_name, cookie_value, cookie_life, cookie_path) {
  var today = new Date()
  var expiry 
  
  if(cookie_life != "")
  {
  expiry = new Date(today.getTime() + cookie_life * 24*60*60*1000)
  }
  
  if (cookie_value != null && cookie_value != ""){
    var cookie_string =cookie_name + "=" + escape(cookie_value)
    if(cookie_life){ cookie_string += "; expires=" + expiry.toGMTString()}
    if(cookie_path){ cookie_string += "; path=" + cookie_path}
 	document.cookie = cookie_string
  }
} // Based on JavaScript provided by Peter Curtis at www.pcurtis.com -->

/* Call function as getCookie("cookiename") It returns the value of a cookie
if set or null. Beware of potential ambiguities in names of cookies -
getCookie is simple and will match the end of a string so xyname 
will also be matched by yname and ame. */
 
function getCookie(name) {
  var index = document.cookie.indexOf(name + "=")
  if (index == -1) { return null}
  index = document.cookie.indexOf("=", index) + 1
  var end_string = document.cookie.indexOf(";", index)
  if (end_string == -1) { end_string = document.cookie.length }
  return unescape(document.cookie.substring(index, end_string))
} // Based on JavaScript provided by Peter Curtis at www.pcurtis.com -->

function getElementsByClassName(ClassName,tagName,parentElement)
{
 var elements=new Array();
 var d=parentElement ? parentElement : document;
 var allElements;

 if(tagName)
   allElements=d.all && d.all.tags(tagName)
    || d.getElementsByTagName && d.getElementsByTagName(tagName);
 else allElements=d.all || d.getElementsByTagName("*");
 
 for(var i=0,len=allElements.length; i<len; i++)
  if(allElements[i].className==ClassName)
   elements[elements.length]=allElements[i];

 return elements;
}

//*** Alternates shading in a table
function altrows() {
var tableElements = getElementsByClassName("altshadetable") ;
var table = tableElements[tableElements.length - 1] ;

var rows = table.getElementsByTagName("tr") ;
try{
	for( i = 0; i <= rows.length; i=i+2) {
	rows[i].bgColor = '#FFF';
	rows[i+1].bgColor = '#CCC';
	}
}catch(x){ }
}
