// This script initializes the javascripts
// 
var theFontSizeController = new fontSizeController();

function cookieCreate(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function cookieRead(name) {

	//Read more: http://java-programming.suite101.com/article.cfm/using_javascript_cookies_with_html#ixzz0OwVsiRGS

	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function cookieErase(name) {
	cookieCreate(name,"",-1);
}

function debug(obj) {
	if (typeof(console) == "undefined") return;
	console.log("%o", obj);
}

function displayFontSizeController(functionName, imageName, altText, titleText) {
	document.write("<div class = \"font_size_controller\">");
	document.write("<a href=\"javascript:theFontSizeController." + functionName + "();\">");
	document.write("<img src=\"" + imageName + "\"");
	document.write("alt=\"" + altText + "\"");
	document.write("title=\"" + titleText + "\" />");
	document.write("</a>");
	document.write("</div>");
}

function fontSizeController()
{
	
	/* The method 'decreaseFontSize' decreases the font size until its minimum */
	function decreaseFontSize() {
		if (0 == this.size) return;
		if (this.size > this.min) {
			this.size -= this.step;
			this.setNewFontSize();
		}
	}
	
	/* The method 'increaseFontSize' increases the font size up to its maximum */
	function increaseFontSize() {
		if (0 == this.size) return;
		if (this.size < this.max) {
			this.size += this.step;
			this.setNewFontSize();
		}
	}

	/* The method 'init' initialises the fontSizeController. It has to be called after the HTML DOM has been established.
	 * Parameters:
	 *	tag_name			Name of the element identifier of the HTML document for which the font size will be controlled
	 *	tag_fontsize_unit		The font size unit ('em', 'px', '%' etc.) used in the HTML document to set de default font size
	 *	cookie_name		The name of the cookie used to read and write the font size so that it can be reused at next start up
	 */
    function init(tag_name, tag_fontsize_unit, cookie_name) {
	
		this.cookie_name = cookie_name;
		fontSize = cookieRead(this.cookie_name);

//alert(fontSize);
		
		/* Check validity of the fontSize value read from the cookie 
		 * If no cookie exists cookieRead will return null, then set fontSize to 0.0 to force 
		 * setting the fontSize to the default font size of the tag
		 */
		if (null == fontSize) {
			fontSize = 0.0; 
		} else {
			fontSize = parseFloat(fontSize);
		}

		if (isNaN(fontSize)) {
			fontSize = 0.0;
		}

//alert(fontSize);
		
		tag_id = document.getElementById(tag_name);
		if (document.defaultView) {
			// non IE browsers return the font size in 'px'
			defaultFontSize = parseFloat(document.defaultView.getComputedStyle(tag_id,null).getPropertyValue('font-size'));
			if (0 == fontSize) {
				fontSize = defaultFontSize;
			} // if (0 == fs)
			minFontSize = 10.0;
			maxFontSize = 30.0;
			stepFontSize = 1.0;
			unitFontSize = "px";
		} else {
			// Internet Explorer browsers return the font size in the units as given in the HTML document.
			// This has to be supplied as the fontsize_unit
			defaultFontSize = parseFloat(tag_id.currentStyle.fontSize);
//alert(defaultFontSize);
			if (0 == fontSize) {
				fontSize = defaultFontSize;
			} // if (0 == fs)
//alert(fontSize);
			minFontSize = 0.65;
			maxFontSize = 3.0;
			stepFontSize = 0.05;
			unitFontSize = tag_fontsize_unit;
//alert(unitFontSize);
		} // if (document.DefaultView)
		
		this.tag_id = tag_id;
		this.default_size = defaultFontSize;
		this.size = fontSize;
		this.min = minFontSize;
		this.max = maxFontSize;
		this.step = stepFontSize;
		this.unit = unitFontSize;
			
		this.setNewFontSize();
	}
	
	/* The method 'resetFontSize' resets the font size to the original value of the HTML document */
	function resetFontSize() {
		if (0 == this.size) return;
		this.size = this.default_size;
		this.setNewFontSize();
	}
	
	/* The method 'setNewFontSize' sets the new font size and write its value to the cookie */
	function setNewFontSize() {
		if (0 == this.size) return;
		this.tag_id.style.fontSize = this.size+this.unit;
		cookieCreate(this.cookie_name,this.size,360);
//alert(this.size);
		
	}
	
	this.decreaseFontSize = decreaseFontSize;
	this.increaseFontSize = increaseFontSize;
	this.init = init;
	this.resetFontSize = resetFontSize;
	this.setNewFontSize = setNewFontSize;

	/* At construction time of the fontSizeController the properties are undefined. The properties
	 * will only be defined after a call to the method 'init'. To be able to check whether or not the
	 * fontSizeController has been initialized we set the font size to zero. indicating that the
	 * fontSizeController has not been initialised yet.
	 */
	this.size = 0;
	
}

sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}

if (window.attachEvent) window.attachEvent("onload", sfHover);




