var gBrN     = navigator.appName.toLowerCase();
var gIsIE    = (gBrN.indexOf("explorer") > -1) ? true : false;
var gIsNS    = (gBrN.indexOf("netscape") > -1) ? true : false;
var gIsGecko = (navigator.product == "Gecko") ? true : false;
var gIsOpera = (gBrN.indexOf("opera") > -1) ? true : false;
var gBrV     = (gIsIE) ? parseFloat((navigator.appVersion.split("MSIE"))[1]) : parseFloat(navigator.appVersion);

var gDomain          = "";
var gFontStyleSheets = new Array();
var gFontCookieName  = "fontsize";
var gFontCookieValue = 1;
var gFontUp          = null;
var gFontDown        = null;
var gFontUpI         = null;
var gFontDownI       = null;

var gStyleSheetSwappingEnabled = ((gIsIE || gIsNS || gIsGecko || gIsOpera) && gBrV >= 5) ? true : false;

///////////////////////////////////////////////////////////////////////////////
//	Name:		BuildFontControls
//	Description:	Reads document defined alternate style sheets into an
//			array and creates controls for font resizing.
//	Accepts:	-
//	Returns:	-
///////////////////////////////////////////////////////////////////////////////

function BuildFontControls(domain) {
	
	gDomain = domain;
	
	if(gStyleSheetSwappingEnabled) {
	
		var i, link;

		for(i = 0; (link = document.getElementsByTagName("link")[i]); i++) {

		   	var title = link.getAttribute("title");
			
			if(title) {
			
				title = title.toLowerCase();
				
				if(link.getAttribute("rel").indexOf("alter") != -1 && title.indexOf("fonttikoko") != -1)
					gFontStyleSheets.push(title);
			}

		}	
		
		document.write('Kirjainkoko<a href="#" onClick="SwapStylesheet(1)" id="fontUp"><img id="fontUpI" src="' + domain + '/images/kirjainkoko_plus.gif" alt="Suurenna kirjainkokoa" width="16" height="11" border="0" /></a><a href="#" onClick="SwapStylesheet(-1)" id="fontDown"><img id="fontDownI" src="' + domain + '/images/kirjainkoko_miinus.gif" alt="Pienennä kirjainkokoa" width="25" height="11" border="0" /></a>');

		gFontUp    = document.getElementById('fontUp');
		gFontDown  = document.getElementById('fontDown');
		gFontUpI   = document.getElementById('fontUpI');
		gFontDownI = document.getElementById('fontDownI');
		
		if(!gFontUp || !gFontDown || !gFontUpI || !gFontDownI) {
			gStyleSheetSwappingEnabled = false;
		}
		
		GetFontCookie();

	}

}

///////////////////////////////////////////////////////////////////////////////
//	Name:		UpdateFontControls
//	Description:	Updates the font control status.
//	Accepts:	-
//	Returns:	-
///////////////////////////////////////////////////////////////////////////////

function UpdateFontControls() {

	if(gStyleSheetSwappingEnabled) {
			
		gFontUp.disabled   = (gFontCookieValue >= gFontStyleSheets.length - 1) ? true : false;
		//gFontUpI.src       = gDomain + "/images/kirjainkoko_plus" + ((gFontUp.disabled) ? "D" : "") + ".gif";
		gFontDown.disabled = (gFontCookieValue < 1) ? true : false;
		//gFontDownI.src     = gDomain + "/images/kirjainkoko_miinus" + ((gFontDown.disabled) ? "D" : "") + ".gif";
		
	}

}

///////////////////////////////////////////////////////////////////////////////
//	Name:		GetFontCookie
//	Description:	Retrieves the cookie value for font size setting.
//	Accepts:	-
//	Returns:	-
///////////////////////////////////////////////////////////////////////////////

function GetFontCookie() {

	var start = document.cookie.indexOf((gFontCookieName + "="));	

	if(start > -1) {
		gFontCookieValue = parseInt(document.cookie.substring(start + gFontCookieName.length + 1, document.cookie.length));	
	} else {
		SetFontCookie();
	}
	
	UpdateFontControls();
	SetActiveStyleSheet(gFontCookieValue);

}

///////////////////////////////////////////////////////////////////////////////
//	Name:		SetFontCookie
//	Description:	Writes the current font size setting into a cookie.
//	Accepts:	-
//	Returns:	-
///////////////////////////////////////////////////////////////////////////////

function SetFontCookie() {
	
	var days   = 7;
	var date   = new Date();
	var expiry = new Date(date.getTime() + (days * 86400000));
	
	document.cookie = gFontCookieName + "=" + gFontCookieValue + 
			  ";expires=" + expiry.toGMTString() +
			  //";domain=" + gDomain + ";" +
			  ";path=/;";

}

///////////////////////////////////////////////////////////////////////////////
//	Name:		SwapStylesheet
//	Description:	Swaps style sheets on mouse click.
//	Accepts:	A value that will be used to modify the current active
//			stylesheet index.
//	Returns:	-
///////////////////////////////////////////////////////////////////////////////

function SwapStylesheet(step) {

	if(gStyleSheetSwappingEnabled) {
	
		SetActiveStyleSheet(gFontCookieValue + step);
		SetFontCookie();
		UpdateFontControls();
			
		if(gIsGecko) {
			window.location.reload( false );

			//alert(gFontCookieValue);
		}

				
	}
	
}

///////////////////////////////////////////////////////////////////////////////
//	Name:		SetActiveStyleSheet
//	Description:	Sets the active stylesheet based on the passed id.
//	Accepts:	The id of the style sheet to be set active.
//	Returns:	-
///////////////////////////////////////////////////////////////////////////////

function SetActiveStyleSheet(id) {
	
	if(gStyleSheetSwappingEnabled) {
	
		if(id >= 0 && id < gFontStyleSheets.length) {

			var title = gFontStyleSheets[id];
			var link;

			for(var i = 0; (link = document.getElementsByTagName("link")[i]); i++) {

				if(link.getAttribute("rel").indexOf("alter") != -1 && link.getAttribute("title")) {

					link.disabled = true;

					if(link.getAttribute("title").toLowerCase() == title)
						link.disabled = false;

				}

			}

			gFontCookieValue = id;

		}
		
	}
	
}

///////////////////////////////////////////////////////////////////////////////
//	Name:		RestoreFontSize
//	Description:	Restores the original font settings.
//	Accepts:	-
//	Returns:	-
///////////////////////////////////////////////////////////////////////////////

function RestoreFontSize() {

	gFontCookieValue = 0;
	SetActiveStyleSheet(1);

}