var fontSizes = ["fontSmall", "fontLarge"];
var fontSizeUrls = ["fontSmall.css","fontLarge.css"];
var fontIndex = -1;

function findFontSize(fs)
{
	for (var i=0; i<fontSizes.length; i++)
		if (fontSizes[i]==fs) return i;
	
	return -1;
}

function increaseFontSize()
{
	if (fontIndex<fontSizes.length-1)
	{
		setActiveFontSize( fontSizes[++fontIndex] );
		saveFontChoice();
	}
}

function decreaseFontSize()
{
	if (fontIndex>0)
	{
		setActiveFontSize( fontSizes[--fontIndex] );
		saveFontChoice();
	}
}

function findStylesheetElement(fs,url)
{
	var stylesheets = document.getElementsByTagName("link");
	for (var i=0; i<stylesheets.length; i++)
	{
		if (stylesheets[i].getAttribute("title")==fs)
			return stylesheets[i];
	}
	
	//var index = fontSizes.indexOf(fs);
	
	//alert('Create');
	
	var head = document.getElementsByTagName("head")[0];
	var lnk = document.createElement("link");
	lnk.setAttribute("rel", "stylesheet");
	lnk.setAttribute("type", "text/css");
	lnk.setAttribute("href", url );
	lnk.setAttribute("title", fs);
	
	head.appendChild(lnk);
	
	return lnk;
}

function setActiveFontSize(fs)
{
	var ss = null;
	
	for (var i=0; i<fontSizes.length; i++)
		if (ss = findStylesheetElement(fontSizes[i], fontSizeUrls[i]))
			ss.disabled = fontSizes[i]!=fs;
}

function saveFontChoice()
{
	setCookie("fontSize", fontSizes[fontIndex], 7);
}

function loadFontChoice()
{
	// Not implemented
	return getCookie("fontSize");
}

function fontSizeInit()
{
	if (typeof(getCookie)=='undefined')
	{
		alert("Cookie.js script file not found");
		return;
	}

	var fontChoice = loadFontChoice();

	if (loadFontChoice==null) fontChoice = fontSizes[0];
	
	fontIndex = findFontSize(fontChoice);
	
	if (fontIndex==-1)
	{
		fontIndex = 0;
		fontChoice = fontSizes[0];
	}
	
	setActiveFontSize(fontChoice);
}

fontSizeInit();