/* Text changer - light version.
Let your text's font size customizable.
by Marco Rosella - http://www.centralscrutinizer.it/en/design/js-php/text-changer
                v0.2 - May 18, 2006
*/

/* Shabana Ali: contact pages have already the window.onload function and textChanger.init(); declared there */
var currentPage = new String(document.location);
if (currentPage.indexOf('contact') == -1) {
	window.onload = function(){
		textChanger.init(); 	
	}
}

var textChanger = {
	cpanel : 'textchanger',  //set here the id of the element (div, p) within you want to insert the control panel
	element : 'divbody',   	 //set here the id of the element (div, p) within you want to change the text
	defaultFS : 1,         //set here the default font size in 'em'
	init: function() {
		var cpel = document.getElementById(textChanger.cpanel);
		var el = document.getElementById(textChanger.element);
		if (cpel == null || el == null) {alert('The elements with the \"'+textChanger.cpanel+'\" and/or \"'+textChanger.element+'\" ID do not exist in HTML source.');} else {
		var u = document.createElement('ul');
		cpel.appendChild(u);
		u.innerHTML = 
		'<li id="decrease">-<a href="#" title="Decrease font size">A</a></li>'+
		'<li id="reset"><a href="#" title="Default font size">A</a></li>'+
		'<li id="increase"><a href="#" title="Increase font size">A</a>+</li>'+
		'<span>Text Size</span>'
		var sz = textChanger.getCookie();
		el.style.fontSize = sz ? sz + 'em' : textChanger.defaultFS + 'em';
		var incr = document.getElementById('increase');
		incr.onclick = function(){textChanger.changeSize(1); return false;};
		var decr = document.getElementById('decrease');
		decr.onclick = function(){textChanger.changeSize(-1); return false;};
		var reset= document.getElementById('reset');
		reset.onclick = function(){textChanger.changeSize(0); return false;};
		}
	} ,

	changeSize: function(val) {
		var el = document.getElementById(textChanger.element);
		var size = el.style.fontSize.substring(0,3);
		var fSize = parseFloat(size,10);
		if (val == 1) {
			fSize += 0.11;
			if (fSize > 2.0) fSize = 2.0;
		} 
		if (val == -1) {
			fSize -= 0.11;
			if (fSize < 0.5) fSize = 0.5;
		}		
		if (val == 0) {
			fSize = 1;
		}
		el.style.fontSize = fSize + 'em';
		textChanger.updateCookie(fSize);
		} ,
		
	updateCookie: function(vl) {
		var today = new Date();
		var exp = new Date(today.getTime() + (365*24*60*60*1000)); //the cookie will expire in one year  
		document.cookie = 'textChangerL=size=' + vl + ';' +'expires=' + exp.toGMTString() + ';' +'path=/';
	} ,

	getCookie: function() { 
		var cname = 'textChangerL=size=';	
		var start = document.cookie.indexOf(cname);
		var len = start + cname.length;
		if ((!start) && (cname != document.cookie.substring(0,cname.length))) {return null;}
		if (start == -1) return null;
		var end = document.cookie.indexOf(";",len);
		if (end == -1) end = document.cookie.length;
		return unescape(document.cookie.substring(len, end));
	}
}


initNav = function() {
	if (typeof document.body.style.maxHeight == 'undefined') {
		var nav = document.getElementById("main-nav");
		var lis = nav.getElementsByTagName("li");
		for (var i=0; i<lis.length; i++)
		{
			lis[i].onmouseover = function()
			{
				this.className += " hover";
			}
			lis[i].onmouseout = function()
			{
				this.className = this.className.replace(" hover", "");
			}
		}
	}
}
if (document.all && !window.opera)
	attachEvent("onload", initNav);
	
	
function initInputs()
{
	var inputs = document.getElementsByTagName("input");
	for (var i=0; i<inputs.length; i++)
	{
		if (inputs[i].type == "text" && (inputs[i].name == "search"))
		{
			inputs[i].onfocus = function ()
			{
				if (this.value == "Search site")
					this.value = "";
			}
			inputs[i].onblur = function ()
			{
				if (this.value == "" && this.name == "search") this.value = "Search site";
			}
		}
	}
}
if (window.addEventListener)
{
	window.addEventListener("load", initInputs, false);
	window.addEventListener("load", initPage, false);
}
else if (window.attachEvent){
	window.attachEvent("onload", initInputs);
	window.attachEvent("onload", initPage);
}


function initPage()
{
	var blocks = document.getElementsByTagName("li");
	for (var i=0; i<blocks.length; i++)
	{
		if (blocks[i].className.indexOf("roll") != -1)
		{
			if ( blocks[i].className.indexOf("close") == -1 ) blocks[i].className += " open";
			var links = blocks[i].getElementsByTagName("a");
		
			for (var k=0; k<links.length; k++) {
					
				if (links[k].className == "sub-menu") {
						
					links[k].onclick = function()
					{
						if ( this.parentNode.className.indexOf("open") != -1 ) 
						{
							this.parentNode.className = this.parentNode.className.replace("open", "close");
						}
						else
						{
							this.parentNode.className += " open";
						}
						return false;
					}
				}
			}
		}
	}
}
